Skip to content

Commit 3630e0c

Browse files
Grant 228 db mot standards (#56)
* Add VersionColumn to Entities * Initialize schema for db! * Add Initial Migration to Create Tables * Create Migrations for app_pbgb Schema * Add CreateTable.sql * Moving version to base entity! * Update Table migration! Co-authored-by: Leon <[email protected]>
1 parent 46e8eac commit 3630e0c

File tree

8 files changed

+120
-4
lines changed

8 files changed

+120
-4
lines changed

docker-compose.dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ services:
6464
container_name: ${PROJECT}_db
6565
volumes:
6666
- ./database:/database
67+
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
6768
ports:
6869
- "5432:5432"
6970
environment:

init.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE SCHEMA IF NOT EXISTS app_pbgp;

server/CreateTables.sql

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE TABLE "app_pbgp"."pbgp_form_metadata" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "form_metadata_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "name" character varying(200) NOT NULL, "description" character varying(2000) NOT NULL DEFAULT '', "active" boolean NOT NULL DEFAULT true, "chefs_form_id" character varying(100) NOT NULL, "version_id" character varying(100) NOT NULL, "version_schema" jsonb NOT NULL, CONSTRAINT "PK_f34d5c3000f4465df58b64d2608" PRIMARY KEY ("form_metadata_id"));
2+
CREATE TABLE "app_pbgp"."pbgp_user" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "user_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "version" integer NOT NULL, "user_name" character varying(100) NOT NULL, "display_name" character varying(200) NOT NULL, "external_id" character varying(100) NOT NULL, "is_authorized" boolean NOT NULL DEFAULT false, "is_admin" boolean NOT NULL DEFAULT false, CONSTRAINT "UQ_87908396860506ed6b2d203bf5f" UNIQUE ("user_name"), CONSTRAINT "UQ_a7ae2d06650d83d7f27532520a6" UNIQUE ("display_name"), CONSTRAINT "UQ_0324c9f00ebd3c636b34a566581" UNIQUE ("external_id"), CONSTRAINT "PK_4106f29a6ab9a0a798618eefef7" PRIMARY KEY ("user_id"));
3+
CREATE INDEX "IDX_0324c9f00ebd3c636b34a56658" ON "app_pbgp"."pbgp_user" ("external_id") ;
4+
CREATE TABLE "app_pbgp"."pbgp_comment" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "comment_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "version" integer NOT NULL, "comment" character varying(2000) NOT NULL, "application_id" uuid, "user_id" uuid, CONSTRAINT "PK_7d497def055f46abad3511d396a" PRIMARY KEY ("comment_id"));
5+
CREATE TABLE "app_pbgp"."pbgp_application" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "application_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "version" integer NOT NULL, "submission" jsonb, "submission_id" character varying(300) NOT NULL, "confirmation_id" character varying(200) NOT NULL, "facility_name" character varying(200) NOT NULL, "project_title" character varying(100), "total_estimated_cost" money, "asks" money, "status" character varying(100) NOT NULL DEFAULT 'INITIAL_REVIEW', "form_id" uuid, "assigned_to_id" uuid, "last_updated_by_id" uuid, CONSTRAINT "UQ_e887af3337c3c6c1b19f99425ca" UNIQUE ("submission_id"), CONSTRAINT "PK_64b4d607de9e1d2becab547905c" PRIMARY KEY ("application_id"));
6+
CREATE TABLE "app_pbgp"."pbgp_attachment" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "attachment_id" character varying(100) NOT NULL, "version" integer NOT NULL, "url" character varying(200) NOT NULL, "data" bytea, "original_name" character varying(200) NOT NULL, CONSTRAINT "UQ_2523067e2f78eb27437501daac6" UNIQUE ("url"), CONSTRAINT "PK_755231ef579d75df8c8c5b0db7d" PRIMARY KEY ("attachment_id"));
7+
CREATE TABLE "app_pbgp"."pbgp_border_review_score" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "data" jsonb, "final_score" integer, "overall_comments" character varying(2000), "border_review_score_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "version" integer NOT NULL, "completion_status" character varying(30) NOT NULL DEFAULT 'IN_PROGRESS', "user_id" uuid, "application_id" uuid, CONSTRAINT "PK_0e0bb42e46644a879f20e78a516" PRIMARY KEY ("border_review_score_id"));
8+
ALTER TABLE "app_pbgp"."pbgp_comment" ADD CONSTRAINT "FK_aaece98bf11e4e6e23b40b32dd7" FOREIGN KEY ("application_id") REFERENCES "app_pbgp"."pbgp_application"("application_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
9+
ALTER TABLE "app_pbgp"."pbgp_comment" ADD CONSTRAINT "FK_cf5dd0252811f945a0e60b34096" FOREIGN KEY ("user_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
10+
ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_bab74b4141c1720bed375a4d2a4" FOREIGN KEY ("form_id") REFERENCES "app_pbgp"."pbgp_form_metadata"("form_metadata_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
11+
ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_5542578e778e096d2502a95ec1f" FOREIGN KEY ("assigned_to_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
12+
ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_5783a8c139f9aebc3c08feb0436" FOREIGN KEY ("last_updated_by_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
13+
ALTER TABLE "app_pbgp"."pbgp_border_review_score" ADD CONSTRAINT "FK_c8f3f699a2d2212425403ee5972" FOREIGN KEY ("user_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
14+
ALTER TABLE "app_pbgp"."pbgp_border_review_score" ADD CONSTRAINT "FK_746c48c7645ab24acaaccff610e" FOREIGN KEY ("application_id") REFERENCES "app_pbgp"."pbgp_application"("application_id") ON DELETE NO ACTION ON UPDATE NO ACTION;
15+
ALTER TABLE "app_pbgp"."pbgp_border_review_score" DROP CONSTRAINT "FK_746c48c7645ab24acaaccff610e";
16+
ALTER TABLE "app_pbgp"."pbgp_border_review_score" DROP CONSTRAINT "FK_c8f3f699a2d2212425403ee5972";
17+
ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_5783a8c139f9aebc3c08feb0436";
18+
ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_5542578e778e096d2502a95ec1f";
19+
ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_bab74b4141c1720bed375a4d2a4";
20+
ALTER TABLE "app_pbgp"."pbgp_comment" DROP CONSTRAINT "FK_cf5dd0252811f945a0e60b34096";
21+
ALTER TABLE "app_pbgp"."pbgp_comment" DROP CONSTRAINT "FK_aaece98bf11e4e6e23b40b32dd7";
22+
DROP TABLE "app_pbgp"."pbgp_border_review_score";
23+
DROP TABLE "app_pbgp"."pbgp_attachment";
24+
DROP TABLE "app_pbgp"."pbgp_application";
25+
DROP TABLE "app_pbgp"."pbgp_comment";
26+
DROP INDEX "app_pbgp"."IDX_0324c9f00ebd3c636b34a56658";
27+
DROP TABLE "app_pbgp"."pbgp_user";
28+
DROP TABLE "app_pbgp"."pbgp_form_metadata";

server/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
2525
"test:e2e": "NODE_ENV=test jest --config ./jest-e2e.json --runInBand",
2626
"typeorm": "ts-node -r tsconfig-paths/register ../../node_modules/typeorm/cli.js --config src/ormconfig.ts",
27-
"db:sync-chefs-data": "NODE_ENV=development node dist/database/scripts/sync-chefs-data.js"
27+
"db:sync-chefs-data": "NODE_ENV=development node dist/database/scripts/sync-chefs-data.js",
28+
"migration:create-tables": "npx typeorm migration:generate -n CreateTables --config dist/ormconfig.js"
2829
},
2930
"dependencies": {
3031
"@nestjs/common": "8.2.3",
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { CreateDateColumn, UpdateDateColumn } from 'typeorm';
1+
import { CreateDateColumn, UpdateDateColumn, VersionColumn } from 'typeorm';
22

33
export class CustomBaseEntity {
44
@CreateDateColumn({ name: 'db_create_timestamp' })
55
createdAt: Date;
66

77
@UpdateDateColumn({ name: 'db_last_update_timestamp' })
88
updatedAt: Date;
9+
10+
@VersionColumn()
11+
version: number;
912
}

server/src/database/database.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ import { AttachmentModule } from '../attachments/attachment.module';
4747

4848
const appOrmConfig: PostgresConnectionOptions = {
4949
...config,
50-
migrationsRun: false,
51-
synchronize: true,
50+
migrationsRun: true,
51+
synchronize: false,
5252
};
5353

5454
@Module({
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { MigrationInterface, QueryRunner } from 'typeorm';
2+
3+
export class CreateTables1673468340058 implements MigrationInterface {
4+
name = 'CreateTables1673468340058';
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(
8+
`CREATE TABLE "app_pbgp"."pbgp_form_metadata" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "form_metadata_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "name" character varying(200) NOT NULL, "description" character varying(2000) NOT NULL DEFAULT '', "active" boolean NOT NULL DEFAULT true, "chefs_form_id" character varying(100) NOT NULL, "version_id" character varying(100) NOT NULL, "version_schema" jsonb NOT NULL, CONSTRAINT "PK_f34d5c3000f4465df58b64d2608" PRIMARY KEY ("form_metadata_id"))`
9+
);
10+
await queryRunner.query(
11+
`CREATE TABLE "app_pbgp"."pbgp_user" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "user_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "user_name" character varying(100) NOT NULL, "display_name" character varying(200) NOT NULL, "external_id" character varying(100) NOT NULL, "is_authorized" boolean NOT NULL DEFAULT false, "is_admin" boolean NOT NULL DEFAULT false, CONSTRAINT "UQ_87908396860506ed6b2d203bf5f" UNIQUE ("user_name"), CONSTRAINT "UQ_a7ae2d06650d83d7f27532520a6" UNIQUE ("display_name"), CONSTRAINT "UQ_0324c9f00ebd3c636b34a566581" UNIQUE ("external_id"), CONSTRAINT "PK_4106f29a6ab9a0a798618eefef7" PRIMARY KEY ("user_id"))`
12+
);
13+
await queryRunner.query(
14+
`CREATE INDEX "IDX_0324c9f00ebd3c636b34a56658" ON "app_pbgp"."pbgp_user" ("external_id") `
15+
);
16+
await queryRunner.query(
17+
`CREATE TABLE "app_pbgp"."pbgp_comment" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "comment_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "comment" character varying(2000) NOT NULL, "application_id" uuid, "user_id" uuid, CONSTRAINT "PK_7d497def055f46abad3511d396a" PRIMARY KEY ("comment_id"))`
18+
);
19+
await queryRunner.query(
20+
`CREATE TABLE "app_pbgp"."pbgp_application" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "application_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "submission" jsonb, "submission_id" character varying(300) NOT NULL, "confirmation_id" character varying(200) NOT NULL, "facility_name" character varying(200) NOT NULL, "project_title" character varying(100), "total_estimated_cost" money, "asks" money, "status" character varying(100) NOT NULL DEFAULT 'INITIAL_REVIEW', "form_id" uuid, "assigned_to_id" uuid, "last_updated_by_id" uuid, CONSTRAINT "UQ_e887af3337c3c6c1b19f99425ca" UNIQUE ("submission_id"), CONSTRAINT "PK_64b4d607de9e1d2becab547905c" PRIMARY KEY ("application_id"))`
21+
);
22+
await queryRunner.query(
23+
`CREATE TABLE "app_pbgp"."pbgp_attachment" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "attachment_id" character varying(100) NOT NULL, "url" character varying(200) NOT NULL, "data" bytea, "original_name" character varying(200) NOT NULL, CONSTRAINT "UQ_2523067e2f78eb27437501daac6" UNIQUE ("url"), CONSTRAINT "PK_755231ef579d75df8c8c5b0db7d" PRIMARY KEY ("attachment_id"))`
24+
);
25+
await queryRunner.query(
26+
`CREATE TABLE "app_pbgp"."pbgp_border_review_score" ("db_create_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "db_last_update_timestamp" TIMESTAMP NOT NULL DEFAULT now(), "version" integer NOT NULL, "data" jsonb, "final_score" integer, "overall_comments" character varying(2000), "border_review_score_id" uuid NOT NULL DEFAULT uuid_generate_v4(), "completion_status" character varying(30) NOT NULL DEFAULT 'IN_PROGRESS', "user_id" uuid, "application_id" uuid, CONSTRAINT "PK_0e0bb42e46644a879f20e78a516" PRIMARY KEY ("border_review_score_id"))`
27+
);
28+
await queryRunner.query(
29+
`ALTER TABLE "app_pbgp"."pbgp_comment" ADD CONSTRAINT "FK_aaece98bf11e4e6e23b40b32dd7" FOREIGN KEY ("application_id") REFERENCES "app_pbgp"."pbgp_application"("application_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
30+
);
31+
await queryRunner.query(
32+
`ALTER TABLE "app_pbgp"."pbgp_comment" ADD CONSTRAINT "FK_cf5dd0252811f945a0e60b34096" FOREIGN KEY ("user_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
33+
);
34+
await queryRunner.query(
35+
`ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_bab74b4141c1720bed375a4d2a4" FOREIGN KEY ("form_id") REFERENCES "app_pbgp"."pbgp_form_metadata"("form_metadata_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
36+
);
37+
await queryRunner.query(
38+
`ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_5542578e778e096d2502a95ec1f" FOREIGN KEY ("assigned_to_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
39+
);
40+
await queryRunner.query(
41+
`ALTER TABLE "app_pbgp"."pbgp_application" ADD CONSTRAINT "FK_5783a8c139f9aebc3c08feb0436" FOREIGN KEY ("last_updated_by_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
42+
);
43+
await queryRunner.query(
44+
`ALTER TABLE "app_pbgp"."pbgp_border_review_score" ADD CONSTRAINT "FK_c8f3f699a2d2212425403ee5972" FOREIGN KEY ("user_id") REFERENCES "app_pbgp"."pbgp_user"("user_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
45+
);
46+
await queryRunner.query(
47+
`ALTER TABLE "app_pbgp"."pbgp_border_review_score" ADD CONSTRAINT "FK_746c48c7645ab24acaaccff610e" FOREIGN KEY ("application_id") REFERENCES "app_pbgp"."pbgp_application"("application_id") ON DELETE NO ACTION ON UPDATE NO ACTION`
48+
);
49+
}
50+
51+
public async down(queryRunner: QueryRunner): Promise<void> {
52+
await queryRunner.query(
53+
`ALTER TABLE "app_pbgp"."pbgp_border_review_score" DROP CONSTRAINT "FK_746c48c7645ab24acaaccff610e"`
54+
);
55+
await queryRunner.query(
56+
`ALTER TABLE "app_pbgp"."pbgp_border_review_score" DROP CONSTRAINT "FK_c8f3f699a2d2212425403ee5972"`
57+
);
58+
await queryRunner.query(
59+
`ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_5783a8c139f9aebc3c08feb0436"`
60+
);
61+
await queryRunner.query(
62+
`ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_5542578e778e096d2502a95ec1f"`
63+
);
64+
await queryRunner.query(
65+
`ALTER TABLE "app_pbgp"."pbgp_application" DROP CONSTRAINT "FK_bab74b4141c1720bed375a4d2a4"`
66+
);
67+
await queryRunner.query(
68+
`ALTER TABLE "app_pbgp"."pbgp_comment" DROP CONSTRAINT "FK_cf5dd0252811f945a0e60b34096"`
69+
);
70+
await queryRunner.query(
71+
`ALTER TABLE "app_pbgp"."pbgp_comment" DROP CONSTRAINT "FK_aaece98bf11e4e6e23b40b32dd7"`
72+
);
73+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_border_review_score"`);
74+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_attachment"`);
75+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_application"`);
76+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_comment"`);
77+
await queryRunner.query(`DROP INDEX "app_pbgp"."IDX_0324c9f00ebd3c636b34a56658"`);
78+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_user"`);
79+
await queryRunner.query(`DROP TABLE "app_pbgp"."pbgp_form_metadata"`);
80+
}
81+
}

server/src/ormconfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const config: PostgresConnectionOptions = {
1818
username: process.env.POSTGRES_USERNAME || 'freshworks',
1919
password: process.env.POSTGRES_PASSWORD,
2020
database: process.env.POSTGRES_DATABASE || 'pbgp',
21+
schema: 'app_pbgp',
2122
cli: {
2223
migrationsDir: 'src/migration',
2324
entitiesDir: 'src/**/*.entity.ts',

0 commit comments

Comments
 (0)