From 146e598bf040691489c98dec38a6530eec8c34a0 Mon Sep 17 00:00:00 2001 From: radmehr Date: Wed, 8 Jul 2026 09:14:10 +0330 Subject: [PATCH] feat: add initial migrations --- package-lock.json | 4 +- package.json | 12 ++- src/app.module.ts | 2 +- src/config/database.config.ts | 1 - src/config/environment.validation.ts | 1 - src/database/data-source.ts | 5 ++ src/migrations/1783489241015-InitialSchema.ts | 82 +++++++++++++++++++ 7 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 src/migrations/1783489241015-InitialSchema.ts diff --git a/package-lock.json b/package-lock.json index a3d3864..1da5e03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "borna-backend", + "name": "heala-backend", "version": "0.0.1", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "borna-backend", + "name": "heala-backend", "version": "0.0.1", "license": "UNLICENSED", "dependencies": { diff --git a/package.json b/package.json index 3160d07..e8a6579 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "borna-backend", + "name": "heala-backend", "version": "0.0.1", "description": "", "author": "", @@ -17,7 +17,15 @@ "test:watch": "jest --watch", "test:cov": "jest --coverage", "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", - "test:e2e": "jest --config ./test/jest-e2e.json" + "test:e2e": "jest --config ./test/jest-e2e.json", + "typeorm:dev": "cross-env NODE_ENV=development typeorm-ts-node-commonjs -d src/database/data-source.ts", + "typeorm:prod": "cross-env NODE_ENV=production typeorm -d dist/database/data-source.js", + "migration:generate": "npm run typeorm:dev -- migration:generate", + "migration:create": "npm run typeorm:dev -- migration:create", + "migration:run:dev": "npm run typeorm:dev -- migration:run", + "migration:run:prod": "npm run typeorm:prod -- migration:run", + "migration:revert:dev": "npm run typeorm:dev -- migration:revert", + "migration:revert:prod": "npm run typeorm:prod -- migration:revert" }, "dependencies": { "@nestjs/common": "^11.0.1", diff --git a/src/app.module.ts b/src/app.module.ts index b1b5433..c138d45 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -63,7 +63,7 @@ const ENV = process.env.NODE_ENV; password: configService.get('database.password'), database: configService.get('database.name'), autoLoadEntities: configService.get('database.autoLoad'), - synchronize: configService.get('database.synchronize'), + synchronize: false, }), }), diff --git a/src/config/database.config.ts b/src/config/database.config.ts index 580d71f..9f25843 100644 --- a/src/config/database.config.ts +++ b/src/config/database.config.ts @@ -7,5 +7,4 @@ export default registerAs('database', () => ({ password: process.env.DATABASE_PASS, name: process.env.DATABASE_NAME, autoLoad: process.env.DATABASE_AUTOLOAD === 'true' ? true : false, - synchronize: process.env.DATABASE_SYNC === 'true' ? true : false, })); diff --git a/src/config/environment.validation.ts b/src/config/environment.validation.ts index 890d35e..9806bb9 100644 --- a/src/config/environment.validation.ts +++ b/src/config/environment.validation.ts @@ -10,7 +10,6 @@ export default Joi.object({ DATABASE_PASS: Joi.string().required(), DATABASE_NAME: Joi.string().required(), DATABASE_AUTOLOAD: Joi.boolean().default('true'), - DATABASE_SYNC: Joi.boolean().default('true'), JWT_ACCESS_SECRET: Joi.string().required(), JWT_ACCESS_TTL: Joi.number().required(), JWT_REFRESH_SECRET: Joi.string().required(), diff --git a/src/database/data-source.ts b/src/database/data-source.ts index dd39f7f..5ddefff 100644 --- a/src/database/data-source.ts +++ b/src/database/data-source.ts @@ -12,4 +12,9 @@ export default new DataSource({ host: process.env.DATABASE_HOST, port: Number(process.env.DATABASE_PORT), username: process.env.DATABASE_USER, + password: process.env.DATABASE_PASS, + database: process.env.DATABASE_NAME, + synchronize: false, + entities: ['dist/**/*.entity.js'], + migrations: ['dist/migrations/*.js'], }); diff --git a/src/migrations/1783489241015-InitialSchema.ts b/src/migrations/1783489241015-InitialSchema.ts new file mode 100644 index 0000000..1399ab5 --- /dev/null +++ b/src/migrations/1783489241015-InitialSchema.ts @@ -0,0 +1,82 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class InitialSchema1783489241015 implements MigrationInterface { + name = 'InitialSchema1783489241015' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TYPE "public"."user_role_enum" AS ENUM('superuser', 'admin', 'user')`); + await queryRunner.query(`CREATE TABLE "user" ("id" SERIAL NOT NULL, "firstName" character varying(96), "lastName" character varying(96), "phone" character varying(11) NOT NULL, "password" character varying(96), "role" "public"."user_role_enum" NOT NULL DEFAULT 'user', "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "UQ_8e1f623798118e629b46a9e6299" UNIQUE ("phone"), CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE UNIQUE INDEX "IDX_8e1f623798118e629b46a9e629" ON "user" ("phone") `); + await queryRunner.query(`CREATE TABLE "choice" ("id" SERIAL NOT NULL, "text" character varying(255) NOT NULL, "value" integer NOT NULL, "order" integer NOT NULL, "score" integer NOT NULL, "questionId" integer, CONSTRAINT "PK_5bf2e5939332f46711278a87fcd" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE UNIQUE INDEX "UNQ_QUESTION_ORDER_IDX" ON "choice" ("questionId", "order") `); + await queryRunner.query(`CREATE TABLE "question_range_rule" ("id" SERIAL NOT NULL, "min" integer NOT NULL, "max" integer NOT NULL, "score" integer NOT NULL, "questionId" integer, CONSTRAINT "PK_801c56fb79cb878310f4706616e" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TYPE "public"."question_type_enum" AS ENUM('multi-choice', 'numeric', 'text')`); + await queryRunner.query(`CREATE TABLE "question" ("id" SERIAL NOT NULL, "type" "public"."question_type_enum" NOT NULL DEFAULT 'multi-choice', "content" text NOT NULL, "order" integer NOT NULL, "weight" integer NOT NULL DEFAULT '1', "required" boolean NOT NULL DEFAULT true, "metadata" jsonb, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "testId" integer, CONSTRAINT "PK_21e5786aa0ea704ae185a79b2d5" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE INDEX "IDX_275b917b91392b9a2d6ab7ccee" ON "question" ("order") `); + await queryRunner.query(`CREATE UNIQUE INDEX "UNQ_TEST_ORDER_IDX" ON "question" ("testId", "order") `); + await queryRunner.query(`CREATE TABLE "result_range" ("id" SERIAL NOT NULL, "minScore" integer NOT NULL, "maxScore" integer NOT NULL, "title" character varying(255) NOT NULL, "description" text NOT NULL, "testId" integer, CONSTRAINT "PK_47825aaf8cbc6dee89983104b2c" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TYPE "public"."test_accesstype_enum" AS ENUM('free', 'login', 'paid')`); + await queryRunner.query(`CREATE TABLE "test" ("id" SERIAL NOT NULL, "title" character varying(255) NOT NULL, "description" text NOT NULL, "accessType" "public"."test_accesstype_enum" NOT NULL DEFAULT 'login', "price" bigint, "isActive" boolean NOT NULL DEFAULT false, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "UQ_2be572eb4330bf8387466d8e052" UNIQUE ("title"), CONSTRAINT "CHK_a1e32908ffafc3f21493debfdf" CHECK ( + ("price" IS NULL AND "accessType" <> 'paid') + OR + ("price" IS NOT NULL AND "accessType" = 'paid') +), CONSTRAINT "PK_5417af0062cf987495b611b59c7" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TABLE "submission_answer" ("id" SERIAL NOT NULL, "numericValue" integer, "textValue" character varying(255), "submissionId" integer, "questionId" integer, "choiceId" integer, CONSTRAINT "PK_1692baefb1f7b14f4a2683c66f4" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE UNIQUE INDEX "IDX_ea78aac2e76e03f1c2fc720f1d" ON "submission_answer" ("submissionId", "questionId") `); + await queryRunner.query(`CREATE TABLE "submission_result" ("id" SERIAL NOT NULL, "score" integer NOT NULL, "probability" integer NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "submissionId" integer, "resultRangeId" integer, CONSTRAINT "REL_3ff0186775dd04fd7a209b6dca" UNIQUE ("submissionId"), CONSTRAINT "CHK_f4b6a23dcc9301306827573971" CHECK ("probability" >= 0 AND "probability" <= 100), CONSTRAINT "PK_965bd40b9ba83db22c13f56ce81" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TYPE "public"."submission_status_enum" AS ENUM('in-progress', 'completed')`); + await queryRunner.query(`CREATE TABLE "submission" ("id" SERIAL NOT NULL, "status" "public"."submission_status_enum" NOT NULL DEFAULT 'in-progress', "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "userId" integer, "testId" integer, CONSTRAINT "PK_7faa571d0e4a7076e85890c9bd0" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE TABLE "otp" ("id" SERIAL NOT NULL, "phone" character varying(11) NOT NULL, "code" character varying NOT NULL, "createdAt" TIMESTAMP NOT NULL DEFAULT now(), "expiresAt" TIMESTAMP WITH TIME ZONE NOT NULL, "used" boolean NOT NULL DEFAULT false, CONSTRAINT "PK_32556d9d7b22031d7d0e1fd6723" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE INDEX "IDX_27c8907d477176af7ed2902f2a" ON "otp" ("phone") `); + await queryRunner.query(`CREATE INDEX "IDX_844706d729b144ed62f482ac2b" ON "otp" ("expiresAt") `); + await queryRunner.query(`CREATE INDEX "IDX_2d614f28549e887b905c330dc6" ON "otp" ("phone", "used") `); + await queryRunner.query(`ALTER TABLE "choice" ADD CONSTRAINT "FK_5cc8f4862acc8b7b956be1be204" FOREIGN KEY ("questionId") REFERENCES "question"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "question_range_rule" ADD CONSTRAINT "FK_d7c902bf4e3c214ad80cb9b6e8a" FOREIGN KEY ("questionId") REFERENCES "question"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "question" ADD CONSTRAINT "FK_2c8f911efa2fb5b0fe1abe92020" FOREIGN KEY ("testId") REFERENCES "test"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "result_range" ADD CONSTRAINT "FK_af2d7b64263e469098d234ce3d0" FOREIGN KEY ("testId") REFERENCES "test"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission_answer" ADD CONSTRAINT "FK_d52d9c3efb0e1aa66d2e27155ac" FOREIGN KEY ("submissionId") REFERENCES "submission"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission_answer" ADD CONSTRAINT "FK_1da4a54a983fd01227d9fad225c" FOREIGN KEY ("questionId") REFERENCES "question"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission_answer" ADD CONSTRAINT "FK_0e7d9c5bf8fc8952fbf3842fa6c" FOREIGN KEY ("choiceId") REFERENCES "choice"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission_result" ADD CONSTRAINT "FK_3ff0186775dd04fd7a209b6dca9" FOREIGN KEY ("submissionId") REFERENCES "submission"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission_result" ADD CONSTRAINT "FK_4f342dd432cc10d2c958d99be88" FOREIGN KEY ("resultRangeId") REFERENCES "result_range"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission" ADD CONSTRAINT "FK_7bd626272858ef6464aa2579094" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "submission" ADD CONSTRAINT "FK_53685d02371670b4495ff181aec" FOREIGN KEY ("testId") REFERENCES "test"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "submission" DROP CONSTRAINT "FK_53685d02371670b4495ff181aec"`); + await queryRunner.query(`ALTER TABLE "submission" DROP CONSTRAINT "FK_7bd626272858ef6464aa2579094"`); + await queryRunner.query(`ALTER TABLE "submission_result" DROP CONSTRAINT "FK_4f342dd432cc10d2c958d99be88"`); + await queryRunner.query(`ALTER TABLE "submission_result" DROP CONSTRAINT "FK_3ff0186775dd04fd7a209b6dca9"`); + await queryRunner.query(`ALTER TABLE "submission_answer" DROP CONSTRAINT "FK_0e7d9c5bf8fc8952fbf3842fa6c"`); + await queryRunner.query(`ALTER TABLE "submission_answer" DROP CONSTRAINT "FK_1da4a54a983fd01227d9fad225c"`); + await queryRunner.query(`ALTER TABLE "submission_answer" DROP CONSTRAINT "FK_d52d9c3efb0e1aa66d2e27155ac"`); + await queryRunner.query(`ALTER TABLE "result_range" DROP CONSTRAINT "FK_af2d7b64263e469098d234ce3d0"`); + await queryRunner.query(`ALTER TABLE "question" DROP CONSTRAINT "FK_2c8f911efa2fb5b0fe1abe92020"`); + await queryRunner.query(`ALTER TABLE "question_range_rule" DROP CONSTRAINT "FK_d7c902bf4e3c214ad80cb9b6e8a"`); + await queryRunner.query(`ALTER TABLE "choice" DROP CONSTRAINT "FK_5cc8f4862acc8b7b956be1be204"`); + await queryRunner.query(`DROP INDEX "public"."IDX_2d614f28549e887b905c330dc6"`); + await queryRunner.query(`DROP INDEX "public"."IDX_844706d729b144ed62f482ac2b"`); + await queryRunner.query(`DROP INDEX "public"."IDX_27c8907d477176af7ed2902f2a"`); + await queryRunner.query(`DROP TABLE "otp"`); + await queryRunner.query(`DROP TABLE "submission"`); + await queryRunner.query(`DROP TYPE "public"."submission_status_enum"`); + await queryRunner.query(`DROP TABLE "submission_result"`); + await queryRunner.query(`DROP INDEX "public"."IDX_ea78aac2e76e03f1c2fc720f1d"`); + await queryRunner.query(`DROP TABLE "submission_answer"`); + await queryRunner.query(`DROP TABLE "test"`); + await queryRunner.query(`DROP TYPE "public"."test_accesstype_enum"`); + await queryRunner.query(`DROP TABLE "result_range"`); + await queryRunner.query(`DROP INDEX "public"."UNQ_TEST_ORDER_IDX"`); + await queryRunner.query(`DROP INDEX "public"."IDX_275b917b91392b9a2d6ab7ccee"`); + await queryRunner.query(`DROP TABLE "question"`); + await queryRunner.query(`DROP TYPE "public"."question_type_enum"`); + await queryRunner.query(`DROP TABLE "question_range_rule"`); + await queryRunner.query(`DROP INDEX "public"."UNQ_QUESTION_ORDER_IDX"`); + await queryRunner.query(`DROP TABLE "choice"`); + await queryRunner.query(`DROP INDEX "public"."IDX_8e1f623798118e629b46a9e629"`); + await queryRunner.query(`DROP TABLE "user"`); + await queryRunner.query(`DROP TYPE "public"."user_role_enum"`); + } + +}