diff --git a/backend/package.json b/backend/package.json index 41963b6..489deab 100644 --- a/backend/package.json +++ b/backend/package.json @@ -19,7 +19,11 @@ "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", - "clean": "rm -rf dist node_modules" + "clean": "rm -rf dist node_modules", + "typeorm": "ts-node -r tsconfig-paths/register ./node_modules/typeorm/cli.js -d src/data-source.ts", + "migration:generate": "npm run typeorm -- migration:generate", + "migration:run": "npm run typeorm -- migration:run", + "migration:revert": "npm run typeorm -- migration:revert" }, "dependencies": { "@nestjs/common": "^11.0.1", @@ -30,6 +34,7 @@ "@nestjs/typeorm": "^11.0.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.3", + "dotenv": "^16.4.7", "pg": "^8.16.3", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1", diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index f72cd04..5e09a63 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -21,7 +21,9 @@ import { IdeasModule } from './ideas/ideas.module'; password: configService.get('DB_PASSWORD', 'teamplanner'), database: configService.get('DB_DATABASE', 'teamplanner'), entities: [__dirname + '/**/*.entity{.ts,.js}'], - synchronize: true, // Only for development! + migrations: [__dirname + '/migrations/*{.ts,.js}'], + migrationsRun: true, + synchronize: false, }), }), IdeasModule, diff --git a/backend/src/data-source.ts b/backend/src/data-source.ts new file mode 100644 index 0000000..fb63a23 --- /dev/null +++ b/backend/src/data-source.ts @@ -0,0 +1,16 @@ +import { DataSource } from 'typeorm'; +import { config } from 'dotenv'; + +config(); + +export const AppDataSource = new DataSource({ + type: 'postgres', + host: process.env.DB_HOST ?? 'localhost', + port: parseInt(process.env.DB_PORT ?? '5432', 10), + username: process.env.DB_USERNAME ?? 'teamplanner', + password: process.env.DB_PASSWORD ?? 'teamplanner', + database: process.env.DB_DATABASE ?? 'teamplanner', + entities: [__dirname + '/**/*.entity{.ts,.js}'], + migrations: [__dirname + '/migrations/*{.ts,.js}'], + synchronize: false, +}); diff --git a/backend/src/migrations/1735660800000-InitialMigration.ts b/backend/src/migrations/1735660800000-InitialMigration.ts new file mode 100644 index 0000000..0b1b6df --- /dev/null +++ b/backend/src/migrations/1735660800000-InitialMigration.ts @@ -0,0 +1,39 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class InitialMigration1735660800000 implements MigrationInterface { + name = 'InitialMigration1735660800000'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TYPE "public"."ideas_status_enum" AS ENUM('backlog', 'todo', 'in_progress', 'done', 'cancelled')`, + ); + await queryRunner.query( + `CREATE TYPE "public"."ideas_priority_enum" AS ENUM('low', 'medium', 'high', 'critical')`, + ); + await queryRunner.query(` + CREATE TABLE "ideas" ( + "id" uuid NOT NULL DEFAULT uuid_generate_v4(), + "title" character varying NOT NULL, + "description" text, + "status" "public"."ideas_status_enum" NOT NULL DEFAULT 'backlog', + "priority" "public"."ideas_priority_enum" NOT NULL DEFAULT 'medium', + "module" character varying(100), + "target_audience" character varying(255), + "pain" text, + "ai_role" text, + "verification_method" text, + "color" character varying(20), + "order" integer NOT NULL DEFAULT '0', + "created_at" TIMESTAMP NOT NULL DEFAULT now(), + "updated_at" TIMESTAMP NOT NULL DEFAULT now(), + CONSTRAINT "PK_ideas_id" PRIMARY KEY ("id") + ) + `); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`DROP TABLE "ideas"`); + await queryRunner.query(`DROP TYPE "public"."ideas_priority_enum"`); + await queryRunner.query(`DROP TYPE "public"."ideas_status_enum"`); + } +} diff --git a/package-lock.json b/package-lock.json index 0422ad7..bd8d355 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,7 @@ "@nestjs/typeorm": "^11.0.0", "class-transformer": "^0.5.1", "class-validator": "^0.14.3", + "dotenv": "^16.4.7", "pg": "^8.16.3", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.1",