diff --git a/backend/package-lock.json b/backend/package-lock.json index ebf1fa7..5fdb22b 100644 --- a/backend/package-lock.json +++ b/backend/package-lock.json @@ -22,7 +22,6 @@ "pg": "^8.16.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.2", - "typeorm": "^0.3.24", "uid": "^2.0.2" }, "devDependencies": { @@ -31,6 +30,7 @@ "rimraf": "^6.0.1", "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", + "typeorm": "^0.3.24", "typescript": "^5.8.3" } }, diff --git a/backend/package.json b/backend/package.json index 1a16f1e..3d0ad6b 100644 --- a/backend/package.json +++ b/backend/package.json @@ -7,8 +7,11 @@ "build": "rimraf dist && tsc", "start": "npm run build && node dist/main.js", "dev": "ts-node -r tsconfig-paths/register src/main.ts", - "typeorm": "typeorm-ts-node-commonjs -d dist/data-source.js", - "test": "echo \"Error: no test specified\" && exit 1" + "typeorma": "typeorm-ts-node-commonjs -d dist/data-source.js", + "test": "echo \"Error: no test specified\" && exit 1", + "typeorm": "ts-node -r ts-node/register ./node_modules/typeorm/cli.js", + "migration:generate": "npm run typeorm migration:generate -d dist/data-source.js", + "migration:run": "npm run typeorm migration:run -- --dataSource dist/data-source.js" }, "keywords": [], "author": "", @@ -27,7 +30,6 @@ "pg": "^8.16.0", "reflect-metadata": "^0.2.2", "rxjs": "^7.8.2", - "typeorm": "^0.3.24", "uid": "^2.0.2" }, "devDependencies": { @@ -36,6 +38,7 @@ "rimraf": "^6.0.1", "ts-node": "^10.9.2", "tsconfig-paths": "^4.2.0", + "typeorm": "^0.3.24", "typescript": "^5.8.3" } } diff --git a/backend/src/migrations/1690_add_foreign_keys.ts b/backend/src/migrations/1690_add_foreign_keys.ts new file mode 100644 index 0000000..e1c5988 --- /dev/null +++ b/backend/src/migrations/1690_add_foreign_keys.ts @@ -0,0 +1,57 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddForeignKeys1690 implements MigrationInterface { + async up(queryRunner: QueryRunner): Promise { + // 1. User与Workspace的关联 + await queryRunner.query(` + ALTER TABLE "user" + ADD CONSTRAINT "fk_user_workspace" + FOREIGN KEY ("workspaceId") + REFERENCES "workspace"("id") + ON DELETE CASCADE + `); + + // 2. AiConfig与Workspace的关联 + await queryRunner.query(` + ALTER TABLE "ai_config" + ADD CONSTRAINT "fk_ai_config_workspace" + FOREIGN KEY ("workspaceId") + REFERENCES "workspace"("id") + ON DELETE CASCADE + `); + + // 3. Project与Workspace的关联 + await queryRunner.query(` + ALTER TABLE "project" + ADD CONSTRAINT "fk_project_workspace" + FOREIGN KEY ("workspaceId") + REFERENCES "workspace"("id") + ON DELETE CASCADE + `); + + // 4. PluginConfig与Workspace的关联 + await queryRunner.query(` + ALTER TABLE "plugin_config" + ADD CONSTRAINT "fk_plugin_config_workspace" + FOREIGN KEY ("workspaceId") + REFERENCES "workspace"("id") + ON DELETE CASCADE + `); + } + + async down(queryRunner: QueryRunner): Promise { + // 删除外键约束 + const foreignKeys = [ + 'fk_user_workspace', + 'fk_ai_config_workspace', + 'fk_project_workspace', + 'fk_plugin_config_workspace' + ]; + + for (const fk of foreignKeys) { + await queryRunner.query( + `ALTER TABLE ${fk.split('_').slice(2).join('_')} DROP CONSTRAINT ${fk}` + ); + } + } +} \ No newline at end of file