2025-12-10 16:54:08 +03:30
|
|
|
import 'dotenv/config'
|
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import mysql from 'mysql2/promise'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import { env } from 'prisma/config'
|
|
|
|
|
|
|
|
|
|
const OUTPUT_DIR = path.join(process.cwd(), 'prisma', 'triggers')
|
|
|
|
|
|
|
|
|
|
async function main() {
|
|
|
|
|
const conn = await mysql.createConnection({
|
|
|
|
|
host: env('DATABASE_HOST'),
|
|
|
|
|
user: env('DATABASE_USER'),
|
|
|
|
|
password: env('DATABASE_PASSWORD'),
|
|
|
|
|
database: env('DATABASE_NAME'),
|
|
|
|
|
port: Number(env('DATABASE_PORT')) || 3306,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
console.log('📌 Fetching triggers...')
|
|
|
|
|
const [triggers] = (await conn.execute('SHOW TRIGGERS')) as [Array<any>, any]
|
|
|
|
|
|
2025-12-16 10:03:49 +03:30
|
|
|
console.log(triggers.length)
|
|
|
|
|
|
2025-12-10 16:54:08 +03:30
|
|
|
if (triggers.length === 0) {
|
|
|
|
|
console.log('⚠️ No triggers found in the database.')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Ensure directory exists
|
|
|
|
|
fs.mkdirSync(OUTPUT_DIR, { recursive: true })
|
|
|
|
|
|
|
|
|
|
const dumpFile = path.join(OUTPUT_DIR, `dump_triggers.sql`)
|
|
|
|
|
|
|
|
|
|
let sqlOutput = '-- AUTO-GENERATED MYSQL TRIGGER DUMP\n'
|
|
|
|
|
sqlOutput += '-- Generated at: ' + new Date().toISOString() + '\n\n'
|
|
|
|
|
|
2026-01-04 13:45:26 +03:30
|
|
|
for (const [index, trg] of triggers.entries()) {
|
2025-12-10 16:54:08 +03:30
|
|
|
const name = trg.Trigger
|
|
|
|
|
console.log(`📥 Extracting trigger: ${name}`)
|
|
|
|
|
|
|
|
|
|
const [rows] = await conn.execute(`SHOW CREATE TRIGGER \`${name}\``)
|
|
|
|
|
|
|
|
|
|
const createStatement = rows[0]['SQL Original Statement']
|
|
|
|
|
|
|
|
|
|
sqlOutput += `-- ------------------------------------------\n`
|
2026-01-04 13:45:26 +03:30
|
|
|
sqlOutput += `-- index: ${index + 1}\n`
|
2025-12-10 16:54:08 +03:30
|
|
|
sqlOutput += `-- Trigger: ${name}\n`
|
|
|
|
|
sqlOutput += `-- Event: ${trg.Event}\n`
|
|
|
|
|
sqlOutput += `-- Table: ${trg.Table}\n`
|
|
|
|
|
sqlOutput += `-- ------------------------------------------\n`
|
|
|
|
|
sqlOutput += `DROP TRIGGER IF EXISTS \`${name}\`;\n`
|
|
|
|
|
sqlOutput += `${createStatement};\n\n`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.writeFileSync(dumpFile, sqlOutput)
|
|
|
|
|
|
|
|
|
|
console.log('✅ Triggers exported to:')
|
|
|
|
|
console.log(` ${dumpFile}`)
|
|
|
|
|
|
|
|
|
|
await conn.end()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
main().catch(err => {
|
|
|
|
|
console.error(err)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
})
|