2024-02-08 06:13:29 -08:00
|
|
|
import type { QueryRunner } from '@n8n/typeorm';
|
|
|
|
import { TableIndex } from '@n8n/typeorm';
|
2023-08-04 08:49:02 -07:00
|
|
|
import LazyPromise from 'p-lazy';
|
|
|
|
|
|
|
|
abstract class IndexOperation extends LazyPromise<void> {
|
|
|
|
abstract execute(queryRunner: QueryRunner): Promise<void>;
|
|
|
|
|
2023-08-09 03:30:02 -07:00
|
|
|
get fullTableName() {
|
|
|
|
return [this.tablePrefix, this.tableName].join('');
|
|
|
|
}
|
|
|
|
|
|
|
|
get fullIndexName() {
|
|
|
|
return ['IDX', `${this.tablePrefix}${this.tableName}`, ...this.columnNames].join('_');
|
|
|
|
}
|
|
|
|
|
2023-08-04 08:49:02 -07:00
|
|
|
constructor(
|
|
|
|
protected tableName: string,
|
2023-08-09 03:30:02 -07:00
|
|
|
protected columnNames: string[],
|
2023-09-04 07:57:10 -07:00
|
|
|
protected tablePrefix: string,
|
2023-08-04 08:49:02 -07:00
|
|
|
queryRunner: QueryRunner,
|
2023-08-09 03:30:02 -07:00
|
|
|
protected customIndexName?: string,
|
2023-08-04 08:49:02 -07:00
|
|
|
) {
|
|
|
|
super((resolve) => {
|
|
|
|
void this.execute(queryRunner).then(resolve);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CreateIndex extends IndexOperation {
|
|
|
|
constructor(
|
|
|
|
tableName: string,
|
2023-08-09 03:30:02 -07:00
|
|
|
columnNames: string[],
|
2023-08-04 08:49:02 -07:00
|
|
|
protected isUnique: boolean,
|
2023-09-04 07:57:10 -07:00
|
|
|
tablePrefix: string,
|
2023-08-04 08:49:02 -07:00
|
|
|
queryRunner: QueryRunner,
|
2023-08-09 03:30:02 -07:00
|
|
|
customIndexName?: string,
|
2023-08-04 08:49:02 -07:00
|
|
|
) {
|
2023-09-04 07:57:10 -07:00
|
|
|
super(tableName, columnNames, tablePrefix, queryRunner, customIndexName);
|
2023-08-04 08:49:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async execute(queryRunner: QueryRunner) {
|
2023-08-09 03:30:02 -07:00
|
|
|
const { columnNames, isUnique } = this;
|
2024-01-17 07:08:50 -08:00
|
|
|
return await queryRunner.createIndex(
|
2023-08-09 03:30:02 -07:00
|
|
|
this.fullTableName,
|
|
|
|
new TableIndex({ name: this.customIndexName ?? this.fullIndexName, columnNames, isUnique }),
|
2023-08-04 08:49:02 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class DropIndex extends IndexOperation {
|
|
|
|
async execute(queryRunner: QueryRunner) {
|
2024-01-17 07:08:50 -08:00
|
|
|
return await queryRunner.dropIndex(
|
|
|
|
this.fullTableName,
|
|
|
|
this.customIndexName ?? this.fullIndexName,
|
|
|
|
);
|
2023-08-04 08:49:02 -07:00
|
|
|
}
|
|
|
|
}
|