first commit
This commit is contained in:
31
Backend/scripts/init-db.js
Normal file
31
Backend/scripts/init-db.js
Normal file
@@ -0,0 +1,31 @@
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const db = require('../db');
|
||||
|
||||
async function initDb() {
|
||||
try {
|
||||
const schemaPath = path.join(__dirname, '../schema.sql');
|
||||
const schemaSql = fs.readFileSync(schemaPath, 'utf8');
|
||||
|
||||
console.log('Applying schema...');
|
||||
await db.query(schemaSql);
|
||||
|
||||
// Seed Channels
|
||||
const channels = ['general', 'random'];
|
||||
for (const name of channels) {
|
||||
await db.query(
|
||||
`INSERT INTO channels (name) VALUES ($1) ON CONFLICT (name) DO NOTHING`,
|
||||
[name]
|
||||
);
|
||||
}
|
||||
console.log('Channels seeded.');
|
||||
|
||||
console.log('Schema applied successfully.');
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
console.error('Error applying schema:', err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
initDb();
|
||||
Reference in New Issue
Block a user