Skip to content

Commit

Permalink
fix(authentication-service): fixed migrations
Browse files Browse the repository at this point in the history
MIGRATION CHANGE:
migration-20210318100600- Changed ids to uuids, changed timestamps

BREAKING CHANGE:
Automigration Added - will need to add SKIP env variable in existing project, read docs for help

gh-124
  • Loading branch information
akshatdubeysf committed Apr 28, 2021
1 parent cfebd0c commit 14c137b
Show file tree
Hide file tree
Showing 2 changed files with 7,983 additions and 7,578 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,173 +5,202 @@ SET search_path TO main,public;
GRANT ALL ON SCHEMA main TO public;

CREATE TABLE main.auth_clients (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
id integer NOT NULL,
client_id text NOT NULL,
client_secret text NOT NULL,
secret text NOT NULL,
redirect_url text,
access_token_expiration integer NOT NULL,
refresh_token_expiration integer NOT NULL,
auth_code_expiration integer NOT NULL
);

CREATE TABLE main.roles (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
name text NOT NULL,
role_type integer NOT NULL,
permissions text
);
id integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
client_id varchar(50) NOT NULL ,
client_secret varchar(50) NOT NULL ,
redirect_url varchar(200) ,
access_token_expiration integer DEFAULT 900 NOT NULL ,
refresh_token_expiration integer DEFAULT 86400 NOT NULL ,
auth_code_expiration integer DEFAULT 180 NOT NULL ,
secret varchar(50) NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
deleted bool DEFAULT false NOT NULL ,
deleted_on timestamptz ,
deleted_by uuid ,
CONSTRAINT pk_auth_clients_id PRIMARY KEY ( id )
);

CREATE TABLE main.roles (
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
name varchar(100) NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by uuid ,
modified_by uuid ,
deleted bool DEFAULT false NOT NULL ,
permissions _text ,
role_type integer DEFAULT 0 NOT NULL ,
deleted_by uuid ,
deleted_on timestamptz ,
CONSTRAINT pk_roles_id PRIMARY KEY ( id )
);

CREATE TABLE main.tenant_configs (
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
config_key varchar(100) NOT NULL ,
config_value jsonb NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by integer ,
modified_by integer ,
deleted bool DEFAULT false NOT NULL ,
tenant_id uuid NOT NULL ,
deleted_by uuid ,
deleted_on timestamptz ,
CONSTRAINT pk_tenant_configs_id PRIMARY KEY ( id )
);

CREATE TABLE main.tenants (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
name text NOT NULL,
key text NOT NULL,
address text,
city text,
state text,
zip text,
country text,
status integer NOT NULL
);

CREATE TABLE main.tenant_configs (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
config_key text NOT NULL,
config_value text,
tenant_id text NOT NULL
);
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
name varchar(100) NOT NULL ,
status integer DEFAULT 0 NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by uuid ,
modified_by uuid ,
deleted bool DEFAULT false NOT NULL ,
"key" varchar(20) NOT NULL ,
address varchar(500) ,
city varchar(100) ,
"state" varchar(100) ,
zip varchar(25) ,
country varchar(25) ,
deleted_on timestamptz ,
deleted_by uuid ,
CONSTRAINT pk_tenants_id PRIMARY KEY ( id ),
CONSTRAINT idx_tenants UNIQUE ( "key" )
);


CREATE TABLE main.user_credentials (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
id text NOT NULL,
user_id text NOT NULL,
auth_provider text NOT NULL,
auth_id text,
auth_token text,
password text
);

CREATE TABLE main.user_permissions (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
user_tenant_id text NOT NULL,
permission text NOT NULL,
allowed boolean DEFAULT true NOT NULL
);


CREATE TABLE main.user_resources (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
user_tenant_id text NOT NULL,
resource_name text NOT NULL,
resource_value text NOT NULL,
allowed boolean DEFAULT true NOT NULL
);
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
user_id uuid NOT NULL ,
auth_provider varchar(50) DEFAULT 'internal'::character varying NOT NULL ,
auth_id varchar(100) ,
auth_token varchar(100) ,
"password" varchar(60) ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
deleted bool DEFAULT false NOT NULL ,
deleted_on timestamptz ,
deleted_by uuid ,
CONSTRAINT pk_user_credentials_id PRIMARY KEY ( id ),
CONSTRAINT idx_user_credentials_user_id UNIQUE ( user_id )
);

CREATE TABLE main.user_permissions (
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
user_tenant_id uuid NOT NULL ,
permission varchar(50) NOT NULL ,
allowed bool NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by uuid ,
modified_by uuid ,
deleted bool DEFAULT false NOT NULL ,
deleted_on timestamptz ,
deleted_by uuid ,
CONSTRAINT pk_user_permissions_id PRIMARY KEY ( id )
);

CREATE TABLE main.user_resources (
deleted bool DEFAULT false NOT NULL ,
deleted_on timestamptz ,
deleted_by uuid ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by uuid ,
modified_by uuid ,
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
user_tenant_id uuid ,
resource_name varchar(50) ,
resource_value varchar(100) ,
allowed bool DEFAULT true NOT NULL ,
CONSTRAINT user_resources_pkey PRIMARY KEY ( id )
);


