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:
@ -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"`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,6 +3,7 @@ import { Field, ObjectType, registerEnumType } from '@nestjs/graphql';
|
|||||||
import { IDField } from '@ptc-org/nestjs-query-graphql';
|
import { IDField } from '@ptc-org/nestjs-query-graphql';
|
||||||
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
|
import { WorkspaceActivationStatus } from 'twenty-shared/workspace';
|
||||||
import {
|
import {
|
||||||
|
Check,
|
||||||
Column,
|
Column,
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
DeleteDateColumn,
|
DeleteDateColumn,
|
||||||
@ -27,6 +28,10 @@ registerEnumType(WorkspaceActivationStatus, {
|
|||||||
name: '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' })
|
@Entity({ name: 'workspace', schema: 'core' })
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class Workspace {
|
export class Workspace {
|
||||||
|
|||||||
Reference in New Issue
Block a user