Skip to content

Commit

Permalink
fix(migration): fix alter table migration not working with sqlite (#290)
Browse files Browse the repository at this point in the history
* remove event ids

* fix

* bug

* ej

* fix
  • Loading branch information
samuelmasse committed Dec 15, 2021
1 parent e6c8dd7 commit 2599256
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/engine/src/database/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ export class DatabaseService extends Service {
}
}

getIsLite() {
return this.isLite
}

getJson(val: any): any {
if (this.isLite) {
return val ? JSON.parse(val) : undefined
Expand Down
4 changes: 3 additions & 1 deletion packages/engine/src/migration/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { Knex } from 'knex'
export abstract class Migration {
protected trx!: Knex.Transaction
protected isDown!: boolean
protected isLite!: boolean

abstract get meta(): MigrationMeta

async init(trx: Knex.Transaction, isDown: boolean) {
async init(trx: Knex.Transaction, isDown: boolean, isLite: boolean) {
this.trx = trx
this.isDown = isDown
this.isLite = isLite
}

async shouldRun() {
Expand Down
17 changes: 16 additions & 1 deletion packages/engine/src/migration/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class MigrationService extends Service {
private async runMigrations(migrations: Migration[]) {
this.loggerDry.window([clc.bold(`Executing ${migrations.length} migration${migrations.length > 1 ? 's' : ''}`)])

await this.disableSqliteForeignKeys()
const migrationsByVersion = _.groupBy(migrations, 'meta.version')
const trx = await this.db.knex.transaction()

Expand All @@ -118,6 +119,8 @@ export class MigrationService extends Service {
await trx.rollback()
this.loggerDry.error(e, 'Migrations failed')
throw new ShutDownSignal(1)
} finally {
await this.enableSqliteForeignKeys()
}
}

Expand All @@ -126,7 +129,7 @@ export class MigrationService extends Service {

for (const migration of migrations) {
this.loggerDry.info(`Running ${migration.meta.name}`)
await migration.init(trx, this.isDown)
await migration.init(trx, this.isDown, this.db.getIsLite())

if (await migration.shouldRun()) {
await migration.run()
Expand Down Expand Up @@ -192,4 +195,16 @@ export class MigrationService extends Service {
}
}
}

private async disableSqliteForeignKeys() {
if (this.db.getIsLite()) {
await this.db.knex.raw('PRAGMA foreign_keys = OFF;')
}
}

private async enableSqliteForeignKeys() {
if (this.db.getIsLite()) {
await this.db.knex.raw('PRAGMA foreign_keys = ON;')
}
}
}
2 changes: 1 addition & 1 deletion packages/server/src/clients/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Emitter, uuid } from '@botpress/messaging-base'
import { Client } from './types'

export enum ClientEvents {
Updated = 4198497150859385
Updated
}

export interface ClientUpdatedEvent {
Expand Down
7 changes: 3 additions & 4 deletions packages/server/src/conduits/events.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Emitter, uuid } from '@botpress/messaging-base'

// Note: event type ids should be random numbers between -9007199254740991 and 9007199254740991
export enum ConduitEvents {
Created = -8735528850944253,
Deleting = -2992045965298849,
Updated = -4333216986594445
Created,
Deleting,
Updated
}

export class ConduitEmitter extends Emitter<{
Expand Down
11 changes: 5 additions & 6 deletions packages/server/src/instances/events.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Emitter, uuid } from '@botpress/messaging-base'

// Note: event type ids should be random numbers between -9007199254740991 and 9007199254740991
export enum InstanceEvents {
Setup = -152025756428307,
SetupFailed = 1227260138789451,
Initialized = 8719796083603497,
InitializationFailed = -7790961391717513,
Destroyed = 1601810829650873
Setup,
SetupFailed,
Initialized,
InitializationFailed,
Destroyed
}

export class InstanceEmitter extends Emitter<{
Expand Down
4 changes: 2 additions & 2 deletions packages/server/src/providers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Emitter, uuid } from '@botpress/messaging-base'
import { Provider } from './types'

export enum ProviderEvents {
Updated = -6307907498173656,
Deleting = 4113861503631719
Updated,
Deleting
}

export interface ProviderUpdatedEvent {
Expand Down

0 comments on commit 2599256

Please sign in to comment.