Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add Postgres migrations #310

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.15-alpine as build
FROM docker.io/golang:1.15-alpine as build
ENV GO111MODULE=on
ENV CGO_ENABLED=0
ENV GOOS=linux
Expand Down
1 change: 1 addition & 0 deletions migrations/20171026211738_create_users.postgres.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS {{ index .Options "Namespace" }}users;
25 changes: 25 additions & 0 deletions migrations/20171026211738_create_users.postgres.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
CREATE TABLE IF NOT EXISTS {{ index .Options "Namespace" }}users (
instance_id uuid,
id uuid PRIMARY KEY,
aud character varying(255),
role character varying(255),
email character varying(255),
encrypted_password character varying(255),
confirmed_at timestamp with time zone,
invited_at timestamp with time zone,
confirmation_token character varying(255),
confirmation_sent_at timestamp with time zone,
recovery_token character varying(255),
recovery_sent_at timestamp with time zone,
email_change_token character varying(255),
email_change character varying(255),
email_change_sent_at timestamp with time zone,
last_sign_in_at timestamp with time zone,
raw_app_meta_data text,
raw_user_meta_data text,
is_super_admin boolean,
created_at timestamp with time zone,
updated_at timestamp with time zone
);
CREATE INDEX users_instance_id_idx ON {{ index .Options "Namespace" }}users USING btree (instance_id);
CREATE INDEX users_instance_id_email_idx ON {{ index .Options "Namespace" }}users USING btree (instance_id, email);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS {{ index .Options "Namespace" }}instances;
8 changes: 8 additions & 0 deletions migrations/20171026211808_create_instances.postgres.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS {{ index .Options "Namespace" }}instances (
id uuid PRIMARY KEY,
uuid uuid,
raw_base_config text,
created_at timestamp with time zone,
updated_at timestamp with time zone,
deleted_at timestamp with time zone
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS {{ index .Options "Namespace" }}refresh_tokens;
12 changes: 12 additions & 0 deletions migrations/20171026211834_create_refresh_tokens.postgres.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE IF NOT EXISTS {{ index .Options "Namespace" }}refresh_tokens (
instance_id uuid,
id bigserial PRIMARY KEY,
token character varying(255),
user_id character varying(255),
revoked boolean,
created_at timestamp with time zone
);

CREATE INDEX refresh_tokens_instance_id_idx ON {{ index .Options "Namespace" }}refresh_tokens USING btree (instance_id);
CREATE INDEX refresh_tokens_instance_id_user_id_idx ON {{ index .Options "Namespace" }}refresh_tokens USING btree (instance_id, user_id);
CREATE INDEX refresh_tokens_token_idx ON {{ index .Options "Namespace" }}refresh_tokens USING btree (token);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE {{ index .Options "Namespace" }}users
ALTER raw_app_meta_data TYPE text USING raw_app_meta_data #>> '{}',
ALTER raw_user_meta_data TYPE text USING raw_user_meta_data #>> '{}';
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
UPDATE {{ index .Options "Namespace" }}users SET raw_app_meta_data = '{}' WHERE raw_app_meta_data = '';
UPDATE {{ index .Options "Namespace" }}users SET raw_user_meta_data = '{}' WHERE raw_user_meta_data = '';

ALTER TABLE {{ index .Options "Namespace" }}users
ALTER raw_app_meta_data TYPE JSONB USING raw_app_meta_data::JSONB,
ALTER raw_user_meta_data TYPE JSONB USING raw_user_meta_data::JSONB;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE {{ index .Options "Namespace" }}instances ADD deleted_at timestamp with time zone;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE {{ index .Options "Namespace" }}instances DROP deleted_at;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE IF EXISTS {{ index .Options "Namespace" }}audit_log_entries;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE IF NOT EXISTS {{ index .Options "Namespace" }}audit_log_entries (
instance_id uuid,
id uuid PRIMARY KEY,
payload json,
created_at timestamp with time zone
);

CREATE INDEX audit_logs_instance_id_idx ON {{ index .Options "Namespace" }}audit_log_entries USING btree (instance_id);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE {{ index .Options "Namespace" }}refresh_tokens DROP updated_at;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE {{ index .Options "Namespace" }}refresh_tokens ADD updated_at timestamp with time zone DEFAULT NULL;
UPDATE {{ index .Options "Namespace" }}refresh_tokens SET updated_at = created_at;