Sindbad~EG File Manager
const { exec } = require("child_process");
// Function to create a new migration
function createNewMigration(migrationName) {
if (!migrationName) {
throw new Error("A migration name must be provided");
}
// Replace any spaces with underscores (_) and remove special characters
const cleanedName = migrationName
.replace(/\s+/g, "_")
.replace(/[^a-zA-Z0-9_]/g, "");
const command = `npx sequelize-cli migration:generate --name ${cleanedName}`;
return executeCommand(command, "Create Migration");
}
// Function to run migrations
function runMigrations() {
return executeCommand("npx sequelize-cli db:migrate", "Migration");
}
// Function to undo the last migration
function undoLastMigration() {
return executeCommand("npx sequelize-cli db:migrate:undo", "Migration undo");
}
// Function to undo all migrations
function undoAllMigrations() {
return executeCommand(
"npx sequelize-cli db:migrate:undo:all",
"Full migration undo"
);
}
// General function to execute any Sequelize CLI command
function executeCommand(command, actionName) {
console.log(`${actionName} started...`);
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
console.error(`${actionName} error: ${error.message}`);
reject(error);
return;
}
if (stderr) {
console.error(`${actionName} stderr: ${stderr}`);
reject(new Error(stderr));
return;
}
console.log(`${actionName} output:\n${stdout}`);
resolve(stdout);
});
});
}
module.exports = {
runMigrations,
undoLastMigration,
undoAllMigrations,
createNewMigration,
};
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists