48 lines
1.7 KiB
JavaScript
48 lines
1.7 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
require('dotenv').config({ path: path.join(__dirname, '../.env') });
|
|
const db = require('../db');
|
|
|
|
async function initDb() {
|
|
try {
|
|
const schemaPath = path.join(__dirname, '../schema.sql');
|
|
const schemaSql = fs.readFileSync(schemaPath, 'utf8');
|
|
|
|
console.log('Dropping existing tables...');
|
|
await db.query(`
|
|
DROP TABLE IF EXISTS invites CASCADE;
|
|
DROP TABLE IF EXISTS messages CASCADE;
|
|
DROP TABLE IF EXISTS channel_keys CASCADE;
|
|
DROP TABLE IF EXISTS roles CASCADE;
|
|
DROP TABLE IF EXISTS channels CASCADE;
|
|
DROP TABLE IF EXISTS users CASCADE;
|
|
`);
|
|
|
|
console.log('Applying schema...');
|
|
await db.query(schemaSql);
|
|
|
|
console.log('Schema applied successfully.');
|
|
|
|
console.log('Seeding Default Roles...');
|
|
// 1. @everyone (Limited)
|
|
await db.query(`
|
|
INSERT INTO roles (name, color, position, is_hoist, permissions)
|
|
VALUES ('@everyone', '#99aab5', 0, false, '{"manage_channels": false, "manage_roles": false, "create_invite": false, "embed_links": true, "attach_files": true}')
|
|
`);
|
|
|
|
// 2. Owner (All Permissions)
|
|
await db.query(`
|
|
INSERT INTO roles (name, color, position, is_hoist, permissions)
|
|
VALUES ('Owner', '#ED4245', 100, true, '{"manage_channels": true, "manage_roles": true, "create_invite": true, "embed_links": true, "attach_files": true}')
|
|
`);
|
|
|
|
console.log('Schema applied successfully.');
|
|
process.exit(0);
|
|
} catch (err) {
|
|
console.error('Error applying schema:', err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
initDb();
|