Skip to content

The open alternative to Salesforce, designed for AI.

feat(database): add unique constraint on workspace subdomain (#9084)

Antoine Moreaux4e329d08b0b7
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 00000000000..c44b731fe1c
--- /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<void> {
+ await queryRunner.query(
+ `ALTER TABLE "core"."workspace" ADD CONSTRAINT "UQ_cba6255a24deb1fff07dd7351b8" UNIQUE ("subdomain")`,
+ );
+ }
+
+ public async down(queryRunner: QueryRunner): Promise<void> {
+ 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 b80ecad5ecc..5f8e89af34c 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()