Instance commands and upgrade_migrations table (#19356)
# 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
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
|
||||
import { pascalCase } from 'twenty-shared/utils';
|
||||
import { DataSource } from 'typeorm';
|
||||
|
||||
import { type UpgradeCommandVersion } from 'src/engine/constants/upgrade-command-supported-versions.constant';
|
||||
|
||||
type GenerateMigrationArgs = {
|
||||
migrationName: string;
|
||||
version: UpgradeCommandVersion;
|
||||
timestamp: number;
|
||||
};
|
||||
|
||||
export type GeneratedMigrationResult = {
|
||||
fileName: string;
|
||||
fileTemplate: string;
|
||||
className: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class InstanceCommandGenerationService {
|
||||
constructor(
|
||||
@InjectDataSource()
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async generate({
|
||||
migrationName,
|
||||
version,
|
||||
timestamp,
|
||||
}: GenerateMigrationArgs): Promise<GeneratedMigrationResult | null> {
|
||||
const sqlInMemory = await this.dataSource.driver
|
||||
.createSchemaBuilder()
|
||||
.log();
|
||||
|
||||
if (sqlInMemory.upQueries.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const className = this.buildClassName(migrationName, version, timestamp);
|
||||
|
||||
const upStatements = sqlInMemory.upQueries.map(
|
||||
({ query, parameters }) =>
|
||||
` await queryRunner.query('${this.escapeForSingleQuotedString(query)}'${this.formatQueryParams(parameters)});`,
|
||||
);
|
||||
|
||||
const downStatements = sqlInMemory.downQueries
|
||||
.reverse()
|
||||
.map(
|
||||
({ query, parameters }) =>
|
||||
` await queryRunner.query('${this.escapeForSingleQuotedString(query)}'${this.formatQueryParams(parameters)});`,
|
||||
);
|
||||
|
||||
const fileTemplate = this.buildMigrationFileContent({
|
||||
className,
|
||||
version,
|
||||
timestamp,
|
||||
upStatements,
|
||||
downStatements,
|
||||
});
|
||||
|
||||
const versionSlug = version.split('.').slice(0, 2).join('-');
|
||||
const fileName = `${versionSlug}-instance-command-fast-${timestamp}-${migrationName}.ts`;
|
||||
|
||||
return { fileName, fileTemplate, className };
|
||||
}
|
||||
|
||||
private buildClassName(
|
||||
name: string,
|
||||
version: string,
|
||||
timestamp: number,
|
||||
): string {
|
||||
const versionSlug = version.split('.').slice(0, 2).join('_');
|
||||
|
||||
return `V${versionSlug}_${pascalCase(name)}_${timestamp}`;
|
||||
}
|
||||
|
||||
private formatQueryParams(parameters: unknown[] | undefined): string {
|
||||
if (!parameters || !parameters.length) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return `, ${JSON.stringify(parameters)}`;
|
||||
}
|
||||
|
||||
private escapeForSingleQuotedString(query: string): string {
|
||||
return query.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
||||
}
|
||||
|
||||
private buildMigrationFileContent({
|
||||
className,
|
||||
version,
|
||||
timestamp,
|
||||
upStatements,
|
||||
downStatements,
|
||||
}: {
|
||||
className: string;
|
||||
version: string;
|
||||
timestamp: number;
|
||||
upStatements: string[];
|
||||
downStatements: string[];
|
||||
}): string {
|
||||
return `import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
import { RegisteredInstanceMigration } from 'src/database/typeorm/core/decorators/registered-instance-migration.decorator';
|
||||
|
||||
@RegisteredInstanceMigration('${version}', ${timestamp})
|
||||
export class ${className} implements MigrationInterface {
|
||||
name = '${className}';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
${upStatements.join('\n')}
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
${downStatements.join('\n')}
|
||||
}
|
||||
}
|
||||
`;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user