mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: apply multiple fixes
This commit is contained in:
parent
e0d2388e95
commit
652ca78120
@ -18,7 +18,7 @@ export class Migration {
|
|||||||
@Prop({ type: String, required: true, unique: true })
|
@Prop({ type: String, required: true, unique: true })
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@Prop({ type: String, required: true, enum: MigrationAction })
|
@Prop({ type: String, required: true, enum: Object.values(MigrationAction) })
|
||||||
status: MigrationAction;
|
status: MigrationAction;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,7 +28,6 @@ import {
|
|||||||
MigrationAction,
|
MigrationAction,
|
||||||
MigrationRunParams,
|
MigrationRunParams,
|
||||||
MigrationSuccessCallback,
|
MigrationSuccessCallback,
|
||||||
MigrationTrigger,
|
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -116,9 +115,11 @@ export class MigrationService implements OnApplicationBootstrap {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
async up() {
|
async up() {
|
||||||
// Migration logic
|
// Migration logic
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
async down() {
|
async down() {
|
||||||
// Rollback logic
|
// Rollback logic
|
||||||
|
return false;
|
||||||
},
|
},
|
||||||
};`;
|
};`;
|
||||||
}
|
}
|
||||||
@ -147,18 +148,18 @@ module.exports = {
|
|||||||
}: MigrationRunParams) {
|
}: MigrationRunParams) {
|
||||||
if (!name) {
|
if (!name) {
|
||||||
if (isAutoMigrate) {
|
if (isAutoMigrate) {
|
||||||
await this.runFromVersion(action, version);
|
await this.runUpgrades(action, version);
|
||||||
} else {
|
} else {
|
||||||
await this.runAll(action);
|
await this.runAll(action);
|
||||||
this.exit();
|
this.exit();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
await this.runOne({ action, name, trigger: MigrationTrigger.CLI });
|
await this.runOne({ action, name });
|
||||||
this.exit();
|
this.exit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async runOne({ name, action, trigger }: MigrationRunParams) {
|
private async runOne({ name, action }: MigrationRunParams) {
|
||||||
// verify DB status
|
// verify DB status
|
||||||
const { exist, migrationDocument } = await this.verifyStatus({
|
const { exist, migrationDocument } = await this.verifyStatus({
|
||||||
name,
|
name,
|
||||||
@ -170,13 +171,14 @@ module.exports = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const migration = await this.loadMigrationFile(name);
|
const migration = await this.loadMigrationFile(name);
|
||||||
await migration[action]();
|
const result = await migration[action]();
|
||||||
await this.successCallback({
|
if (result) {
|
||||||
name,
|
await this.successCallback({
|
||||||
action,
|
name,
|
||||||
migrationDocument,
|
action,
|
||||||
trigger,
|
migrationDocument,
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.failureCallback({
|
this.failureCallback({
|
||||||
name,
|
name,
|
||||||
@ -212,11 +214,10 @@ module.exports = {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async runFromVersion(action: MigrationAction, version: string) {
|
private async runUpgrades(action: MigrationAction, version: string) {
|
||||||
const filenames = await this.getDirFiles();
|
const versions = await this.getAvailableUpgradeVersions();
|
||||||
const versions = this.getVersionsFromFilenames(filenames);
|
const filteredVersions = versions.filter((v) =>
|
||||||
const filteredVersions = versions.filter(
|
this.isNewerVersion(v, version),
|
||||||
(v) => v === version || this.isNewerVersion(v, version),
|
|
||||||
);
|
);
|
||||||
let lastVersion = version;
|
let lastVersion = version;
|
||||||
|
|
||||||
@ -229,8 +230,7 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async runAll(action: MigrationAction) {
|
private async runAll(action: MigrationAction) {
|
||||||
const filenames = await this.getDirFiles();
|
const versions = await this.getAvailableUpgradeVersions();
|
||||||
const versions = this.getVersionsFromFilenames(filenames);
|
|
||||||
|
|
||||||
for (const version of versions) {
|
for (const version of versions) {
|
||||||
await this.runOne({ name: version, action });
|
await this.runOne({ name: version, action });
|
||||||
@ -272,17 +272,17 @@ module.exports = {
|
|||||||
return migrationName;
|
return migrationName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private getVersionsFromFilenames(filenames: string[]) {
|
private async getAvailableUpgradeVersions() {
|
||||||
return filenames
|
const filenames = await this.getMigrationFiles();
|
||||||
.filter((fileName) => fileName.endsWith('.migration.js'))
|
|
||||||
.map((filename: string) => {
|
return filenames.map((filename: string) => {
|
||||||
const [migrationFileName] = filename.split('.');
|
const [migrationFileName] = filename.split('.');
|
||||||
const [, , ...migrationVersion] = migrationFileName.split('-');
|
const [, , ...migrationVersion] = migrationFileName.split('-');
|
||||||
return `v${migrationVersion.join('.')}`;
|
return `v${migrationVersion.join('.')}`;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async findMigrationFileByName(name: string): Promise<string | null> {
|
async findMigrationFileByName(version: string): Promise<string | null> {
|
||||||
const files = await this.getMigrationFiles();
|
const files = await this.getMigrationFiles();
|
||||||
return (
|
return (
|
||||||
files.find((file) => {
|
files.find((file) => {
|
||||||
@ -290,7 +290,7 @@ module.exports = {
|
|||||||
/\.migration\.(js|ts)/,
|
/\.migration\.(js|ts)/,
|
||||||
'',
|
'',
|
||||||
);
|
);
|
||||||
return migrationName === kebabCase(name);
|
return migrationName === kebabCase(version);
|
||||||
}) || null
|
}) || null
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -339,19 +339,17 @@ module.exports = {
|
|||||||
name,
|
name,
|
||||||
action,
|
action,
|
||||||
migrationDocument,
|
migrationDocument,
|
||||||
trigger,
|
|
||||||
}: MigrationSuccessCallback) {
|
}: MigrationSuccessCallback) {
|
||||||
await this.updateStatus({ name, action, migrationDocument });
|
await this.updateStatus({ name, action, migrationDocument });
|
||||||
const migrationDisplayName = `${name} [${action}]`;
|
const migrationDisplayName = `${name} [${action}]`;
|
||||||
this.logger.log(`"${migrationDisplayName}" migration done`);
|
this.logger.log(`"${migrationDisplayName}" migration done`);
|
||||||
if (trigger === MigrationTrigger.CLI) {
|
// Update DB version
|
||||||
const result = await this.metadataService.createOrUpdate({
|
const result = await this.metadataService.createOrUpdate({
|
||||||
name: 'db-version',
|
name: 'db-version',
|
||||||
value: name,
|
value: name,
|
||||||
});
|
});
|
||||||
const operation = result ? 'updated' : 'created';
|
const operation = result ? 'updated' : 'created';
|
||||||
this.logger.log(`db-version metadata ${operation} "${name}"`);
|
this.logger.log(`db-version metadata ${operation} "${name}"`);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private failureCallback({ name, action }: MigrationRunParams) {
|
private failureCallback({ name, action }: MigrationRunParams) {
|
||||||
|
|||||||
@ -13,26 +13,15 @@ enum MigrationAction {
|
|||||||
DOWN = 'down',
|
DOWN = 'down',
|
||||||
}
|
}
|
||||||
|
|
||||||
enum MigrationTrigger {
|
|
||||||
CLI = 'cli',
|
|
||||||
PROGRAM = 'program',
|
|
||||||
}
|
|
||||||
|
|
||||||
interface MigrationRunParams {
|
interface MigrationRunParams {
|
||||||
name?: string;
|
name?: string;
|
||||||
action: MigrationAction;
|
action: MigrationAction;
|
||||||
version?: string;
|
version?: string;
|
||||||
isAutoMigrate?: boolean;
|
isAutoMigrate?: boolean;
|
||||||
trigger?: MigrationTrigger;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MigrationSuccessCallback extends MigrationRunParams {
|
interface MigrationSuccessCallback extends MigrationRunParams {
|
||||||
migrationDocument: MigrationDocument;
|
migrationDocument: MigrationDocument;
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export { MigrationAction, MigrationRunParams, MigrationSuccessCallback };
|
||||||
MigrationAction,
|
|
||||||
MigrationRunParams,
|
|
||||||
MigrationSuccessCallback,
|
|
||||||
MigrationTrigger,
|
|
||||||
};
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user