webupdate
This commit is contained in:
@@ -1,64 +1,51 @@
|
||||
#!/usr/bin/env node
|
||||
const { pool, query } = require("./config/database");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
async function applyMigration() {
|
||||
console.log("🔧 Applying Database Fixes...\n");
|
||||
const { query, pool } = require('./config/database');
|
||||
const fs = require('fs');
|
||||
|
||||
async function applyFixes() {
|
||||
console.log('🔧 Applying database fixes...\n');
|
||||
|
||||
try {
|
||||
// Read the migration file
|
||||
const migrationPath = path.join(
|
||||
__dirname,
|
||||
"migrations",
|
||||
"006_database_fixes.sql"
|
||||
);
|
||||
const migrationSQL = fs.readFileSync(migrationPath, "utf8");
|
||||
|
||||
console.log("📄 Running migration: 006_database_fixes.sql");
|
||||
console.log("─".repeat(60));
|
||||
|
||||
// Execute the migration
|
||||
await query(migrationSQL);
|
||||
|
||||
console.log("\n✅ Migration applied successfully!");
|
||||
console.log("\n📊 Verification:");
|
||||
console.log("─".repeat(60));
|
||||
|
||||
// Verify the changes
|
||||
const fkResult = await query(`
|
||||
SELECT COUNT(*) as fk_count
|
||||
FROM information_schema.table_constraints
|
||||
WHERE constraint_type = 'FOREIGN KEY'
|
||||
AND table_schema = 'public'
|
||||
`);
|
||||
console.log(` Foreign keys: ${fkResult.rows[0].fk_count}`);
|
||||
|
||||
const indexResult = await query(`
|
||||
SELECT COUNT(*) as index_count
|
||||
FROM pg_indexes
|
||||
const sql = fs.readFileSync('./fix-database-issues.sql', 'utf8');
|
||||
|
||||
console.log('Executing SQL fixes...');
|
||||
await query(sql);
|
||||
|
||||
console.log('\n✅ All fixes applied successfully!\n');
|
||||
|
||||
// Verify the fixes
|
||||
console.log('📊 Verifying fixes:');
|
||||
|
||||
// Count indexes
|
||||
const indexes = await query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM pg_indexes
|
||||
WHERE schemaname = 'public'
|
||||
AND tablename IN ('products', 'product_images', 'portfolioprojects', 'blogposts', 'pages')
|
||||
`);
|
||||
console.log(` Indexes (main tables): ${indexResult.rows[0].index_count}`);
|
||||
|
||||
const uniqueResult = await query(`
|
||||
SELECT COUNT(*) as unique_count
|
||||
FROM information_schema.table_constraints
|
||||
WHERE constraint_type = 'UNIQUE'
|
||||
AND table_schema = 'public'
|
||||
AND table_name IN ('products', 'blogposts', 'pages')
|
||||
console.log(` • Total indexes: ${indexes.rows[0].count}`);
|
||||
|
||||
// Check constraints
|
||||
const constraints = await query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_schema = 'public'
|
||||
`);
|
||||
console.log(` Unique constraints: ${uniqueResult.rows[0].unique_count}`);
|
||||
|
||||
console.log("\n✅ Database fixes complete!");
|
||||
console.log(` • Total constraints: ${constraints.rows[0].count}`);
|
||||
|
||||
// Check triggers
|
||||
const triggers = await query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM information_schema.triggers
|
||||
WHERE trigger_schema = 'public'
|
||||
`);
|
||||
console.log(` • Total triggers: ${triggers.rows[0].count}`);
|
||||
|
||||
console.log('\n🎉 Database optimization complete!\n');
|
||||
process.exit(0);
|
||||
} catch (error) {
|
||||
console.error("❌ Error applying migration:", error.message);
|
||||
console.error(error);
|
||||
console.error('❌ Error applying fixes:', error.message);
|
||||
console.error('Details:', error);
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await pool.end();
|
||||
}
|
||||
}
|
||||
|
||||
applyMigration();
|
||||
applyFixes();
|
||||
|
||||
Reference in New Issue
Block a user