Skip to content

Commit

Permalink
fix: Migration generator (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-smartive authored Sep 25, 2023
1 parent af7316f commit 30fa32b
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/bin/gqm/gqm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,27 @@ program
});

program
.command('generate-migration')
.command('generate-migration [<name>] [<date>]')
.description('Generate Migration')
.action(async () => {
.action(async (name, date) => {
const git = simpleGit();

let name = process.argv[process.argv.indexOf('gqm') + 1] || (await git.branch()).current.split('/').pop();
if (!name) {
name = (await git.branch()).current.split('/').pop();
}

if (name && ['staging', 'production'].includes(name)) {
if (!name || ['main', 'staging', 'production'].includes(name)) {
name = await readLine('Migration name:');
}

const knexfile = await parseKnexfile();
const db = knex(knexfile);

try {
const rawModels = await parseModels();
const migrations = await new MigrationGenerator(db, rawModels).generate();
const models = await parseModels();
const migrations = await new MigrationGenerator(db, models).generate();

writeToFile(`migrations/${getMigrationDate()}_${name}.ts`, migrations);
writeToFile(`migrations/${date || getMigrationDate()}_${name}.ts`, migrations);
} finally {
await db.destroy();
}
Expand Down

0 comments on commit 30fa32b

Please sign in to comment.