feat(database): initialize database schema and triggers
- Created initial database schema with tables for Users, Roles, Products, and related entities. - Added foreign key constraints to maintain data integrity across tables. - Implemented triggers for managing stock movements on insert, update, and delete operations. - Developed scripts to dump existing triggers and pull trigger definitions from the database.
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
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]
|
||||
|
||||
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'
|
||||
|
||||
for (const trg of triggers) {
|
||||
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`
|
||||
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)
|
||||
})
|
||||
@@ -0,0 +1,24 @@
|
||||
import 'dotenv/config'
|
||||
import mysql from 'mysql2/promise'
|
||||
import { env } from 'prisma/config'
|
||||
;(async () => {
|
||||
const db = 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,
|
||||
})
|
||||
|
||||
const [rows] = await db.query(`
|
||||
SELECT
|
||||
TRIGGER_NAME,
|
||||
ACTION_STATEMENT
|
||||
FROM INFORMATION_SCHEMA.TRIGGERS
|
||||
WHERE TRIGGER_SCHEMA = DATABASE();
|
||||
`)
|
||||
|
||||
console.log(rows)
|
||||
|
||||
await db.end()
|
||||
})()
|
||||
Reference in New Issue
Block a user