Skip to content

Commit

Permalink
Merge pull request #84 from panoratech/db_upgrade_v108
Browse files Browse the repository at this point in the history
update: db model_v108
  • Loading branch information
rflihxyz authored Nov 15, 2023
2 parents 93f6eee + 24f2e76 commit d0e45b7
Showing 1 changed file with 119 additions and 114 deletions.
233 changes: 119 additions & 114 deletions packages/api/scripts/init.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@



-- ************************************** organizations

CREATE TABLE organizations
Expand All @@ -18,24 +16,6 @@ CREATE TABLE organizations



-- ************************************** jobs

CREATE TABLE jobs
(
id_job serial NOT NULL,
status text NOT NULL,
"timestamp" timestamp NOT NULL DEFAULT NOW(),
CONSTRAINT PK_jobs PRIMARY KEY ( id_job )
);



COMMENT ON COLUMN jobs.status IS 'pending,, retry_scheduled, failed, success';





-- ************************************** users

CREATE TABLE users
Expand Down Expand Up @@ -88,197 +68,222 @@ CREATE INDEX FK_1_projects ON projects



-- ************************************** jobs_status_history
-- ************************************** linked_users

CREATE TABLE jobs_status_history
CREATE TABLE linked_users
(
id_jobs_status_history serial NOT NULL,
"timestamp" timestamp NOT NULL DEFAULT NOW(),
previous_status text NOT NULL,
new_status text NOT NULL,
id_job serial NOT NULL,
CONSTRAINT PK_1 PRIMARY KEY ( id_jobs_status_history ),
CONSTRAINT FK_4 FOREIGN KEY ( id_job ) REFERENCES jobs ( id_job )
id_linked_user bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
linked_user_origin_id text NOT NULL,
alias text NOT NULL,
status text NOT NULL,
id_project bigint NOT NULL,
CONSTRAINT key_id_linked_users PRIMARY KEY ( id_linked_user ),
CONSTRAINT FK_10 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project )
);

CREATE INDEX id_job_jobs_status_history ON jobs_status_history
CREATE INDEX FK_proectID_linked_users ON linked_users
(
id_job
id_project
);



COMMENT ON COLUMN jobs_status_history.previous_status IS 'void when first initialization';
COMMENT ON COLUMN jobs_status_history.new_status IS 'pending, retry_scheduled, failed, success';
COMMENT ON COLUMN linked_users.linked_user_origin_id IS 'id of the customer, in our customers own systems';
COMMENT ON COLUMN linked_users.alias IS 'human-readable alias, for UI (ex ACME company)';
COMMENT ON COLUMN linked_users.status IS 'ONLY FOR INVITE LINK';





-- ************************************** crm_contacts
-- ************************************** api_keys

CREATE TABLE crm_contacts
CREATE TABLE api_keys
(
id_crm_contact serial NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
id_job serial NOT NULL,
CONSTRAINT PK_crm_contacts PRIMARY KEY ( id_crm_contact ),
CONSTRAINT job_id_crm_contact FOREIGN KEY ( id_job ) REFERENCES jobs ( id_job )
id_api_key bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
api_key_hash text NOT NULL,
id_user int NOT NULL,
id_project bigint NOT NULL,
CONSTRAINT id_ PRIMARY KEY ( id_api_key ),
CONSTRAINT unique_api_keys UNIQUE ( api_key_hash ),
CONSTRAINT FK_8 FOREIGN KEY ( id_user ) REFERENCES users ( id_user ),
CONSTRAINT FK_7 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project )
);

CREATE INDEX crm_contact_id_job ON crm_contacts
CREATE INDEX FK_2 ON api_keys
(
id_job
id_user
);

CREATE INDEX FK_api_keys_projects ON api_keys
(
id_project
);







-- ************************************** linked_users

CREATE TABLE linked_users
-- ************************************** jobs

