Explicitly set workspaceId column as uuid type to ease pg LEFT JOIN (#15430)

Pg scan was redundant because historically the workspaceId was
`varchar`, even though below migration won't change that we had a look
to workspaceId col declaration across the codebase
This commit is contained in:
Paul Rastoin
2025-10-29 19:08:44 +01:00
committed by GitHub
parent 33545a072c
commit f6f52d676f
14 changed files with 88 additions and 15 deletions
@@ -0,0 +1,72 @@
import { type MigrationInterface, type QueryRunner } from 'typeorm';
export class WorkspaceIdUuidNotNullable1761749599736
implements MigrationInterface
{
name = 'WorkspaceIdUuidNotNullable1761749599736';
public async up(queryRunner: QueryRunner): Promise<void> {
// Delete orphaned rows without workspaceId before migration
await queryRunner.query(
`DELETE FROM "core"."indexMetadata" WHERE "workspaceId" IS NULL`,
);
await queryRunner.query(
`DROP INDEX "core"."IDX_INDEX_METADATA_WORKSPACE_ID_OBJECT_METADATA_ID"`,
);
await queryRunner.query(
`DROP INDEX "core"."IDX_b27c681286ac581f81498c5d4b"`,
);
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" DROP CONSTRAINT "IDX_INDEX_METADATA_NAME_WORKSPACE_ID_OBJECT_METADATA_ID_UNIQUE"`,
);
// Convert column type from varchar to uuid while preserving data
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ALTER COLUMN "workspaceId" TYPE uuid USING "workspaceId"::uuid`,
);
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ALTER COLUMN "workspaceId" SET NOT NULL`,
);
await queryRunner.query(
`CREATE UNIQUE INDEX "IDX_b27c681286ac581f81498c5d4b" ON "core"."indexMetadata" ("workspaceId", "universalIdentifier") `,
);
await queryRunner.query(
`CREATE INDEX "IDX_INDEX_METADATA_WORKSPACE_ID_OBJECT_METADATA_ID" ON "core"."indexMetadata" ("workspaceId", "objectMetadataId") `,
);
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ADD CONSTRAINT "IDX_INDEX_METADATA_NAME_WORKSPACE_ID_OBJECT_METADATA_ID_UNIQUE" UNIQUE ("name", "workspaceId", "objectMetadataId")`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" DROP CONSTRAINT "IDX_INDEX_METADATA_NAME_WORKSPACE_ID_OBJECT_METADATA_ID_UNIQUE"`,
);
await queryRunner.query(
`DROP INDEX "core"."IDX_INDEX_METADATA_WORKSPACE_ID_OBJECT_METADATA_ID"`,
);
await queryRunner.query(
`DROP INDEX "core"."IDX_b27c681286ac581f81498c5d4b"`,
);
// Convert column type from uuid back to varchar while preserving data
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ALTER COLUMN "workspaceId" DROP NOT NULL`,
);
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ALTER COLUMN "workspaceId" TYPE character varying USING "workspaceId"::character varying`,
);
await queryRunner.query(
`ALTER TABLE "core"."indexMetadata" ADD CONSTRAINT "IDX_INDEX_METADATA_NAME_WORKSPACE_ID_OBJECT_METADATA_ID_UNIQUE" UNIQUE ("name", "workspaceId", "objectMetadataId")`,
);
await queryRunner.query(
`CREATE UNIQUE INDEX "IDX_b27c681286ac581f81498c5d4b" ON "core"."indexMetadata" ("workspaceId", "universalIdentifier") `,
);
await queryRunner.query(
`CREATE INDEX "IDX_INDEX_METADATA_WORKSPACE_ID_OBJECT_METADATA_ID" ON "core"."indexMetadata" ("workspaceId", "objectMetadataId") `,
);
}
}
@@ -37,7 +37,7 @@ export class ApiKeyEntity {
revokedAt?: Date | null;
@Field(() => UUIDScalarType)
@Column('uuid')
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@Field(() => Date)
@@ -52,8 +52,8 @@ export class AppTokenEntity {
@JoinColumn({ name: 'workspaceId' })
workspace: Relation<WorkspaceEntity>;
@Column({ nullable: true })
workspaceId: string;
@Column({ nullable: true, type: 'uuid' })
workspaceId: string | null;
@Field()
@Column({ nullable: false, type: 'text', default: AppTokenType.RefreshToken })
@@ -37,7 +37,7 @@ export class ApprovedAccessDomainEntity {
@Column({ type: 'boolean', default: false, nullable: false })
isValidated: boolean;
@Column()
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@ManyToOne(
@@ -28,9 +28,9 @@ import {
compareHash,
hashPassword,
} from 'src/engine/core-modules/auth/auth.util';
import { type AuthTokens } from 'src/engine/core-modules/auth/dto/auth-tokens.dto';
import { type AuthorizeAppOutput } from 'src/engine/core-modules/auth/dto/authorize-app.dto';
import { type AuthorizeAppInput } from 'src/engine/core-modules/auth/dto/authorize-app.input';
import { type AuthTokens } from 'src/engine/core-modules/auth/dto/auth-tokens.dto';
import { type UpdatePasswordOutput } from 'src/engine/core-modules/auth/dto/update-password.dto';
import { type UserCredentialsInput } from 'src/engine/core-modules/auth/dto/user-credentials.input';
import { type CheckUserExistOutput } from 'src/engine/core-modules/auth/dto/user-exists.dto';
@@ -65,7 +65,8 @@ export class RenewTokenService {
const accessToken =
isDefined(authProvider) &&
targetedTokenType === JwtTokenTypeEnum.WORKSPACE_AGNOSTIC
targetedTokenType === JwtTokenTypeEnum.WORKSPACE_AGNOSTIC &&
!isDefined(workspaceId)
? await this.workspaceAgnosticTokenService.generateWorkspaceAgnosticToken(
{
userId: user.id,
@@ -74,7 +75,7 @@ export class RenewTokenService {
)
: await this.accessTokenService.generateAccessToken({
userId: user.id,
workspaceId,
workspaceId: workspaceId as string,
authProvider: resolvedAuthProvider,
isImpersonating,
impersonatorUserWorkspaceId,
@@ -60,7 +60,7 @@ export type TransientTokenJwtPayload = CommonPropertiesJwtPayload & {
export type RefreshTokenJwtPayload = CommonPropertiesJwtPayload & {
type: JwtTokenTypeEnum.REFRESH;
workspaceId?: string;
workspaceId?: string | null;
userId: string;
jti?: string;
authProvider?: AuthProviderEnum;
@@ -60,7 +60,7 @@ export class EmailingDomainEntity {
@Column({ type: 'timestamptz', nullable: true })
verifiedAt: Date | null;
@Column({ nullable: false })
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@ManyToOne(() => WorkspaceEntity, (workspace) => workspace.emailingDomains, {
@@ -67,7 +67,7 @@ export class KeyValuePairEntity {
@JoinColumn({ name: 'workspaceId' })
workspace: Relation<WorkspaceEntity>;
@Column({ nullable: true })
@Column({ nullable: true, type: 'uuid' })
workspaceId: string | null;
@Field(() => String)
@@ -72,7 +72,7 @@ export class WorkspaceSSOIdentityProviderEntity {
@JoinColumn({ name: 'workspaceId' })
workspace: Relation<WorkspaceEntity>;
@Column()
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@CreateDateColumn({ type: 'timestamptz' })
@@ -69,7 +69,7 @@ export class UserWorkspaceEntity {
workspace: Relation<WorkspaceEntity>;
@Field(() => UUIDScalarType, { nullable: false })
@Column()
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@Column({ nullable: true })
@@ -42,7 +42,7 @@ export class WebhookEntity {
secret: string;
@Field(() => UUIDScalarType)
@Column('uuid')
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@Field()
@@ -20,7 +20,7 @@ export class AgentChatThreadEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column('uuid')
@Column({ nullable: false, type: 'uuid' })
@Index()
userWorkspaceId: string;
@@ -44,7 +44,7 @@ export class IndexMetadataEntity
@Column({ nullable: false })
name: string;
@Column({ nullable: true })
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;
@Column({ nullable: false, type: 'uuid' })