Add reset database script and frontend
This commit is contained in:
125
inventory-server/scripts/reset-db.js
Normal file
125
inventory-server/scripts/reset-db.js
Normal file
@@ -0,0 +1,125 @@
|
||||
const mysql = require('mysql2/promise');
|
||||
const path = require('path');
|
||||
const dotenv = require('dotenv');
|
||||
const { spawn } = require('child_process');
|
||||
|
||||
dotenv.config({ path: path.join(__dirname, '../.env') });
|
||||
|
||||
const dbConfig = {
|
||||
host: process.env.DB_HOST,
|
||||
user: process.env.DB_USER,
|
||||
password: process.env.DB_PASSWORD,
|
||||
database: process.env.DB_NAME,
|
||||
multipleStatements: true
|
||||
};
|
||||
|
||||
// Helper function to output progress in JSON format
|
||||
function outputProgress(data) {
|
||||
if (!data.status) {
|
||||
data = {
|
||||
status: 'running',
|
||||
...data
|
||||
};
|
||||
}
|
||||
console.log(JSON.stringify(data));
|
||||
}
|
||||
|
||||
async function resetDatabase() {
|
||||
outputProgress({
|
||||
operation: 'Starting database reset',
|
||||
message: 'Connecting to database...'
|
||||
});
|
||||
|
||||
const connection = await mysql.createConnection(dbConfig);
|
||||
|
||||
try {
|
||||
// Get list of all tables
|
||||
outputProgress({
|
||||
operation: 'Getting table list',
|
||||
message: 'Retrieving all table names...'
|
||||
});
|
||||
|
||||
const [tables] = await connection.query(
|
||||
'SELECT table_name FROM information_schema.tables WHERE table_schema = ?',
|
||||
[dbConfig.database]
|
||||
);
|
||||
|
||||
if (tables.length === 0) {
|
||||
outputProgress({
|
||||
operation: 'No tables found',
|
||||
message: 'Database is already empty'
|
||||
});
|
||||
} else {
|
||||
// Disable foreign key checks to allow dropping tables with dependencies
|
||||
await connection.query('SET FOREIGN_KEY_CHECKS = 0');
|
||||
|
||||
// Drop each table
|
||||
for (let i = 0; i < tables.length; i++) {
|
||||
const tableName = tables[i].TABLE_NAME;
|
||||
outputProgress({
|
||||
operation: 'Dropping tables',
|
||||
message: `Dropping table: ${tableName}`,
|
||||
current: i + 1,
|
||||
total: tables.length,
|
||||
percentage: (((i + 1) / tables.length) * 100).toFixed(1)
|
||||
});
|
||||
await connection.query(`DROP TABLE IF EXISTS \`${tableName}\``);
|
||||
}
|
||||
|
||||
// Re-enable foreign key checks
|
||||
await connection.query('SET FOREIGN_KEY_CHECKS = 1');
|
||||
}
|
||||
|
||||
// Run setup-db.js
|
||||
outputProgress({
|
||||
operation: 'Running database setup',
|
||||
message: 'Creating new tables...'
|
||||
});
|
||||
|
||||
const setupScript = path.join(__dirname, 'setup-db.js');
|
||||
const setupProcess = spawn('node', [setupScript]);
|
||||
|
||||
setupProcess.stdout.on('data', (data) => {
|
||||
const output = data.toString().trim();
|
||||
outputProgress({
|
||||
operation: 'Database setup',
|
||||
message: output
|
||||
});
|
||||
});
|
||||
|
||||
setupProcess.stderr.on('data', (data) => {
|
||||
const error = data.toString().trim();
|
||||
outputProgress({
|
||||
status: 'error',
|
||||
error
|
||||
});
|
||||
});
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
setupProcess.on('close', (code) => {
|
||||
if (code === 0) {
|
||||
resolve();
|
||||
} else {
|
||||
reject(new Error(`Setup process exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
outputProgress({
|
||||
status: 'complete',
|
||||
operation: 'Database reset complete',
|
||||
message: 'Database has been reset and tables recreated'
|
||||
});
|
||||
} catch (error) {
|
||||
outputProgress({
|
||||
status: 'error',
|
||||
error: error.message
|
||||
});
|
||||
process.exit(1);
|
||||
} finally {
|
||||
await connection.end();
|
||||
}
|
||||
}
|
||||
|
||||
// Run the reset
|
||||
resetDatabase();
|
||||
Reference in New Issue
Block a user