<commit_before>import IEngine from './IEngine'
import IMigrator from './IMigrator'
import * as fs from 'fs'
import { ConnectionString } from 'connection-string'
import * as DBMigrate from 'db-migrate'

const LENGTH_OF_MIGRATION_NAME = 14

export default class Migrator implements IMigrator {
  engine: IEngine
  migrationsPath: string
  dbmigrate: any

  constructor (engine: IEngine, connectionString: string, migrationsPath: string) {
    this.engine = engine
    const dbMigrateConfig = Migrator.generateConfigObject(connectionString)
    this.dbmigrate = DBMigrate.getInstance(true, dbMigrateConfig)
    this.migrationsPath = migrationsPath
    if (this.migrationsPath.endsWith('/') !== true) {
      this.migrationsPath += '/'
    }
  }

  static generateConfigObject (connectionUrl: string) {
    const driversMap = new Map<string, string>()
    driversMap.set('postgresql', 'pg')
    driversMap.set('sqlite', 'sqlite3')
    const connectionObject = new ConnectionString(connectionUrl)
    let result: DBMigrate.InstanceOptions = {}
    switch (process.env.DB_URL!.split('://')[0]) {
      case 'sqlite': {
        result = {
          cmdOptions: {
            'migrations-dir': './migrations/sqlite'
          },
          config: {
            defaultEnv: 'defaultSqlite',
            defaultSqlite: {
              driver: `${driversMap.get(connectionObject.protocol!)}`,
              filename: `${connectionObject.hostname}`
            }
          }
        }
        break
      }
      case 'postgresql': {
        result = {
          cmdOptions: {
            'migrations-dir': './migrations/postgresql'
          },
          config: {
            defaultEnv: 'defaultPg',
            defaultPg: {
              driver: `${driversMap.get(connectionObject.protocol!)}`,
              user: `${connectionObject.user}`,
              password: `${connectionObject.password}`,
              host: `${connectionObject.hostname}`,
              database: `${connectionObject.segments![0]}`
            }
          }
        }
        break
      }
    }
    return result
  }

  async isLatest (): Promise<boolean> {
    return this.dbmigrate.check()
  }

  async sync (n?: string): Promise<void> {
    if (n !== undefined) {
      this.dbmigrate.sync(n)
    } else {
      const migrationsInFolder = await this.retrieveInFolderMigrationList()
      const lastMigrationInFolderName = migrationsInFolder[migrationsInFolder.length - 1].substring(0, LENGTH_OF_MIGRATION_NAME)
      this.dbmigrate.sync(lastMigrationInFolderName)
    }
  }

  async retrieveInFolderMigrationList (): Promise<string[]> {
    let result: string[] = []
    const listOfFiles: string[] = fs.readdirSync(this.migrationsPath)
    for (let filename of listOfFiles) {
      const isDir = fs.statSync(this.migrationsPath + filename).isDirectory()
      if (!isDir) {
        result.push(filename.slice(0, -3))
      }
    }
    result.sort()
    return result
  }
}
<commit_msg>Fix pkginfo-import-related issue.<commit_after>import IEngine from './IEngine'
import IMigrator from './IMigrator'
import * as fs from 'fs'
import { ConnectionString } from 'connection-string'

const LENGTH_OF_MIGRATION_NAME = 14

export default class Migrator implements IMigrator {
  engine: IEngine
  migrationsPath: string
  dbmigrate: any
  DBMigrate: any
  dbMigrateConfig: any

  constructor (engine: IEngine, connectionString: string, migrationsPath: string) {
    this.engine = engine
    this.dbMigrateConfig = Migrator.generateConfigObject(connectionString)
    this.migrationsPath = migrationsPath
    if (this.migrationsPath.endsWith('/') !== true) {
      this.migrationsPath += '/'
    }
  }

  static generateConfigObject (connectionUrl: string) {
    const driversMap = new Map<string, string>()
    driversMap.set('postgresql', 'pg')
    driversMap.set('sqlite', 'sqlite3')
    const connectionObject = new ConnectionString(connectionUrl)
    let result = {}
    switch (process.env.DB_URL!.split('://')[0]) {
      case 'sqlite': {
        result = {
          cmdOptions: {
            'migrations-dir': './migrations/sqlite'
          },
          config: {
            defaultEnv: 'defaultSqlite',
            defaultSqlite: {
              driver: `${driversMap.get(connectionObject.protocol!)}`,
              filename: `${connectionObject.hostname}`
            }
          }
        }
        break
      }
      case 'postgresql': {
        result = {
          cmdOptions: {
            'migrations-dir': './migrations/postgresql'
          },
          config: {
            defaultEnv: 'defaultPg',
            defaultPg: {
              driver: `${driversMap.get(connectionObject.protocol!)}`,
              user: `${connectionObject.user}`,
              password: `${<PASSWORD>}`,
              host: `${connectionObject.hostname}`,
              database: `${connectionObject.segments![0]}`
            }
          }
        }
        break
      }
    }
    return result
  }

  async isLatest (): Promise<boolean> {
    await this.ensureDBMigrateInit()
    return this.dbmigrate.check()
  }

  async sync (n?: string): Promise<void> {
    await this.ensureDBMigrateInit()
    if (n !== undefined) {
      this.dbmigrate.sync(n)
    } else {
      const migrationsInFolder = await this.retrieveInFolderMigrationList()
      const lastMigrationInFolderName = migrationsInFolder[migrationsInFolder.length - 1].substring(0, LENGTH_OF_MIGRATION_NAME)
      this.dbmigrate.sync(lastMigrationInFolderName)
    }
  }

  async retrieveInFolderMigrationList (): Promise<string[]> {
    let result: string[] = []
    const listOfFiles: string[] = fs.readdirSync(this.migrationsPath)
    for (let filename of listOfFiles) {
      const isDir = fs.statSync(this.migrationsPath + filename).isDirectory()
      if (!isDir) {
        result.push(filename.slice(0, -3))
      }
    }
    result.sort()
    return result
  }

  async ensureDBMigrateInit (): Promise<void> {
    try {
      this.DBMigrate = await import('db-migrate')
      // tslint:disable-next-line:no-empty
    } catch (error) {
    } finally {
      this.dbmigrate = this.DBMigrate.getInstance(true, this.dbMigrateConfig)
    }
  }
}

{'fraction_non_alphanumeric': 0.12032993692382339, 'fraction_numerical': 0.002911208151382824, 'mean_word_length': 2.533714285714286, 'pattern_counts': {'":': 0, '<': 13, '<?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': '358107', 'n_tokens_mistral': 1833, 'n_tokens_neox': 1880, 'n_words': 352}