-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup-db.js
More file actions
52 lines (41 loc) · 1.47 KB
/
Copy pathsetup-db.js
File metadata and controls
52 lines (41 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import 'dotenv/config';
import pg from 'pg';
const { Pool } = pg;
// Configure connection using your existing .env variables
const pool = new Pool({
host: process.env.DB_HOST || 'localhost',
port: parseInt(process.env.DB_PORT || '5433', 10),
database: process.env.DB_NAME || 'job_queue',
user: process.env.DB_USER || 'job_user',
password: process.env.DB_PASSWORD || 'job_password',
});
const setupQuery = `
-- 1. Ensure the jobs table has a progress column
ALTER TABLE jobs ADD COLUMN IF NOT EXISTS progress INTEGER DEFAULT 0;
-- 2. Ensure we have an index on status for faster dashboard queries
CREATE INDEX IF NOT EXISTS idx_jobs_status ON jobs(status);
-- 3. (Optional) Verify the table structure
SELECT column_name, data_type
FROM information_schema.columns
WHERE table_name = 'jobs' AND column_name = 'progress';
`;
async function runSetup() {
console.log('🚀 Starting Database Schema Update...');
const client = await pool.connect();
try {
await client.query('BEGIN');
await client.query(setupQuery);
await client.query('COMMIT');
console.log('✅ Database schema updated successfully!');
console.log('👉 Added: "progress" column (Integer)');
console.log('👉 Added: "idx_jobs_status" index');
} catch (error) {
await client.query('ROLLBACK');
console.error('❌ Database setup failed:', error.message);
} finally {
client.release();
await pool.end();
process.exit(0);
}
}
runSetup();