CREATE TABLE jobs
(
id_linked_user bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
linked_user_origin_id text NOT NULL,
alias text NOT NULL,
status text NOT NULL,
id_project bigint NOT NULL,
CONSTRAINT key_id_linked_users PRIMARY KEY ( id_linked_user ),
CONSTRAINT FK_10 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project )
id_job serial NOT NULL,
status text NOT NULL,
"timestamp" timestamp NOT NULL DEFAULT NOW(),
id_linked_user bigint NOT NULL,
CONSTRAINT PK_jobs PRIMARY KEY ( id_job ),
CONSTRAINT FK_12 FOREIGN KEY ( id_linked_user ) REFERENCES linked_users ( id_linked_user )
);

CREATE INDEX FK_proectID_linked_users ON linked_users
CREATE INDEX FK_linkeduserID_projectID ON jobs
(
id_project
id_linked_user
);



COMMENT ON COLUMN linked_users.linked_user_origin_id IS 'id of the customer, in our customers own systems';
COMMENT ON COLUMN linked_users.alias IS 'human-readable alias, for UI (ex ACME company)';
COMMENT ON COLUMN linked_users.status IS 'ONLY FOR INVITE LINK';
COMMENT ON COLUMN jobs.status IS 'pending,, retry_scheduled, failed, success';





-- ************************************** crm_contacts_phone_numbers
-- ************************************** connections

CREATE TABLE crm_contacts_phone_numbers
CREATE TABLE connections
(
id_crm_contacts_phone_number serial NOT NULL,
phone_number text NOT NULL,
phone_type text NOT NULL,
id_crm_contact serial NOT NULL,
CONSTRAINT PK_crm_contacts_phone_numbers PRIMARY KEY ( id_crm_contacts_phone_number ),
CONSTRAINT FK_2 FOREIGN KEY ( id_crm_contact ) REFERENCES crm_contacts ( id_crm_contact )
id_connection bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
provider_slug text NOT NULL,
account_url text NULL,
token_type text NOT NULL,
access_token text NULL,
refresh_token text NULL,
expiration_timestamp timestamp NULL,
created_at timestamp NOT NULL,
id_project bigint NOT NULL,
id_linked_user bigint NOT NULL,
CONSTRAINT PK_connections PRIMARY KEY ( id_connection ),
CONSTRAINT Index_3 UNIQUE ( access_token, refresh_token ),
CONSTRAINT FK_9 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project ),
CONSTRAINT FK_11 FOREIGN KEY ( id_linked_user ) REFERENCES linked_users ( id_linked_user )
);

CREATE INDEX crm_contactID_crm_contact_phone_number ON crm_contacts_phone_numbers
CREATE INDEX FK_1 ON connections
(
id_crm_contact
id_project
);

CREATE INDEX FK_connections_to_LinkedUsersID ON connections
(
id_linked_user
);



COMMENT ON COLUMN connections.token_type IS 'The type of the token, such as "Bearer," "JWT," or any other supported type.';





-- ************************************** crm_contact_email_addresses
-- ************************************** jobs_status_history

CREATE TABLE crm_contact_email_addresses
CREATE TABLE jobs_status_history
(
id_crm_contact_email serial NOT NULL,
email_address text NOT NULL,
email_address_type text NOT NULL,
id_crm_contact serial NOT NULL,
CONSTRAINT PK_crm_contact_email_addresses PRIMARY KEY ( id_crm_contact_email ),
CONSTRAINT FK_3 FOREIGN KEY ( id_crm_contact ) REFERENCES crm_contacts ( id_crm_contact )
id_jobs_status_history serial NOT NULL,
"timestamp" timestamp NOT NULL DEFAULT NOW(),
previous_status text NOT NULL,
new_status text NOT NULL,
id_job serial NOT NULL,
CONSTRAINT PK_1 PRIMARY KEY ( id_jobs_status_history ),
CONSTRAINT FK_4 FOREIGN KEY ( id_job ) REFERENCES jobs ( id_job )
);

CREATE INDEX crm_contactID_crm_contact_email_address ON crm_contact_email_addresses
CREATE INDEX id_job_jobs_status_history ON jobs_status_history
(
id_crm_contact
id_job
);



