Skip to content

Commit

Permalink
fix(misunderstood): proper constraint migration (#4351)
Browse files Browse the repository at this point in the history
* moved migration

* fixed typing

* fix(misunderstood): proper constraint migration
  • Loading branch information
EFF committed Jan 11, 2021
1 parent 33cc0d8 commit acf046a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
4 changes: 2 additions & 2 deletions modules/misunderstood/src/backend/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import moment from 'moment'

import {
DbFlaggedEvent,
FilteringOptions,
FlaggedEvent,
FLAGGED_MESSAGE_STATUS,
FLAGGED_MESSAGE_STATUSES,
FLAG_REASON,
ResolutionData,
RESOLUTION_TYPE,
FilteringOptions
RESOLUTION_TYPE
} from '../types'

import applyChanges from './applyChanges'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import * as sdk from 'botpress/sdk'
import Knex from 'knex'

import { FLAGGED_MESSAGE_STATUS, FLAGGED_MESSAGE_STATUSES, FLAG_REASON, RESOLUTION_TYPE } from '../types'

const TEMP_TABLE_NAME = 'misunderstood_tmp'
const TABLE_NAME = 'misunderstood'
const COLUMN = 'reason'

const migrateLite = async (db: sdk.KnexExtended, trx: Knex.Transaction) => {
// 1- create a temp table with same schema and good constraint
const hastmp = await db.schema.transacting(trx).hasTable(TEMP_TABLE_NAME)
if (hastmp) {
await trx.raw(`TRUNCATE TABLE ${TEMP_TABLE_NAME}`)
} else {
await db.schema.transacting(trx).createTable(TEMP_TABLE_NAME, table => {
table.increments('id')
table.string('eventId')
table.string('botId')
table.string('language')
table.string('preview')
table.enum('reason', Object.values(FLAG_REASON))
table.enum('status', FLAGGED_MESSAGE_STATUSES).defaultTo(FLAGGED_MESSAGE_STATUS.new)
table.enum('resolutionType', Object.values(RESOLUTION_TYPE))
table.string('resolution')
table.json('resolutionParams')
table.timestamp('createdAt').defaultTo(db.fn.now())
table.timestamp('updatedAt').defaultTo(db.fn.now())
})
}

// 2- move all data from original table to that table
await trx.raw(`INSERT INTO ${TEMP_TABLE_NAME} SELECT * FROM ${TABLE_NAME}`)
// 3- drop old table
await db.schema.transacting(trx).dropTable(TABLE_NAME)
// 4- rename temp table to original table name
await db.schema.transacting(trx).renameTable(TEMP_TABLE_NAME, TABLE_NAME)
}

const migratePG = async (db: sdk.KnexExtended) => {
const constraintName = `${TABLE_NAME}_${COLUMN}_check`
const constraintValues: string = Object.values(FLAG_REASON)
.map(r => `'${r}'::text`)
.join(', ')

const query = `
ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT IF EXISTS ${constraintName};
ALTER TABLE ${TABLE_NAME} ADD CONSTRAINT ${constraintName} CHECK (${COLUMN} = ANY (ARRAY[${constraintValues}]));
`

return db.raw(query)
}

const migration: sdk.ModuleMigration = {
info: {
description: `Adds thumbs down as reason on ${TABLE_NAME}`,
type: 'database'
},
up: async ({ bp, database }: sdk.ModuleMigrationOpts): Promise<sdk.MigrationResult> => {
const db = database.knex as sdk.KnexExtended
try {
if (bp.database.isLite) {
await db.transaction(async trx => migrateLite(db, trx))
} else {
await migratePG(db)
}

return { success: true, message: `Constraint on ${COLUMN} of ${TABLE_NAME} altered` }
} catch (error) {
bp.logger.attachError(error).error(`Could not alter constraint on ${COLUMN} of ${TABLE_NAME}`)
return { success: false, message: error.message }
}
}
}

export default migration
2 changes: 1 addition & 1 deletion src/bp/core/services/migration/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class MigrationService {

await this.database
.knex('srv_migrations')
.insert({ ...entry, details: logs.join('\n'), created_at: this.database.knex.date.now() })
.insert({ ...entry, details: logs.join('\n').slice(-255), created_at: this.database.knex.date.now() })

const hasBotMigrations = !!missingMigrations.find(x => this.loadedMigrations[x.filename].info.target === 'bot')
if (hasBotMigrations) {
Expand Down

0 comments on commit acf046a

Please sign in to comment.