51 lines
1.7 KiB
TypeScript
51 lines
1.7 KiB
TypeScript
|
|
import { Pool } from 'pg';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
|
|
// Load env vars from .env file in backend root
|
|
dotenv.config({ path: path.join(__dirname, '../../.env') });
|
|
|
|
const pool = new Pool({
|
|
connectionString: process.env.DATABASE_URL,
|
|
});
|
|
|
|
async function fixSchema() {
|
|
console.log('🔧 Fixing schema...');
|
|
const client = await pool.connect();
|
|
try {
|
|
// Add seo_keywords column
|
|
console.log('Adding seo_keywords column...');
|
|
await client.query(`
|
|
ALTER TABLE monitors ADD COLUMN IF NOT EXISTS seo_keywords JSONB;
|
|
`);
|
|
|
|
// Create monitor_rankings table
|
|
console.log('Creating monitor_rankings table...');
|
|
await client.query(`
|
|
CREATE TABLE IF NOT EXISTS monitor_rankings (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
monitor_id UUID NOT NULL REFERENCES monitors(id) ON DELETE CASCADE,
|
|
keyword VARCHAR(255) NOT NULL,
|
|
rank INTEGER,
|
|
url_found TEXT,
|
|
created_at TIMESTAMP DEFAULT NOW()
|
|
);
|
|
`);
|
|
|
|
// Create indexes for monitor_rankings
|
|
await client.query(`CREATE INDEX IF NOT EXISTS idx_monitor_rankings_monitor_id ON monitor_rankings(monitor_id);`);
|
|
await client.query(`CREATE INDEX IF NOT EXISTS idx_monitor_rankings_keyword ON monitor_rankings(keyword);`);
|
|
await client.query(`CREATE INDEX IF NOT EXISTS idx_monitor_rankings_created_at ON monitor_rankings(created_at);`);
|
|
|
|
console.log('✅ Schema fixed successfully!');
|
|
} catch (err) {
|
|
console.error('❌ Schema fix failed:', err);
|
|
} finally {
|
|
client.release();
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
fixSchema();
|