From 823520d9cb99a9eeeebc4a20495e290132aa6eb8 Mon Sep 17 00:00:00 2001 From: Dave Date: Sat, 13 Jun 2020 15:29:18 +0100 Subject: [PATCH] refactor: make db use schema --- config/default.js | 1 + src/api/db/create-db.js | 10 ++++ src/api/db/index.js | 3 +- src/api/db/sql/db-create.sql | 107 ++++++----------------------------- 4 files changed, 30 insertions(+), 91 deletions(-) diff --git a/config/default.js b/config/default.js index 8611bdc..a5c4f6f 100644 --- a/config/default.js +++ b/config/default.js @@ -20,6 +20,7 @@ module.exports = { host: db_host || process.env.DB_HOST, password: db_password || process.env.DB_PASSWORD, user: db_user || process.env.DB_USER, + schema: "auth", }, server: { diff --git a/src/api/db/create-db.js b/src/api/db/create-db.js index 88ef1cb..cb9d132 100644 --- a/src/api/db/create-db.js +++ b/src/api/db/create-db.js @@ -25,6 +25,14 @@ const createDB = async (dbConfig) => { // reconnect client first const migrationClient = await pool.connect(); + // create schema if not exists + await migrationClient.query( + `CREATE SCHEMA IF NOT EXISTS ${dbConfig.schema}` + ); + + // set default schema + await migrationClient.query(`SET SCHEMA '${dbConfig.schema}'`); + await migrationClient.query(sql); await migrationClient.query(` INSERT INTO DB_VERSION (ID, VERSION) @@ -52,6 +60,8 @@ const createDB = async (dbConfig) => { try { debug("Checking for initial migration ...."); + // set default schema + await client.query(`SET SCHEMA '${dbConfig.schema}'`); const result = await client.query( "select exists(select version from db_version where version >= 1) as migrated" ); diff --git a/src/api/db/index.js b/src/api/db/index.js index 9bddafa..8e81e48 100644 --- a/src/api/db/index.js +++ b/src/api/db/index.js @@ -15,7 +15,7 @@ class DBConnection { password: dbConfig.password, port: dbConfig.port, }); - + this.schema = dbConfig.schema; this.client = await this.pool.connect(); debug("Connected to DB successfully"); } catch (error) { @@ -28,6 +28,7 @@ class DBConnection { if (!this.pool) { await this.connect(); } + await this.client.query(`SET SCHEMA '${this.schema}'`); const result = await this.client.query(sql, params); return result.rows; } diff --git a/src/api/db/sql/db-create.sql b/src/api/db/sql/db-create.sql index 168739c..f166fae 100644 --- a/src/api/db/sql/db-create.sql +++ b/src/api/db/sql/db-create.sql @@ -1,102 +1,29 @@ -CREATE TABLE IF NOT EXISTS public.db_version ( +CREATE TABLE IF NOT EXISTS db_version ( version numeric NOT NULL, - id smallint NOT NULL + id serial PRIMARY KEY ); - -CREATE SEQUENCE IF NOT EXISTS public.db_version_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - -CREATE TABLE IF NOT EXISTS public.organizations ( +CREATE TABLE IF NOT EXISTS organizations ( organization_name character varying NOT NULL, - organization_id bigint NOT NULL, - organization_key character varying NOT NULL, - email character varying NOT NULL, - password character varying NOT NULL + organization_id bigserial PRIMARY KEY, + organization_email character varying UNIQUE, + organization_key character varying NOT NULL ); -CREATE SEQUENCE IF NOT EXISTS public.organization_organization_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; +CREATE TABLE IF NOT EXISTS admins ( + admin_id bigserial PRIMARY KEY, + fullname character varying NOT NULL, + email character varying NOT NULL, + password character varying NOT NULL, + organization_id bigint NOT NULL REFERENCES organizations(organization_id) +); -CREATE TABLE IF NOT EXISTS public.users ( +CREATE TABLE IF NOT EXISTS users ( user_id bigint NOT NULL, password character varying NOT NULL, email character varying NOT NULL, - organization_id bigint, - fullname character varying NOT NULL + organization_id bigint REFERENCES organizations(organization_id), + fullname character varying NOT NULL, + UNIQUE(organization_id, email) ); - - -CREATE SEQUENCE IF NOT EXISTS public.users_user_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - -ALTER TABLE ONLY public.db_version ALTER COLUMN id SET DEFAULT nextval('public.db_version_id_seq'::regclass); - -ALTER TABLE ONLY public.organizations ALTER COLUMN organization_id SET DEFAULT nextval('public.organization_organization_id_seq'::regclass); - -ALTER TABLE ONLY public.users ALTER COLUMN user_id SET DEFAULT nextval('public.users_user_id_seq'::regclass); - -ALTER TABLE ONLY public.db_version - DROP CONSTRAINT IF EXISTS db_version_pkey; -ALTER TABLE ONLY public.db_version - ADD CONSTRAINT db_version_pkey PRIMARY KEY (id); - --- --- Name: organization_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres --- - -ALTER TABLE ONLY public.organizations - DROP CONSTRAINT IF EXISTS organization_pkey; -ALTER TABLE ONLY public.organizations - ADD CONSTRAINT organization_pkey PRIMARY KEY (organization_id); - -ALTER TABLE public.organizations - DROP CONSTRAINT IF EXISTS organizations_email_key; -ALTER TABLE public.organizations - ADD CONSTRAINT organizations_email_key UNIQUE(email); - - --- --- Name: users_organization_id_email_key; Type: CONSTRAINT; Schema: public; Owner: postgres --- - -ALTER TABLE ONLY public.users - DROP CONSTRAINT IF EXISTS users_organization_id_email_key; -ALTER TABLE ONLY public.users - ADD CONSTRAINT users_organization_id_email_key UNIQUE (organization_id, email); - - --- --- Name: users_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres --- - -ALTER TABLE ONLY public.users - DROP CONSTRAINT IF EXISTS users_pkey; -ALTER TABLE ONLY public.users - ADD CONSTRAINT users_pkey PRIMARY KEY (user_id); - - --- --- Name: users_organization_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres --- - -ALTER TABLE ONLY public.users - DROP CONSTRAINT IF EXISTS users_organization_id_fkey; -ALTER TABLE ONLY public.users - ADD CONSTRAINT users_organization_id_fkey FOREIGN KEY (organization_id) REFERENCES public.organizations(organization_id); - -