Add workspace constraint on defaultRoleId and activationStatus (#11191)

Part of https://github.com/twentyhq/core-team-issues/issues/526

An active workspace's defaultRoleId should never be null.
We can't rely on a simple postgres NOT NULL constraint as defaultRoleId
will always be initially null when the workspace is first created since
the roles do not exist at that time.
Since a suspended workspace can be active again, we should maintain that
rule during the workspace suspension. The only moment defaultRoleId can
have a null value is during the onboarding. this pr enforces that
This commit is contained in:
Marie
2025-03-26 15:43:13 +01:00
committed by GitHub
parent fe3dd40034
commit 4827ad600d
2 changed files with 22 additions and 0 deletions

View File

@ -0,0 +1,17 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class AddWorkspaceConstraint1742998832316 implements MigrationInterface {
name = 'AddWorkspaceConstraint1742998832316';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "core"."workspace" ADD CONSTRAINT "onboarded_workspace_requires_default_role" CHECK ("activationStatus" IN ('PENDING_CREATION', 'ONGOING_CREATION') OR "defaultRoleId" IS NOT NULL)`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "core"."workspace" DROP CONSTRAINT "onboarded_workspace_requires_default_role"`,
);
}
}

View File

@ -3,6 +3,7 @@ import { Field, ObjectType, registerEnumType } from '@nestjs/graphql';
import { IDField } from '@ptc-org/nestjs-query-graphql';
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
import {
Check,
Column,
CreateDateColumn,
DeleteDateColumn,
@ -27,6 +28,10 @@ registerEnumType(WorkspaceActivationStatus, {
name: 'WorkspaceActivationStatus',
});
@Check(
'onboarded_workspace_requires_default_role',
`"activationStatus" IN ('PENDING_CREATION', 'ONGOING_CREATION') OR "defaultRoleId" IS NOT NULL`,
)
@Entity({ name: 'workspace', schema: 'core' })
@ObjectType()
export class Workspace {