tables scripts

This commit is contained in:
2026-02-22 10:18:15 +05:30
parent bd650a8276
commit 70997efc46
5 changed files with 122 additions and 11 deletions

View File

@@ -0,0 +1,35 @@
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();