{"version":3,"file":"index.mjs","sources":["../../../src/dialects/mysql/index.ts"],"sourcesContent":["import type { Knex } from 'knex';\n\nimport Dialect from '../dialect';\nimport MysqlSchemaInspector from './schema-inspector';\nimport MysqlDatabaseInspector from './database-inspector';\nimport type { Database } from '../..';\n\nimport type { Information } from './database-inspector';\n\nexport default class MysqlDialect extends Dialect {\n  schemaInspector: MysqlSchemaInspector;\n\n  databaseInspector: MysqlDatabaseInspector;\n\n  info: Information | null = null;\n\n  constructor(db: Database) {\n    super(db, 'mysql');\n\n    this.schemaInspector = new MysqlSchemaInspector(db);\n    this.databaseInspector = new MysqlDatabaseInspector(db);\n  }\n\n  configure(conn?: Knex.MySqlConnectionConfig) {\n    const connection = conn || (this.db.config.connection.connection as Knex.MySqlConnectionConfig);\n\n    connection.supportBigNumbers = true;\n    // Only allow bigNumberStrings option set to be true if no connection option passed\n    // Otherwise bigNumberStrings option should be allowed to used from DB config\n    if (connection.bigNumberStrings === undefined) {\n      connection.bigNumberStrings = true;\n    }\n    connection.typeCast = (\n      field: { type: string; string: () => string; length: number },\n      next: () => void\n    ) => {\n      if (field.type === 'DECIMAL' || field.type === 'NEWDECIMAL') {\n        const value = field.string();\n        return value === null ? null : Number(value);\n      }\n\n      if (field.type === 'TINY' && field.length === 1) {\n        const value = field.string();\n        return value ? value === '1' : null;\n      }\n\n      if (field.type === 'DATE') {\n        return field.string();\n      }\n\n      return next();\n    };\n  }\n\n  async initialize(nativeConnection: unknown) {\n    try {\n      await this.db.connection\n        .raw(`set session sql_require_primary_key = 0;`)\n        .connection(nativeConnection);\n    } catch {\n      // Ignore error due to lack of session permissions\n    }\n\n    // We only need to get info on the first connection in the pool\n    /**\n     * Note: There is a race condition here where if two connections are opened at the same time, both will retrieve\n     * db info, but it doesn't cause issues, it's just one wasted query one time, so we can safely leave it to avoid\n     * adding extra complexity\n     * */\n    if (!this.info) {\n      this.info = await this.databaseInspector.getInformation(nativeConnection);\n    }\n  }\n\n  async startSchemaUpdate() {\n    try {\n      await this.db.connection.raw(`set foreign_key_checks = 0;`);\n      await this.db.connection.raw(`set session sql_require_primary_key = 0;`);\n    } catch {\n      // Ignore error due to lack of session permissions\n    }\n  }\n\n  async endSchemaUpdate() {\n    await this.db.connection.raw(`set foreign_key_checks = 1;`);\n  }\n\n  supportsUnsigned() {\n    return true;\n  }\n\n  usesForeignKeys() {\n    return true;\n  }\n\n  /**\n   * Drops the foreign-key constraint named `name` on `table` (MySQL FK names\n   * are unique per database, so a stale one blocks re-creating it on any\n   * table), then the index of the same name. Runs on `trx`.\n   */\n  async dropSchemaObject(\n    trx: Knex,\n    { table, name }: { table: string; name: string }\n  ): Promise<boolean> {\n    let dropped = false;\n\n    const [foreignKeys] = await trx.raw(\n      `SELECT 1\n       FROM information_schema.table_constraints\n       WHERE table_schema = database() AND table_name = ? AND constraint_name = ?\n       AND constraint_type = 'FOREIGN KEY'\n       LIMIT 1`,\n      [table, name]\n    );\n    if (foreignKeys.length > 0) {\n      await trx.raw('ALTER TABLE ?? DROP FOREIGN KEY ??', [table, name]);\n      dropped = true;\n    }\n\n    const [indexes] = await trx.raw(\n      `SELECT 1\n       FROM information_schema.statistics\n       WHERE table_schema = database() AND table_name = ? AND index_name = ?\n       LIMIT 1`,\n      [table, name]\n    );\n    if (indexes.length > 0) {\n      await trx.raw('ALTER TABLE ?? DROP INDEX ??', [table, name]);\n      dropped = true;\n    }\n\n    return dropped;\n  }\n\n  transformErrors(error: Error) {\n    super.transformErrors(error);\n  }\n}\n"],"names":["MysqlDialect","Dialect","configure","conn","connection","db","config","supportBigNumbers","bigNumberStrings","undefined","typeCast","field","next","type","value","string","Number","length","initialize","nativeConnection","raw","info","databaseInspector","getInformation","startSchemaUpdate","endSchemaUpdate","supportsUnsigned","usesForeignKeys","dropSchemaObject","trx","table","name","dropped","foreignKeys","indexes","transformErrors","error","schemaInspector","MysqlSchemaInspector","MysqlDatabaseInspector"],"mappings":";;;;AASe,MAAMA,YAAAA,SAAqBC,OAAAA,CAAAA;AA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