Allow seeding without forcing sync

This commit is contained in:
cameronapak
2026-01-19 11:46:48 -06:00
parent 9685c5d4e1
commit 11e3488917

View File

@@ -21,46 +21,47 @@ export const sync: CliCommand = (program) => {
console.info(""); console.info("");
if (stmts.length === 0) { if (stmts.length === 0) {
console.info(c.yellow("No changes to sync")); console.info(c.yellow("No changes to sync"));
process.exit(0); } else {
} // @todo: currently assuming parameters aren't used
// @todo: currently assuming parameters aren't used const sql = stmts.map((d) => d.sql).join(";\n") + ";";
const sql = stmts.map((d) => d.sql).join(";\n") + ";";
if (options.force) { if (options.force) {
console.info(c.dim("Executing:") + "\n" + c.cyan(sql)); console.info(c.dim("Executing:") + "\n" + c.cyan(sql));
await schema.sync({ force: true, drop: options.drop }); await schema.sync({ force: true, drop: options.drop });
console.info(`\n${c.dim(`Executed ${c.cyan(stmts.length)} statement(s)`)}`); console.info(`\n${c.dim(`Executed ${c.cyan(stmts.length)} statement(s)`)}`);
console.info(`${c.green("Database synced")}`); console.info(`${c.green("Database synced")}`);
} else {
if (options.seed) { if (options.out) {
console.info(c.dim("\nExecuting seed...")); const output = options.sql ? sql : JSON.stringify(stmts, null, 2);
const seed = app.options?.seed; await writeFile(options.out, output);
if (seed) { console.info(`SQL written to ${c.cyan(options.out)}`);
await app.options?.seed?.({
...app.modules.ctx(),
app: app,
});
console.info(c.green("Seed executed"));
} else { } else {
console.info(c.yellow("No seed function provided")); console.info(c.dim("DDL to execute:") + "\n" + c.cyan(sql));
console.info(
c.yellow(
"\nNo statements have been executed. Use --force to perform database syncing operations",
),
);
} }
} }
} else { }
if (options.out) {
const output = options.sql ? sql : JSON.stringify(stmts, null, 2);
await writeFile(options.out, output);
console.info(`SQL written to ${c.cyan(options.out)}`);
} else {
console.info(c.dim("DDL to execute:") + "\n" + c.cyan(sql));
console.info( if (options.seed) {
c.yellow( console.info(c.dim("\nExecuting seed..."));
"\nNo statements have been executed. Use --force to perform database syncing operations", const seed = app.options?.seed;
), if (seed) {
); await seed({
...app.modules.ctx(),
app: app,
});
console.info(c.green("Seed executed"));
} else {
console.info(c.yellow("No seed function provided"));
} }
} }
process.exit(0); process.exit(0);
}); });
}; };