23874848a4
# Introduction Now only using typeorm to generate migrations up and down statement We handle and maintain our own migration table history ## What's new Now all the instance commands will live within the same module and folder than the upgrade commands Sequentiality comes from the timestamp located in the filename Same sequentiality also applies to the workspace commands in the future, for the moment still expected a as code explicit declaration ( below screen is an example see below section ) <img width="1382" height="634" alt="image" src="https://github.com/user-attachments/assets/5610a246-4eae-485e-99f4-98fb89ad5ac8" /> ## Existing 1.21 migrations We won't start following this pattern in 1.21 yet at least not with the migration that has already been released as typeorm migrations in cloud production as they would rerun ## Small duplication Duplicating the legacy typeorm and instance commands run in the `run-instance-commands` to avoid any merge of interest for the moment ## Concurrency Not handling any run in parrallel of the upgrade for the moment
92 lines
2.8 KiB
TypeScript
92 lines
2.8 KiB
TypeScript
import { InjectDataSource } from '@nestjs/typeorm';
|
|
|
|
import { Command } from 'nest-commander';
|
|
import { DataSource } from 'typeorm';
|
|
|
|
import { ActiveOrSuspendedWorkspaceCommandRunner } from 'src/database/commands/command-runners/active-or-suspended-workspace.command-runner';
|
|
import { WorkspaceIteratorService } from 'src/database/commands/command-runners/workspace-iterator.service';
|
|
import { type RunOnWorkspaceArgs } from 'src/database/commands/command-runners/workspace.command-runner';
|
|
import { addGlobalKeyValuePairUniqueIndexQueries } from 'src/database/typeorm/core/migrations/utils/1774700000000-add-global-key-value-pair-unique-index.util';
|
|
|
|
@Command({
|
|
name: 'upgrade:1-21:add-global-key-value-pair-unique-index',
|
|
description:
|
|
'Deduplicate global keyValuePair rows and add the null/null unique index',
|
|
})
|
|
export class AddGlobalKeyValuePairUniqueIndexCommand extends ActiveOrSuspendedWorkspaceCommandRunner {
|
|
private hasRunOnce = false;
|
|
|
|
private async deduplicateGlobalKeyValuePairs(
|
|
queryRunner: DataSource['createQueryRunner'] extends () => infer T
|
|
? T
|
|
: never,
|
|
): Promise<void> {
|
|
await queryRunner.query(`
|
|
DELETE FROM "core"."keyValuePair"
|
|
WHERE id IN (
|
|
SELECT id
|
|
FROM (
|
|
SELECT
|
|
id,
|
|
ROW_NUMBER() OVER (
|
|
PARTITION BY key
|
|
ORDER BY "updatedAt" DESC, "createdAt" DESC, id DESC
|
|
) AS row_number
|
|
FROM "core"."keyValuePair"
|
|
WHERE "userId" IS NULL
|
|
AND "workspaceId" IS NULL
|
|
) ranked_key_value_pairs
|
|
WHERE ranked_key_value_pairs.row_number > 1
|
|
)
|
|
`);
|
|
}
|
|
|
|
constructor(
|
|
protected readonly workspaceIteratorService: WorkspaceIteratorService,
|
|
@InjectDataSource()
|
|
private readonly coreDataSource: DataSource,
|
|
) {
|
|
super(workspaceIteratorService);
|
|
}
|
|
|
|
override async runOnWorkspace({
|
|
options,
|
|
}: RunOnWorkspaceArgs): Promise<void> {
|
|
if (this.hasRunOnce) {
|
|
this.logger.log(
|
|
'Skipping has already been run once AddGlobalKeyValuePairUniqueIndexCommand',
|
|
);
|
|
|
|
return;
|
|
}
|
|
|
|
if (options.dryRun) {
|
|
return;
|
|
}
|
|
|
|
const queryRunner = this.coreDataSource.createQueryRunner();
|
|
|
|
await queryRunner.connect();
|
|
await queryRunner.startTransaction();
|
|
|
|
try {
|
|
await this.deduplicateGlobalKeyValuePairs(queryRunner);
|
|
await addGlobalKeyValuePairUniqueIndexQueries(queryRunner);
|
|
|
|
await queryRunner.commitTransaction();
|
|
this.logger.log(
|
|
'Successfully run AddGlobalKeyValuePairUniqueIndexCommand',
|
|
);
|
|
this.hasRunOnce = true;
|
|
} catch (error) {
|
|
await queryRunner.rollbackTransaction();
|
|
this.logger.error(
|
|
`Rolling back AddGlobalKeyValuePairUniqueIndexCommand: ${error.message}`,
|
|
);
|
|
throw error;
|
|
} finally {
|
|
await queryRunner.release();
|
|
}
|
|
}
|
|
}
|