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

refactor: make db use schema #13

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions config/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
10 changes: 10 additions & 0 deletions src/api/db/create-db.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"
);
Expand Down
3 changes: 2 additions & 1 deletion src/api/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
107 changes: 17 additions & 90 deletions src/api/db/sql/db-create.sql
Original file line number Diff line number Diff line change
@@ -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);