COMMENT ON COLUMN jobs_status_history.previous_status IS 'void when first initialization';
COMMENT ON COLUMN jobs_status_history.new_status IS 'pending, retry_scheduled, failed, success';





-- ************************************** api_keys
-- ************************************** crm_contacts

CREATE TABLE api_keys
CREATE TABLE crm_contacts
(
id_api_key bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
api_key_hash text NOT NULL,
id_user int NOT NULL,
id_project bigint NOT NULL,
CONSTRAINT id_ PRIMARY KEY ( id_api_key ),
CONSTRAINT unique_api_keys UNIQUE ( api_key_hash ),
CONSTRAINT FK_8 FOREIGN KEY ( id_user ) REFERENCES users ( id_user ),
CONSTRAINT FK_7 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project )
id_crm_contact serial NOT NULL,
first_name text NOT NULL,
last_name text NOT NULL,
id_job serial NOT NULL,
CONSTRAINT PK_crm_contacts PRIMARY KEY ( id_crm_contact ),
CONSTRAINT job_id_crm_contact FOREIGN KEY ( id_job ) REFERENCES jobs ( id_job )
);

CREATE INDEX FK_2 ON api_keys
CREATE INDEX crm_contact_id_job ON crm_contacts
(
id_user
id_job
);

CREATE INDEX FK_api_keys_projects ON api_keys
(
id_project
);







-- ************************************** crm_contacts_phone_numbers

-- ************************************** connections
CREATE TABLE crm_contacts_phone_numbers
(
id_crm_contacts_phone_number serial NOT NULL,
phone_number text NOT NULL,
phone_type text NOT NULL,
id_crm_contact serial NOT NULL,
CONSTRAINT PK_crm_contacts_phone_numbers PRIMARY KEY ( id_crm_contacts_phone_number ),
CONSTRAINT FK_2 FOREIGN KEY ( id_crm_contact ) REFERENCES crm_contacts ( id_crm_contact )
);

CREATE TABLE connections
CREATE INDEX crm_contactID_crm_contact_phone_number ON crm_contacts_phone_numbers
(
id_connection bigint NOT NULL GENERATED ALWAYS AS IDENTITY,
provider_slug text NOT NULL,
account_url text NULL,
token_type text NOT NULL,
access_token text NULL,
refresh_token text NULL,
expiration_timestamp timestamp NULL,
created_at timestamp NOT NULL,
id_project bigint NOT NULL,
id_linked_user bigint NOT NULL,
CONSTRAINT PK_connections PRIMARY KEY ( id_connection ),
CONSTRAINT Index_3 UNIQUE ( access_token, refresh_token ),
CONSTRAINT FK_9 FOREIGN KEY ( id_project ) REFERENCES projects ( id_project ),
CONSTRAINT FK_11 FOREIGN KEY ( id_linked_user ) REFERENCES linked_users ( id_linked_user )
id_crm_contact
);

CREATE INDEX FK_1 ON connections







-- ************************************** crm_contact_email_addresses

CREATE TABLE crm_contact_email_addresses
(
id_project
id_crm_contact_email serial NOT NULL,
email_address text NOT NULL,
email_address_type text NOT NULL,
id_crm_contact serial NOT NULL,
CONSTRAINT PK_crm_contact_email_addresses PRIMARY KEY ( id_crm_contact_email ),
CONSTRAINT FK_3 FOREIGN KEY ( id_crm_contact ) REFERENCES crm_contacts ( id_crm_contact )
);

CREATE INDEX FK_connections_to_LinkedUsersID ON connections
CREATE INDEX crm_contactID_crm_contact_email_address ON crm_contact_email_addresses
(
id_linked_user
id_crm_contact
);



COMMENT ON COLUMN connections.token_type IS 'The type of the token, such as "Bearer," "JWT," or any other supported type.';



Expand Down

0 comments on commit d0e45b7

Please sign in to comment.