<commit_before>import * as sdk from 'botpress/sdk'
import { ConfigProvider } from 'core/config/config-loader'
import Database from 'core/database'
import { StrategyUserTable } from 'core/database/tables/server-wide/strategy_users'
import { StrategyUsersRepository } from 'core/repositories/strategy_users'
import { WorkspaceUsersRepository } from 'core/repositories/workspace_users'
import { GhostService } from 'core/services'
import { Migration } from 'core/services/migration'
import { TYPES } from 'core/types'
import { Container } from 'inversify'
import _ from 'lodash'

const migration: Migration = {
  info: {
    description: 'Copy existing users from workspaces file to database',
    type: 'database'
  },
  up: async (
    bp: typeof sdk,
    configProvider: ConfigProvider,
    database: Database,
    inversify: Container
  ): Promise<sdk.MigrationResult> => {
    const ghost = inversify.get<GhostService>(TYPES.GhostService)
    const userRepo = inversify.get<StrategyUsersRepository>(TYPES.StrategyUsersRepository)
    const workspaceRepo = inversify.get<WorkspaceUsersRepository>(TYPES.WorkspaceUsersRepository)

    const strategyTable = new StrategyUserTable()
    await strategyTable.createStrategyTable(database.knex, `strategy_default`)

    try {
      const workspaces: any = await ghost.global().readFileAsObject('/', `workspaces.json`)
      const users = _.get(workspaces, '[0].users')

      for (const user of users) {
        await userRepo.createUser({
          email: user.email,
          strategy: 'default',
          password: <PASSWORD>,
          salt: user.salt,
          attributes: _.pick(user, [
            'last_ip',
            'last_logon',
            'unscuccessful_logins',
            'locked_out',
            'firstname',
            'lastname'
          ])
        })

        await workspaceRepo.createEntry({
          email: user.email,
          strategy: 'default',
          role: user.role,
          workspace: 'default'
        })
      }
    } catch (err) {
      return { success: false, message: `Could not migrate users: ${err.messag}` }
    }

    return { success: true }
  }
}

export default migration
<commit_msg>added check for migration<commit_after>import * as sdk from 'botpress/sdk'
import { ConfigProvider } from 'core/config/config-loader'
import Database from 'core/database'
import { StrategyUserTable } from 'core/database/tables/server-wide/strategy_users'
import { StrategyUsersRepository } from 'core/repositories/strategy_users'
import { WorkspaceUsersRepository } from 'core/repositories/workspace_users'
import { GhostService } from 'core/services'
import { Migration } from 'core/services/migration'
import { TYPES } from 'core/types'
import { Container } from 'inversify'
import _ from 'lodash'

const migration: Migration = {
  info: {
    description: 'Copy existing users from workspaces file to database',
    type: 'database'
  },
  up: async (
    bp: typeof sdk,
    configProvider: ConfigProvider,
    database: Database,
    inversify: Container
  ): Promise<sdk.MigrationResult> => {
    const ghost = inversify.get<GhostService>(TYPES.GhostService)
    const userRepo = inversify.get<StrategyUsersRepository>(TYPES.StrategyUsersRepository)
    const workspaceRepo = inversify.get<WorkspaceUsersRepository>(TYPES.WorkspaceUsersRepository)

    const strategyTable = new StrategyUserTable()
    await strategyTable.createStrategyTable(database.knex, `strategy_default`)

    try {
      const workspaces: any = await ghost.global().readFileAsObject('/', `workspaces.json`)
      const users = _.get(workspaces, '[0].users')

      if (!_.isArray(users)) {
        return { success: true, message: `No users in the workspaces file to migrate.` }
      }

      for (const user of users) {
        await userRepo.createUser({
          email: user.email,
          strategy: 'default',
          password: <PASSWORD>,
          salt: user.salt,
          attributes: _.pick(user, [
            'last_ip',
            'last_logon',
            'unscuccessful_logins',
            'locked_out',
            'firstname',
            'lastname'
          ])
        })

        await workspaceRepo.createEntry({
          email: user.email,
          strategy: 'default',
          role: user.role,
          workspace: 'default'
        })
      }
    } catch (err) {
      return { success: false, message: `Could not migrate users: ${err.message}` }
    }

    return { success: true }
  }
}

export default migration

{'fraction_non_alphanumeric': 0.10761736049601417, 'fraction_numerical': 0.0004428697962798937, 'mean_word_length': 2.9588080631025417, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, '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': '3136223', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1285, 'n_words': 313}