Added `README.md` to document the database migration system, including generation and usage instructions. Updated `generate-migrations.ts` to fix directory structure and streamline commands. Included initial migration files for schema setup using DrizzleORM.
56 lines
1.5 KiB
TypeScript
56 lines
1.5 KiB
TypeScript
import { exec } from 'child_process';
|
|
import * as path from 'path';
|
|
import * as fs from 'fs';
|
|
|
|
/**
|
|
* Script to generate migrations using drizzle-kit
|
|
*
|
|
* This script:
|
|
* 1. Runs drizzle-kit generate to create migration files
|
|
* 2. Ensures the migrations directory exists
|
|
* 3. Handles errors and provides feedback
|
|
*/
|
|
const main = async () => {
|
|
console.log('Generating migrations...');
|
|
|
|
// Ensure migrations directory exists
|
|
const migrationsDir = path.join(__dirname, 'sql');
|
|
if (!fs.existsSync(migrationsDir)) {
|
|
fs.mkdirSync(migrationsDir, { recursive: true });
|
|
}
|
|
|
|
// Run drizzle-kit generate command
|
|
const command = 'npx drizzle-kit generate';
|
|
|
|
exec(command, (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.error(`Error generating migrations: ${error.message}`);
|
|
return;
|
|
}
|
|
|
|
if (stderr) {
|
|
console.error(`Migration generation stderr: ${stderr}`);
|
|
}
|
|
|
|
console.log(stdout);
|
|
console.log('Migrations generated successfully');
|
|
|
|
// List generated migration files
|
|
const files = fs.readdirSync(migrationsDir)
|
|
.filter(file => file.endsWith('.sql'));
|
|
|
|
if (files.length === 0) {
|
|
console.log('No migration files were generated. Your schema might be up to date.');
|
|
} else {
|
|
console.log('Generated migration files:');
|
|
files.forEach(file => console.log(`- ${file}`));
|
|
}
|
|
});
|
|
};
|
|
|
|
main().catch(err => {
|
|
console.error('Migration generation failed');
|
|
console.error(err);
|
|
process.exit(1);
|
|
});
|