Files
ArtSplash/scripts/create_votes_table.js
2026-02-22 10:18:15 +05:30

35 lines
985 B
JavaScript

const { Pool } = require('pg');
require('dotenv').config();
const createTableQuery = `
CREATE TABLE IF NOT EXISTS votes (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
artwork_id INT NOT NULL REFERENCES artworks(id) ON DELETE CASCADE,
created_at TIMESTAMP DEFAULT NOW(),
CONSTRAINT unique_user_artwork_vote UNIQUE (user_id, artwork_id)
);
`;
async function createTable() {
const pool = new Pool({
connectionString: process.env.DATABASE_URL,
});
try {
console.log('Connecting to the database...');
const client = await pool.connect();
console.log('Creating votes table if it does not exist...');
await client.query(createTableQuery);
console.log('Votes table created or already exists.');
client.release();
} catch (error) {
console.error('Error creating votes table:', error);
} finally {
await pool.end();
}
}
createTable();