spurrin-backend/src/migrations/createMigration.js
2025-06-09 11:11:52 +05:30

47 lines
1.2 KiB
JavaScript

const fs = require('fs').promises;
const path = require('path');
async function createMigration() {
try {
const name = process.argv[2];
if (!name) {
console.error('Please provide a migration name');
console.log('Usage: node createMigration.js <migration_name>');
process.exit(1);
}
const timestamp = new Date().toISOString().replace(/[-:]/g, '').split('.')[0];
const fileName = `${timestamp}_${name}.js`;
const filePath = path.join(__dirname, 'migrations', fileName);
const template = `const db = require('../../config/database');
module.exports = {
async up() {
// Add your migration SQL here
// Example:
// await db.query(\`
// CREATE TABLE IF NOT EXISTS table_name (
// id INT AUTO_INCREMENT PRIMARY KEY,
// name VARCHAR(255) NOT NULL,
// created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
// )
// \`);
},
async down() {
// Add your rollback SQL here
// Example:
// await db.query('DROP TABLE IF EXISTS table_name');
}
};`;
await fs.writeFile(filePath, template);
console.log(`Created migration file: ${fileName}`);
} catch (error) {
console.error('Error creating migration:', error);
process.exit(1);
}
}
createMigration();