-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(misunderstood): proper constraint migration (#4351)
* moved migration * fixed typing * fix(misunderstood): proper constraint migration
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
modules/misunderstood/src/migrations/v12_16_1-1608235308-add_thumbs_down_misunderstood.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters