38 lines
1.0 KiB
JavaScript
38 lines
1.0 KiB
JavaScript
const { Pool } = require('pg');
|
|
require('dotenv').config();
|
|
|
|
const createTableQuery = `
|
|
CREATE TABLE IF NOT EXISTS participants (
|
|
id SERIAL PRIMARY KEY,
|
|
user_id INT UNIQUE REFERENCES users(id) ON DELETE SET NULL,
|
|
email VARCHAR(255) UNIQUE NOT NULL,
|
|
participant_name VARCHAR(255) NOT NULL,
|
|
year INT CHECK (year BETWEEN 1 AND 4),
|
|
department VARCHAR(255),
|
|
phone_number VARCHAR(15),
|
|
created_at TIMESTAMP DEFAULT NOW()
|
|
);
|
|
`;
|
|
|
|
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 participants table if it does not exist...');
|
|
await client.query(createTableQuery);
|
|
|
|
console.log('Participants table created or already exists.');
|
|
client.release();
|
|
} catch (error) {
|
|
console.error('Error creating participants table:', error);
|
|
} finally {
|
|
await pool.end();
|
|
}
|
|
}
|
|
|
|
createTable(); |