## What When the same PR introduces a new core entity *and* adds a cache provider that queries it, every workspace step from older versions that runs before the introducing instance step hits `relation … does not exist` — the cause of the failed [v2.6.0 staging-ci run](https://github.com/twentyhq/twenty-infra/actions/runs/26042742000). Same class of failure for renamed core entities and for new FK columns hidden inside relation loads. This PR adds **upgrade-aware entity decorators** + a runtime that adapts TypeORM's view of the schema to the current `core.upgradeMigration` cursor. ## Strategy ``` ┌────────────────────────────────┐ │ @Entity classes (final shape) │ │ + @WasIntroducedInUpgrade │ │ + @WasRenamedInUpgrade │ └───────────────┬────────────────┘ │ UpgradeSequenceRunner.run() ┌─────────────────────┴─────────────────────┐ ▼ ▼ step N+1 begins step N just completed │ │ └────────► adapter.refresh() ◄──────────────┘ │ reads core.upgradeMigration via UpgradeMigrationService.getLastAttemptedInstanceCommand │ ▼ ┌────────────────────────────────────────────────────────┐ │ UpgradeAwareEntityMetadataAdapter │ │ • mutates EntityMetadata.tableName / tablePath │ │ -> historical name for renames not yet applied │ │ • flips column.isSelect = false for not-yet-introduced│ │ columns │ │ • tracks per-entity availability sidecar │ └─────────────────┬──────────────────────────────────────┘ │ ▼ DataSource.getRepository wrapped at TypeOrmModule.forRoot: repo.find() / findOne() / count() / … ┌─────────────────────────────────────────┐ │ wrapRepositoryWithUpgradeAwareProxy │ │ • entity unavailable -> short-circuit │ │ (find -> [], count -> 0, │ │ findOneOrFail -> EntityNotFound) │ │ • write -> Promise.reject( │ │ UpgradeUnavailableEntityWriteEx) │ │ • find({ relations: ['X'] }) with X │ │ unavailable -> X stripped │ └─────────────────────────────────────────┘ ``` The decorator strings reference real `core.upgradeMigration.name` values (`${version}_${className}_${timestamp}`). A boot-time validator walks the actual `UpgradeSequenceReaderService.getUpgradeSequence()` and fails fast on typos. ## Files - New decorators: `engine/core-modules/upgrade/decorators/was-introduced-in-upgrade.decorator.ts`, `was-renamed-in-upgrade.decorator.ts` - Runtime: `engine/twenty-orm/upgrade-aware/` (adapter, proxy, install hook, state singleton, exceptions) - Wired into `UpgradeSequenceRunnerService` (`refresh()` between steps) and `TypeOrmModule.forRoot` (proxy install) - 2-6 entity decorations: `RolePermissionFlagEntity` (rename history + new `permissionFlagId` column), `PermissionFlagEntity` (new catalog) ## Validation End-to-end local cross-version upgrade (v1.22 → HEAD): `28 workspace(s) succeeded, 0 failed`; `upgrade:status → Instance: Up to date, 4 up to date, 0 behind, 0 failed`. Full log excerpts and the second-failure-found-and-fixed (`WorkspaceRolesPermissionsCacheService` relation load) in [this comment](https://github.com/twentyhq/twenty/pull/20686#issuecomment-4480036816). ## Test plan - [x] Adapter spec covers rename mutation; proxy spec covers `find()` short-circuit on unavailable entity. Resolver + validator + decorators are covered by `resolve-entity-shape-at-upgrade-cursor.util.spec.ts` (integration-level via real decorator application). - [x] `nx lint:diff-with-main twenty-server` + `nx typecheck twenty-server` clean - [x] All 82 affected tests passing - [ ] Cross-version-upgrade CI re-runs after this lands; v2.6.0 retag once green ## Follow-ups deferred - v2.7 `connectionProvider` rename repro as a permanent end-to-end test artifact - Extending the proxy to also cover `EntityManager.getRepository` and `createQueryBuilder` if a non-`find()` upgrade-time consumer surfaces
347 lines
10 KiB
TypeScript
347 lines
10 KiB
TypeScript
import { Test, type TestingModule } from '@nestjs/testing';
|
|
import { getDataSourceToken, getRepositoryToken } from '@nestjs/typeorm';
|
|
|
|
import { config } from 'dotenv';
|
|
import { DataSource, type Repository } from 'typeorm';
|
|
|
|
import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
|
|
import { TwentyConfigService } from 'src/engine/core-modules/twenty-config/twenty-config.service';
|
|
import { InstanceCommandRunnerService } from 'src/engine/core-modules/upgrade/services/instance-command-runner.service';
|
|
import { UpgradeMigrationService } from 'src/engine/core-modules/upgrade/services/upgrade-migration.service';
|
|
import {
|
|
UpgradeSequenceReaderService,
|
|
type UpgradeStep,
|
|
type WorkspaceUpgradeStep,
|
|
} from 'src/engine/core-modules/upgrade/services/upgrade-sequence-reader.service';
|
|
import { UpgradeSequenceRunnerService } from 'src/engine/core-modules/upgrade/services/upgrade-sequence-runner.service';
|
|
import { UpgradeAwareEntityMetadataAdapter } from 'src/engine/twenty-orm/upgrade-aware/upgrade-aware-entity-metadata.adapter';
|
|
import { UpgradeStatusService } from 'src/engine/core-modules/upgrade/services/upgrade-status.service';
|
|
import { WorkspaceCommandRunnerService } from 'src/engine/core-modules/upgrade/services/workspace-command-runner.service';
|
|
import { UpgradeMigrationEntity } from 'src/engine/core-modules/upgrade/upgrade-migration.entity';
|
|
import {
|
|
SEED_APPLE_WORKSPACE_ID,
|
|
SEED_EMPTY_WORKSPACE_3_ID,
|
|
SEED_EMPTY_WORKSPACE_4_ID,
|
|
SEED_YCOMBINATOR_WORKSPACE_ID,
|
|
} from 'src/engine/workspace-manager/dev-seeder/core/constants/seeder-workspaces.constant';
|
|
import { WorkspaceVersionService } from 'src/engine/workspace-manager/workspace-version/services/workspace-version.service';
|
|
|
|
jest.useRealTimers();
|
|
|
|
config({
|
|
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
|
|
override: true,
|
|
});
|
|
|
|
export const WS_1 = SEED_APPLE_WORKSPACE_ID;
|
|
export const WS_2 = SEED_YCOMBINATOR_WORKSPACE_ID;
|
|
export const WS_3 = SEED_EMPTY_WORKSPACE_3_ID;
|
|
export const WS_4 = SEED_EMPTY_WORKSPACE_4_ID;
|
|
|
|
const EXECUTED_BY_VERSION = '42.42.42';
|
|
|
|
const noopAsync = async () => {};
|
|
|
|
export const makeStep = (
|
|
kind: UpgradeStep['kind'],
|
|
name: string,
|
|
): UpgradeStep => {
|
|
const command =
|
|
kind === 'workspace'
|
|
? { runOnWorkspace: noopAsync }
|
|
: kind === 'slow-instance'
|
|
? { up: noopAsync, down: noopAsync, runDataMigration: noopAsync }
|
|
: { up: noopAsync, down: noopAsync };
|
|
|
|
return {
|
|
kind,
|
|
name,
|
|
command,
|
|
version: '1.21.0',
|
|
timestamp: 0,
|
|
} as unknown as UpgradeStep;
|
|
};
|
|
|
|
export const makeFastInstance = (name: string) =>
|
|
makeStep('fast-instance', name);
|
|
|
|
export const makeSlowInstance = (name: string) =>
|
|
makeStep('slow-instance', name);
|
|
|
|
export const makeWorkspace = (name: string) =>
|
|
makeStep('workspace', name) as WorkspaceUpgradeStep;
|
|
|
|
let mockActiveWorkspaceIds: string[] = [];
|
|
|
|
export const setMockActiveWorkspaceIds = (ids: string[]) => {
|
|
mockActiveWorkspaceIds = ids;
|
|
};
|
|
|
|
export const DEFAULT_OPTIONS = {
|
|
workspaceIds: undefined,
|
|
startFromWorkspaceId: undefined,
|
|
workspaceCountLimit: undefined,
|
|
dryRun: false,
|
|
verbose: false,
|
|
};
|
|
|
|
type IntegrationTestModule = Awaited<
|
|
ReturnType<typeof createUpgradeSequenceRunnerIntegrationTestModule>
|
|
>;
|
|
|
|
export type IntegrationTestContext = {
|
|
[K in keyof IntegrationTestModule]: IntegrationTestModule[K];
|
|
};
|
|
|
|
export const createUpgradeSequenceRunnerIntegrationTestModule = async () => {
|
|
const dataSource = new DataSource({
|
|
type: 'postgres',
|
|
url: process.env.PG_DATABASE_URL,
|
|
schema: 'core',
|
|
entities: [
|
|
'src/engine/core-modules/**/*.entity.ts',
|
|
'src/engine/metadata-modules/**/*.entity.ts',
|
|
],
|
|
synchronize: false,
|
|
});
|
|
|
|
await dataSource.initialize();
|
|
|
|
const migrationRepo: Repository<UpgradeMigrationEntity> =
|
|
dataSource.getRepository(UpgradeMigrationEntity);
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
|
providers: [
|
|
{
|
|
provide: getRepositoryToken(UpgradeMigrationEntity),
|
|
useValue: migrationRepo,
|
|
},
|
|
{
|
|
provide: getDataSourceToken(),
|
|
useValue: dataSource,
|
|
},
|
|
{
|
|
provide: TwentyConfigService,
|
|
useValue: {
|
|
get: (key: string) =>
|
|
key === 'APP_VERSION' ? EXECUTED_BY_VERSION : undefined,
|
|
},
|
|
},
|
|
UpgradeMigrationService,
|
|
{
|
|
provide: WorkspaceVersionService,
|
|
useValue: {
|
|
getActiveOrSuspendedWorkspaceIds: jest
|
|
.fn()
|
|
.mockImplementation(async () => mockActiveWorkspaceIds),
|
|
hasActiveOrSuspendedWorkspaces: jest
|
|
.fn()
|
|
.mockImplementation(async () => mockActiveWorkspaceIds.length > 0),
|
|
},
|
|
},
|
|
{
|
|
provide: UpgradeSequenceReaderService,
|
|
useFactory: () => new UpgradeSequenceReaderService({} as any),
|
|
},
|
|
{
|
|
provide: UpgradeStatusService,
|
|
useValue: {
|
|
invalidateInstanceAndAllWorkspacesStatus: jest
|
|
.fn()
|
|
.mockResolvedValue(undefined),
|
|
},
|
|
},
|
|
InstanceCommandRunnerService,
|
|
WorkspaceCommandRunnerService,
|
|
{
|
|
provide: WorkspaceIteratorService,
|
|
useValue: {
|
|
iterate: jest.fn().mockImplementation(async (args: any) => {
|
|
const { callback, workspaceIds } = args;
|
|
const ids = workspaceIds ?? [WS_1];
|
|
const report = { fail: [] as any[], success: [] as any[] };
|
|
|
|
for (const [index, workspaceId] of ids.entries()) {
|
|
try {
|
|
await callback({
|
|
workspaceId,
|
|
index,
|
|
total: ids.length,
|
|
dataSource,
|
|
});
|
|
report.success.push({ workspaceId });
|
|
} catch (error) {
|
|
report.fail.push({ error, workspaceId });
|
|
}
|
|
}
|
|
|
|
return report;
|
|
}),
|
|
},
|
|
},
|
|
{
|
|
provide: UpgradeAwareEntityMetadataAdapter,
|
|
useValue: {
|
|
refresh: jest.fn().mockResolvedValue(undefined),
|
|
isEntityAvailable: jest.fn().mockReturnValue(true),
|
|
getHiddenColumnPropertyNames: jest.fn().mockReturnValue(new Set()),
|
|
},
|
|
},
|
|
UpgradeSequenceRunnerService,
|
|
],
|
|
}).compile();
|
|
|
|
const runner = module.get(UpgradeSequenceRunnerService);
|
|
|
|
jest.spyOn(runner['logger'], 'log').mockImplementation();
|
|
jest.spyOn(runner['logger'], 'error').mockImplementation();
|
|
jest.spyOn(runner['logger'], 'warn').mockImplementation();
|
|
|
|
const instanceCommandRunnerService = module.get(InstanceCommandRunnerService);
|
|
|
|
jest
|
|
.spyOn(instanceCommandRunnerService['logger'], 'log')
|
|
.mockImplementation();
|
|
jest
|
|
.spyOn(instanceCommandRunnerService['logger'], 'error')
|
|
.mockImplementation();
|
|
|
|
const workspaceCommandRunnerService = module.get(
|
|
WorkspaceCommandRunnerService,
|
|
);
|
|
|
|
jest
|
|
.spyOn(workspaceCommandRunnerService['logger'], 'log')
|
|
.mockImplementation();
|
|
jest
|
|
.spyOn(workspaceCommandRunnerService['logger'], 'error')
|
|
.mockImplementation();
|
|
|
|
return {
|
|
module,
|
|
dataSource,
|
|
runner,
|
|
};
|
|
};
|
|
|
|
let seedSequenceCounter = 0;
|
|
|
|
export const resetSeedSequenceCounter = () => {
|
|
seedSequenceCounter = 0;
|
|
};
|
|
|
|
export const seedInstanceMigration = async (
|
|
dataSource: DataSource,
|
|
{
|
|
name,
|
|
status,
|
|
workspaceIds = [],
|
|
attempt = 1,
|
|
}: {
|
|
name: string;
|
|
status: 'completed' | 'failed';
|
|
workspaceIds?: string[];
|
|
attempt?: number;
|
|
},
|
|
) => {
|
|
// Seeds must have past timestamps so the runner's NOW()-based records
|
|
// always sort after them in createdAt order.
|
|
const createdAt = new Date(
|
|
Date.now() - (1000000 - seedSequenceCounter * 1000),
|
|
).toISOString();
|
|
|
|
seedSequenceCounter++;
|
|
|
|
const values: string[] = [];
|
|
const args: unknown[] = [];
|
|
let paramIndex = 1;
|
|
|
|
values.push(
|
|
`($${paramIndex++}, $${paramIndex++}, $${paramIndex++}, $${paramIndex++}, NULL, $${paramIndex++}, false)`,
|
|
);
|
|
args.push(name, status, attempt, EXECUTED_BY_VERSION, createdAt);
|
|
|
|
for (const workspaceId of workspaceIds) {
|
|
values.push(
|
|
`($${paramIndex++}, $${paramIndex++}, $${paramIndex++}, $${paramIndex++}, $${paramIndex++}, $${paramIndex++}, false)`,
|
|
);
|
|
args.push(name, status, attempt, EXECUTED_BY_VERSION, workspaceId, createdAt);
|
|
}
|
|
|
|
await dataSource.query(
|
|
`INSERT INTO core."upgradeMigration" (name, status, attempt, "executedByVersion", "workspaceId", "createdAt", "isInitial")
|
|
VALUES ${values.join(', ')}`,
|
|
args,
|
|
);
|
|
};
|
|
|
|
export const seedWorkspaceMigration = async (
|
|
dataSource: DataSource,
|
|
{
|
|
name,
|
|
status,
|
|
workspaceId,
|
|
attempt = 1,
|
|
isInitial = false,
|
|
useCurrentTimestamp = false,
|
|
}: {
|
|
name: string;
|
|
status: 'completed' | 'failed';
|
|
workspaceId: string;
|
|
attempt?: number;
|
|
isInitial?: boolean;
|
|
useCurrentTimestamp?: boolean;
|
|
},
|
|
) => {
|
|
if (useCurrentTimestamp) {
|
|
await dataSource.query(
|
|
`INSERT INTO core."upgradeMigration" (name, status, attempt, "executedByVersion", "workspaceId", "isInitial")
|
|
VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
[name, status, attempt, EXECUTED_BY_VERSION, workspaceId, isInitial],
|
|
);
|
|
} else {
|
|
const createdAt = new Date(
|
|
Date.now() - (1000000 - seedSequenceCounter * 1000),
|
|
).toISOString();
|
|
|
|
seedSequenceCounter++;
|
|
|
|
await dataSource.query(
|
|
`INSERT INTO core."upgradeMigration" (name, status, attempt, "executedByVersion", "workspaceId", "createdAt", "isInitial")
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
|
[name, status, attempt, EXECUTED_BY_VERSION, workspaceId, createdAt, isInitial],
|
|
);
|
|
}
|
|
};
|
|
|
|
export type ExecutedMigrationRecord = {
|
|
name: string;
|
|
status: string;
|
|
attempt: number;
|
|
workspaceId: string | null;
|
|
isInitial: boolean;
|
|
};
|
|
|
|
export const testGetExecutedMigrationsInOrder = async (
|
|
dataSource: DataSource,
|
|
): Promise<ExecutedMigrationRecord[]> => {
|
|
return dataSource.query(
|
|
`SELECT name, status, attempt, "workspaceId", "isInitial"
|
|
FROM core."upgradeMigration"
|
|
ORDER BY "createdAt" ASC, "workspaceId" ASC NULLS FIRST, attempt ASC`,
|
|
);
|
|
};
|
|
|
|
export const migrationRecordToKey = ({
|
|
name,
|
|
workspaceId,
|
|
status,
|
|
attempt,
|
|
isInitial,
|
|
}: ExecutedMigrationRecord): string => {
|
|
const scope = workspaceId ?? 'instance';
|
|
const initial = isInitial ? ':initial' : '';
|
|
|
|
return `${name}:${scope}:${status}:${attempt}${initial}`;
|
|
};
|