diff --git a/server/index.js b/server/index.js index 0cb51de..d40e4f6 100644 --- a/server/index.js +++ b/server/index.js @@ -12,6 +12,7 @@ const { sequelize } = require('./models'); const routes = require('./routes'); const { initializeSocketHandlers } = require('./services/socketService'); const AlertService = require('./services/alertService'); +const seedDatabase = require('./seedDatabase'); const errorHandler = require('./middleware/errorHandler'); const app = express(); @@ -76,9 +77,23 @@ async function startServer() { await sequelize.authenticate(); console.log('Database connected successfully.'); - if (process.env.NODE_ENV !== 'production') { + // Always sync database in containerized environments or development + // Check if tables exist before syncing + try { await sequelize.sync({ alter: true }); console.log('Database synchronized.'); + + // Seed database with initial data + await seedDatabase(); + } catch (syncError) { + console.error('Database sync error:', syncError); + // If sync fails, try force sync (this will drop and recreate tables) + console.log('Attempting force sync...'); + await sequelize.sync({ force: false, alter: true }); + console.log('Database force synchronized.'); + + // Seed database with initial data + await seedDatabase(); } server.listen(PORT, () => { diff --git a/server/seedDatabase.js b/server/seedDatabase.js new file mode 100644 index 0000000..add7d35 --- /dev/null +++ b/server/seedDatabase.js @@ -0,0 +1,61 @@ +const bcrypt = require('bcryptjs'); +const { User, Device } = require('./models'); + +async function seedDatabase() { + try { + console.log('🌱 Seeding database...'); + + // Check if admin user exists + const existingAdmin = await User.findOne({ where: { username: 'admin' } }); + + if (!existingAdmin) { + // Create default admin user + const adminPassword = await bcrypt.hash('admin123', 10); + + await User.create({ + username: 'admin', + email: 'admin@dronedetection.local', + password_hash: adminPassword, + first_name: 'System', + last_name: 'Administrator', + role: 'admin', + is_active: true, + sms_alerts_enabled: false, + email_alerts_enabled: false, + timezone: 'Europe/Stockholm' + }); + + console.log('✅ Admin user created (username: admin, password: admin123)'); + } else { + console.log('✅ Admin user already exists'); + } + + // Create a sample device if none exist + const deviceCount = await Device.count(); + if (deviceCount === 0) { + await Device.create({ + device_id: 1001, + name: 'Drone Detector Alpha', + location: 'Stockholm Central', + geo_lat: 59.3293, + geo_lon: 18.0686, + is_active: true, + detection_range: 15000, // 15km + frequency_bands: ['2.4GHz', '5.8GHz'], + device_type: 'RF_SCANNER', + last_heartbeat: new Date() + }); + + console.log('✅ Sample device created'); + } else { + console.log('✅ Devices already exist'); + } + + console.log('🌱 Database seeding completed'); + } catch (error) { + console.error('❌ Database seeding failed:', error); + throw error; + } +} + +module.exports = seedDatabase;