tududi/backend/scripts/reset-and-seed.js
Chris 542be2c1e9
Fix bug 366 (#764)
* Optimize DB

* Clean up names

* fixup! Clean up names

* fixup! fixup! Clean up names
2026-01-07 18:18:07 +02:00

92 lines
3.1 KiB
JavaScript
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const { getConfig } = require('../config/config');
const { sequelize } = require('../models');
/**
* Reset database and seed with comprehensive test data
* Run with: NODE_ENV=development node backend/scripts/reset-and-seed.js
*/
const config = getConfig();
console.log('🔄 Starting database reset and seed...\n');
console.log(`📁 Database: ${config.dbFile}`);
console.log(`🌍 Environment: ${config.environment}\n`);
if (config.environment === 'production') {
console.error(
'❌ ERROR: Cannot run this script in production environment!'
);
process.exit(1);
}
async function main() {
try {
// Step 1: Delete existing database file
console.log('1⃣ Removing existing database...');
if (fs.existsSync(config.dbFile)) {
fs.unlinkSync(config.dbFile);
console.log(' ✅ Database removed\n');
} else {
console.log(' No existing database found\n');
}
// Step 2: Reset database using sequelize.sync
console.log('2⃣ Creating fresh database...');
await sequelize.sync({ force: true });
console.log(' ✅ Database created\n');
// Step 3: Seed basic development data
console.log('3⃣ Seeding basic development data...');
const { seedDatabase } = require('../seeders/dev-seeder');
await seedDatabase();
console.log(' ✅ Basic data seeded\n');
// Step 4: Generate notifications
console.log('4⃣ Generating notifications...');
const { checkDueTasks } = require('../modules/tasks/dueTaskService');
const {
checkDeferredTasks,
} = require('../modules/tasks/deferredTaskService');
const {
checkDueProjects,
} = require('../modules/projects/dueProjectService');
const dueTasksResult = await checkDueTasks();
const deferredTasksResult = await checkDeferredTasks();
const dueProjectsResult = await checkDueProjects();
const total =
dueTasksResult.notificationsCreated +
deferredTasksResult.notificationsCreated +
dueProjectsResult.notificationsCreated;
console.log(` ✅ Generated ${total} notifications\n`);
// Final summary
console.log('✅ Database reset and seed completed successfully!\n');
console.log('📊 Summary:');
console.log(' • Database: Fresh and ready');
console.log(' • Users: Test users created');
console.log(' • Tasks: Sample tasks with various due dates');
console.log(' • Projects: Sample projects with various due dates');
console.log(` • Notifications: ${total} notifications generated`);
console.log('\n🚀 You can now start the application with:');
console.log(' npm start\n');
await sequelize.close();
process.exit(0);
} catch (error) {
console.error('❌ Error:', error.message);
console.error(error);
await sequelize.close();
process.exit(1);
}
}
// Run the main function
main();