Por alguna razón, no puedo usar las migraciones knex en el entorno de producción.
¿Puedo generar SQL sin procesar a partir de migraciones de knex como operaciones de selección/inserción/actualización de knex?
Por ejemplo:
Puedo obtener SQL sin procesar de oraciones seleccionadas con:
Knex('t_example').select('id').toQuery() // => select id from t_examplePero no sé cómo generar SQL sin formato a partir de los archivos de migración:
import { Knex } from 'knex' // how to get raw SQL like // "alter table `t_example` add `foo` int"; // from the "up" function export async function up (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.integer('foo') }) } export async function down (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.dropColumn('foo') }) }up (de esteproblema de github )algo como
const knex = require('knex')({...}); const {up, down} = require('./migrations/20200304140624_createUsersTable.js'); console.log(up(knex).toSQL().toNative()); console.log(down(knex).toSQL().toNative());El fragmento de código anterior funciona bien para algunas migraciones (createTable en mi caso), mientras que no funciona para otras (addColumn).
Por ejemplo, con este archivo de migración:
import { Knex } from 'knex' export async function up (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.integer('foo') }) } export async function down (knex: Knex): Promise<void> { return knex.schema.table('t_example', (table) => { table.dropColumn('foo') }) } Lanza un mensaje de error: TypeError: up(...).toSql is not a function
y
UnhandledPromiseRejectionWarning: Error: ER_DUP_FIELDNAME: Duplicate column name 'foo'
En primer lugar, deshaga las migraciones con knex migrate:rollback
Luego, ejecute las migraciones con el indicador DEBUG:
DEBUG=knex:query knex migrate:latestProduce registros SQL como
knex:query alter table `t_example` add `bar` int trx2 +8ms`Puedo obtener SQL de estas salidas ahora, pero requiere algunas tareas de formato adicionales.
¿Hay una solución mejor?