Skip to content

Commit

Permalink
fix(migrations): fix migrations with targets that are out of bounds (#…
Browse files Browse the repository at this point in the history
…274)

* rename file

* validate src and dst

* fix migrating to version above app version
  • Loading branch information
samuelmasse committed Dec 7, 2021
1 parent c87e430 commit c0e7a50
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
30 changes: 30 additions & 0 deletions packages/engine/src/migration/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export class MigrationService extends Service {
}

private async migrate() {
this.validateSrcAndDst()

const migrations = this.listMigrationsToRun()
if (!migrations.length && !this.isDry && !process.env.MIGRATE_CMD?.length) {
return
Expand All @@ -66,6 +68,34 @@ export class MigrationService extends Service {
await this.runMigrations(migrations)
}

private validateSrcAndDst() {
if (this.isDown && semver.gt(this.dstVersion, this.srcVersion)) {
this.logger.error(
undefined,
`Invalid migration parameters: down migration cannot target a version (${this.dstVersion}) higher than the current server version (${this.srcVersion})`
)
throw new ShutDownSignal(1)
}

if (!this.isDown && semver.lt(this.dstVersion, this.srcVersion)) {
this.logger.error(
undefined,
`Invalid migration parameters: up migration cannot target a version (${this.dstVersion}) lower than the current server version (${this.srcVersion})`
)
throw new ShutDownSignal(1)
}

if (semver.gt(this.dstVersion, this.meta.app().version)) {
this.logger.error(
undefined,
`Invalid migration parameters: up migration cannot target a version (${
this.dstVersion
}) higher than the application version (${this.meta.app().version})`
)
throw new ShutDownSignal(1)
}
}

private async runMigrations(migrations: Migration[]) {
this.loggerDry.window([clc.bold(`Executing ${migrations.length} migration${migrations.length > 1 ? 's' : ''}`)])

Expand Down
2 changes: 1 addition & 1 deletion packages/server/src/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Migration } from '@botpress/messaging-engine'
import { StatusMigration } from './0.1.18-status'
import { StatusMigration } from './0.1.19-status'

export const Migrations: { new (): Migration }[] = [StatusMigration]

0 comments on commit c0e7a50

Please sign in to comment.