CREATE TABLE main.user_tenants (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
id text NOT NULL,
status integer,
locale text,
tenant_id text NOT NULL,
user_id text NOT NULL,
role_id text NOT NULL
);
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
user_id uuid NOT NULL ,
tenant_id uuid NOT NULL ,
role_id uuid NOT NULL ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
deleted bool DEFAULT false NOT NULL ,
status integer DEFAULT 0 NOT NULL ,
locale varchar(5) ,
deleted_by uuid ,
deleted_on timestamptz ,
CONSTRAINT pk_user_tenants_id PRIMARY KEY ( id )
);


CREATE TABLE main.users (
deleted boolean DEFAULT false,
deleted_on timestamp with time zone,
deleted_by text,
created_on timestamp with time zone,
modified_on timestamp with time zone,
created_by text,
modified_by text,
id text NOT NULL,
first_name text NOT NULL,
last_name text,
middle_name text,
username text NOT NULL,
email text,
phone text,
auth_client_ids text,
last_login timestamp with time zone,
dob timestamp with time zone,
gender text,
default_tenant_id text
);

ALTER TABLE ONLY main.auth_clients
ADD CONSTRAINT auth_clients_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.roles
ADD CONSTRAINT roles_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.tenant_configs
ADD CONSTRAINT tenant_configs_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.tenants
ADD CONSTRAINT tenants_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.user_credentials
ADD CONSTRAINT user_credentials_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.user_permissions
ADD CONSTRAINT user_permissions_pkey PRIMARY KEY (id);


ALTER TABLE ONLY main.user_resources
ADD CONSTRAINT user_resources_pkey PRIMARY KEY (id);


ALTER TABLE ONLY main.user_tenants
ADD CONSTRAINT user_tenants_pkey PRIMARY KEY (id);

ALTER TABLE ONLY main.users
ADD CONSTRAINT users_pkey PRIMARY KEY (id);
id uuid DEFAULT md5(random()::text || clock_timestamp()::text)::uuid NOT NULL ,
first_name varchar(50) NOT NULL ,
middle_name varchar(50) ,
last_name varchar(50) ,
username varchar(150) NOT NULL ,
email varchar(150) ,
phone varchar(15) ,
created_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
modified_on timestamptz DEFAULT CURRENT_TIMESTAMP NOT NULL ,
created_by uuid ,
modified_by uuid ,
deleted bool DEFAULT false NOT NULL ,
last_login timestamptz ,
auth_client_ids integer[] ,
gender char(1) ,
dob date ,
default_tenant_id uuid ,
deleted_by uuid ,
deleted_on timestamptz ,
CONSTRAINT pk_users_id PRIMARY KEY ( id )
);




CREATE OR REPLACE FUNCTION main.moddatetime()
RETURNS trigger
LANGUAGE plpgsql
AS $function$
BEGIN
NEW.modified_on = now();
RETURN NEW;
END;
$function$
;

CREATE TRIGGER mdt_auth_clients BEFORE UPDATE ON main.auth_clients FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_roles BEFORE UPDATE ON main.roles FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_tenant_configs BEFORE UPDATE ON main.tenant_configs FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_tenants BEFORE UPDATE ON main.tenants FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_user_credentials BEFORE UPDATE ON main.user_credentials FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_user_permissions BEFORE UPDATE ON main.user_permissions FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_user_tenants BEFORE UPDATE ON main.user_tenants FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

CREATE TRIGGER mdt_users BEFORE UPDATE ON main.users FOR EACH ROW EXECUTE PROCEDURE main.moddatetime('modified_on');

ALTER TABLE main.tenant_configs ADD CONSTRAINT fk_tenant_configs_tenants FOREIGN KEY ( tenant_id ) REFERENCES main.tenants( id );

ALTER TABLE main.user_credentials ADD CONSTRAINT fk_user_credentials_users FOREIGN KEY ( user_id ) REFERENCES main.users( id );

ALTER TABLE main.user_permissions ADD CONSTRAINT fk_user_permissions FOREIGN KEY ( user_tenant_id ) REFERENCES main.user_tenants( id );

ALTER TABLE main.user_resources ADD CONSTRAINT fk_user_resources FOREIGN KEY ( user_tenant_id ) REFERENCES main.user_tenants( id );

ALTER TABLE main.user_tenants ADD CONSTRAINT fk_user_tenants_users FOREIGN KEY ( user_id ) REFERENCES main.users( id );

ALTER TABLE main.user_tenants ADD CONSTRAINT fk_user_tenants_tenants FOREIGN KEY ( tenant_id ) REFERENCES main.tenants( id );

ALTER TABLE main.user_tenants ADD CONSTRAINT fk_user_tenants_roles FOREIGN KEY ( role_id ) REFERENCES main.roles( id );

Loading

0 comments on commit 14c137b

Please sign in to comment.