78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
const mysql = require('mysql2/promise');
|
|
const bcrypt = require('bcryptjs');
|
|
require('dotenv').config();
|
|
|
|
async function setup() {
|
|
try {
|
|
const pool = mysql.createPool({
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.DB_NAME,
|
|
});
|
|
|
|
console.log('Connected to MySQL...');
|
|
|
|
// Create admin_users table
|
|
await pool.execute(`
|
|
CREATE TABLE IF NOT EXISTS admin_users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
email VARCHAR(255) NOT NULL UNIQUE,
|
|
password_hash VARCHAR(255) NOT NULL,
|
|
role VARCHAR(50) DEFAULT 'admin',
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
console.log('Verified admin_users table exists.');
|
|
|
|
// Create events table
|
|
await pool.execute(`
|
|
CREATE TABLE IF NOT EXISTS events (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
year INT NOT NULL,
|
|
eventdate VARCHAR(100),
|
|
eventtitle VARCHAR(255) NOT NULL,
|
|
eventimageurl TEXT,
|
|
eventdescription TEXT,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
)
|
|
`);
|
|
console.log('Verified events table exists.');
|
|
|
|
// Create event_images table
|
|
await pool.execute(`
|
|
CREATE TABLE IF NOT EXISTS event_images (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
eventid INT,
|
|
imageurl TEXT NOT NULL,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (eventid) REFERENCES events(id) ON DELETE CASCADE
|
|
)
|
|
`);
|
|
console.log('Verified event_images table exists.');
|
|
|
|
// Check if admin user exists
|
|
const [rows] = await pool.execute('SELECT * FROM admin_users WHERE email = ?', ['admin@example.com']);
|
|
|
|
if (rows.length === 0) {
|
|
const passwordStr = 'adminpassword';
|
|
const hash = await bcrypt.hash(passwordStr, 10);
|
|
|
|
await pool.execute('INSERT INTO admin_users (email, password_hash, role) VALUES (?, ?, ?)', [
|
|
'admin@example.com', hash, 'admin'
|
|
]);
|
|
console.log('Default admin user created: admin@example.com / adminpassword');
|
|
} else {
|
|
console.log('Default admin user already exists.');
|
|
}
|
|
|
|
console.log('Database setup complete.');
|
|
process.exit(0);
|
|
} catch (err) {
|
|
console.error('Setup failed:', err);
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
setup();
|