|
|
@ -12,9 +12,16 @@ if (!fs.existsSync(dbDir)) {
|
|
|
|
const dbPath = path.join(dbDir, 'bot.db');
|
|
|
|
const dbPath = path.join(dbDir, 'bot.db');
|
|
|
|
export const db = new Database(dbPath);
|
|
|
|
export const db = new Database(dbPath);
|
|
|
|
|
|
|
|
|
|
|
|
// Run the schema script
|
|
|
|
// Run the schema script with error handling
|
|
|
|
|
|
|
|
try {
|
|
|
|
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
|
|
|
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
|
|
|
|
|
|
|
|
db.exec('PRAGMA foreign_keys=OFF;'); // Disable foreign keys during schema changes
|
|
|
|
db.exec(schema);
|
|
|
|
db.exec(schema);
|
|
|
|
|
|
|
|
db.exec('PRAGMA foreign_keys=ON;'); // Re-enable foreign keys
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
|
|
console.error('Error initializing database schema:', error);
|
|
|
|
|
|
|
|
process.exit(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helper function to query the database
|
|
|
|
// Helper function to query the database
|
|
|
|
export function query(sql: string, params: any[] = []) {
|
|
|
|
export function query(sql: string, params: any[] = []) {
|
|
|
|