From 4e329d08b0b745097604da36b3cf2ddd945d6521 Mon Sep 17 00:00:00 2001 From: Antoine Moreaux Date: Mon, 16 Dec 2024 19:41:18 +0100 Subject: [PATCH] feat(database): add unique constraint on workspace subdomain (#9084) Added a unique constraint to the "subdomain" column in the workspace entity to ensure no duplicate subdomains exist in the database. Included a TypeORM migration script to enforce this change at the database level. --- ...355945585-add-unique-index-on-subdomain.ts | 19 +++++++++++++++++++ .../workspace/workspace.entity.ts | 2 +- 2 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 packages/twenty-server/src/database/typeorm/core/migrations/common/1734355945585-add-unique-index-on-subdomain.ts diff --git a/packages/twenty-server/src/database/typeorm/core/migrations/common/1734355945585-add-unique-index-on-subdomain.ts b/packages/twenty-server/src/database/typeorm/core/migrations/common/1734355945585-add-unique-index-on-subdomain.ts new file mode 100644 index 000000000..c44b731fe --- /dev/null +++ b/packages/twenty-server/src/database/typeorm/core/migrations/common/1734355945585-add-unique-index-on-subdomain.ts @@ -0,0 +1,19 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddUniqueIndexOnSubdomain1734355945585 + implements MigrationInterface +{ + name = 'AddUniqueIndexOnSubdomain1734355945585'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "core"."workspace" ADD CONSTRAINT "UQ_cba6255a24deb1fff07dd7351b8" UNIQUE ("subdomain")`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `ALTER TABLE "core"."workspace" DROP CONSTRAINT "UQ_cba6255a24deb1fff07dd7351b8"`, + ); + } +} diff --git a/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts b/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts index b80ecad5e..5f8e89af3 100644 --- a/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts +++ b/packages/twenty-server/src/engine/core-modules/workspace/workspace.entity.ts @@ -144,7 +144,7 @@ export class Workspace { databaseSchema: string; @Field() - @Column() + @Column({ unique: true }) subdomain: string; @Field()