WIP
This commit is contained in:
52
backend/scripts/clear-data.js
Normal file
52
backend/scripts/clear-data.js
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* Clear all data from database tables while keeping the schema
|
||||
* This removes all users, bookmarks, and resets migrations
|
||||
*/
|
||||
|
||||
require('dotenv').config();
|
||||
const dbConnection = require('../src/database/connection');
|
||||
|
||||
async function clearAllData() {
|
||||
try {
|
||||
console.log('🧹 Clearing all data from database...');
|
||||
|
||||
// Connect to database
|
||||
await dbConnection.connect();
|
||||
|
||||
// Clear data in the correct order (respecting foreign key constraints)
|
||||
console.log('🗑️ Clearing bookmarks...');
|
||||
const bookmarksResult = await dbConnection.query('DELETE FROM bookmarks');
|
||||
console.log(` Removed ${bookmarksResult.rowCount} bookmarks`);
|
||||
|
||||
console.log('🗑️ Clearing users...');
|
||||
const usersResult = await dbConnection.query('DELETE FROM users');
|
||||
console.log(` Removed ${usersResult.rowCount} users`);
|
||||
|
||||
console.log('🗑️ Clearing migration history...');
|
||||
const migrationsResult = await dbConnection.query('DELETE FROM migrations');
|
||||
console.log(` Removed ${migrationsResult.rowCount} migration records`);
|
||||
|
||||
// Reset sequences to start from 1
|
||||
console.log('🔄 Resetting ID sequences...');
|
||||
await dbConnection.query('ALTER SEQUENCE users_id_seq RESTART WITH 1');
|
||||
await dbConnection.query('ALTER SEQUENCE bookmarks_id_seq RESTART WITH 1');
|
||||
await dbConnection.query('ALTER SEQUENCE migrations_id_seq RESTART WITH 1');
|
||||
|
||||
console.log('✅ All data cleared successfully');
|
||||
console.log('💡 Database schema is preserved - you can now add fresh data');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Failed to clear data:', error.message);
|
||||
throw error;
|
||||
} finally {
|
||||
await dbConnection.close();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the clear operation
|
||||
clearAllData().catch((error) => {
|
||||
console.error('❌ Clear data operation failed:', error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user