dockerfile/examples/omnivore/official-src/omnivore-main/packages/db/plopfile.js

46 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2024-03-15 14:52:38 +08:00
const fs = require('fs');
const getLatestMigrationNumber = () => {
const fileNames = fs.readdirSync('./migrations');
if (fileNames.length === 0) return 0;
const migrationNumbers = fileNames
.map(fname => parseInt(fname.split('.')[0]))
.sort((a, b) => a > b);
return migrationNumbers[migrationNumbers.length - 1];
};
module.exports = function (plop) {
plop.setGenerator('migration', {
description: 'Migration generator',
prompts: [
{
type: 'input',
name: 'name',
message: 'migration name (snake_case):',
},
{
type: 'input',
name: 'desc',
message: 'migration description:',
},
],
actions: () => {
const migrationNumber = (getLatestMigrationNumber() + 1).toString().padStart(4, '0');
return [
{
type: 'add',
path: 'migrations/{{number}}.do.{{name}}.sql',
templateFile: 'templates/migration.hbs',
data: { type: 'DO', number: migrationNumber },
},
{
type: 'add',
path: 'migrations/{{number}}.undo.{{name}}.sql',
templateFile: 'templates/migration.hbs',
data: { type: 'UNDO', number: migrationNumber },
},
];
},
});
};