<commit_before>import * as sdk from 'botpress/sdk'
import { Migration } from 'core/services/migration'

const TABLE_NAME = 'dialog_sessions'
const TEMP_TABLE_NAME = 'dialog_sessions_temp'

const migration: Migration = {
  info: {
    description: 'Change dialog_sessions pk constraint to include botId',
    target: 'core',
    type: 'database'
  },
  up: async ({
    bp,
    configProvider,
    database,
    inversify,
    metadata
  }: sdk.ModuleMigrationOpts): Promise<sdk.MigrationResult> => {
    const db = database.knex as sdk.KnexExtended
    const { client } = db.client.config

    try {
      if (client === 'sqlite3') {
        await db.transaction(async trx => {
          await db.schema.transacting(trx).createTable(TEMP_TABLE_NAME, table => {
            table.string('id').notNullable()
            table.string('botId').notNullable()
            table.json('context').notNullable()
            table.json('temp_data').notNullable()
            table.json('session_data').notNullable()
            table.timestamp('context_expiry').nullable()
            table.timestamp('session_expiry').nullable()
            table.timestamp('created_on').notNullable()
            table.timestamp('modified_on').notNullable()
            table.primary(['id', 'botId'])
          })

          await trx.raw(`INSERT INTO ${TEMP_TABLE_NAME} SELECT * FROM ${TABLE_NAME};`)
          await database.knex.schema.transacting(trx).dropTable(TABLE_NAME)
          await database.knex.schema.transacting(trx).renameTable(TEMP_TABLE_NAME, TABLE_NAME)
        })
      } else {
        await db.transaction(async trx => {
          await trx.raw(`ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT ${TABLE_NAME}_pkey;`)
          await trx.raw(`ALTER TABLE ${TABLE_NAME} ADD PRIMARY KEY (id, "botId");`)
        })
      }
    } catch (err) {
      bp.logger.attachError(err).error('Could not update the primary key constraint')
      return { success: false, message: 'Could not update the primary key constraint' }
    }
    return { success: true, message: 'Primary key constraint successfully updated' }
  }
}

export default migration
<commit_msg>fix(core): make sure column botId exists before doing migration (#4526)<commit_after>import * as sdk from 'botpress/sdk'
import { Migration } from 'core/services/migration'

const TABLE_NAME = 'dialog_sessions'
const TEMP_TABLE_NAME = 'dialog_sessions_temp'

const migration: Migration = {
  info: {
    description: 'Change dialog_sessions PK constraint to include botId and remove botId column',
    target: 'core',
    type: 'database'
  },
  up: async ({
    bp,
    configProvider,
    database,
    inversify,
    metadata
  }: sdk.ModuleMigrationOpts): Promise<sdk.MigrationResult> => {
    const db = database.knex as sdk.KnexExtended
    const { client } = db.client.config

    try {
      const hasBotIdColumn = db.schema.hasColumn(TABLE_NAME, 'botId')
      if (!hasBotIdColumn) {
        return { success: true, message: 'PK constraint already changed and column botId already removed, skipping...' }
      }

      if (client === 'sqlite3') {
        await db.transaction(async trx => {
          await db.schema.transacting(trx).createTable(TEMP_TABLE_NAME, table => {
            table.string('id').notNullable()
            table.string('botId').notNullable()
            table.json('context').notNullable()
            table.json('temp_data').notNullable()
            table.json('session_data').notNullable()
            table.timestamp('context_expiry').nullable()
            table.timestamp('session_expiry').nullable()
            table.timestamp('created_on').notNullable()
            table.timestamp('modified_on').notNullable()
            table.primary(['id', 'botId'])
          })

          await trx.raw(`INSERT INTO ${TEMP_TABLE_NAME} SELECT * FROM ${TABLE_NAME};`)
          await database.knex.schema.transacting(trx).dropTable(TABLE_NAME)
          await database.knex.schema.transacting(trx).renameTable(TEMP_TABLE_NAME, TABLE_NAME)
        })
      } else {
        await db.transaction(async trx => {
          await trx.raw(`ALTER TABLE ${TABLE_NAME} DROP CONSTRAINT ${TABLE_NAME}_pkey;`)
          await trx.raw(`ALTER TABLE ${TABLE_NAME} ADD PRIMARY KEY (id, "botId");`)
        })
      }
    } catch (err) {
      bp.logger.attachError(err).error('Could not update the primary key constraint')
      return { success: false, message: 'Could not update the primary key constraint' }
    }
    return { success: true, message: 'Primary key constraint successfully updated' }
  }
}

export default migration

{'fraction_non_alphanumeric': 0.12382821015914541, 'fraction_numerical': 0.0013080444735120995, 'mean_word_length': 2.9517657192075797, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4672850', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1334, 'n_words': 299}