@stacksjs/buddy 0.70.203 → 0.70.205
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commands/migrate.js +56 -0
- package/package.json +1 -1
package/dist/commands/migrate.js
CHANGED
|
@@ -501,5 +501,61 @@ export function migrate(buddy) {
|
|
|
501
501
|
});
|
|
502
502
|
process.exit(ExitCode.Success);
|
|
503
503
|
});
|
|
504
|
+
buddy.command("migrate:regenerate [dialect]", "Rebuild database/migrations from your models for a given dialect").option("--dry-run", "Show what would change without writing anything", { default: !1 }).option("-f, --force", "Regenerate even though the database already has migrations recorded", { default: !1 }).action(async (dialect, options) => {
|
|
505
|
+
const perf = await intro("buddy migrate:regenerate"), target = (dialect || process.env.DB_CONNECTION || "sqlite").toLowerCase();
|
|
506
|
+
if (!new Set(["sqlite", "mysql", "postgres", "singlestore"]).has(target)) {
|
|
507
|
+
log.syncError(`Unknown dialect "${target}". Allowed: sqlite, mysql, postgres, singlestore.`);
|
|
508
|
+
process.exit(ExitCode.FatalError);
|
|
509
|
+
}
|
|
510
|
+
const { countAppliedMigrations, regenerateMigrationCorpus } = await import("@stacksjs/database");
|
|
511
|
+
let applied = 0;
|
|
512
|
+
try {
|
|
513
|
+
applied = await countAppliedMigrations();
|
|
514
|
+
} catch {
|
|
515
|
+
applied = 0;
|
|
516
|
+
}
|
|
517
|
+
if (applied > 0 && !options.force && !options.dryRun) {
|
|
518
|
+
log.syncError(`This database already has ${applied} migration(s) recorded.`);
|
|
519
|
+
log.syncError("Regenerating renumbers every file, and the migrations table keys on the filename,");
|
|
520
|
+
log.syncError("so already-applied migrations would look pending and run a second time.");
|
|
521
|
+
log.syncError(" Point at an empty database, or re-run with --force if you know it is safe.");
|
|
522
|
+
process.exit(ExitCode.FatalError);
|
|
523
|
+
}
|
|
524
|
+
const plan = await regenerateMigrationCorpus({ dialect: target, dryRun: !0 });
|
|
525
|
+
if (plan.isErr) {
|
|
526
|
+
log.syncError(plan.error.message);
|
|
527
|
+
process.exit(ExitCode.FatalError);
|
|
528
|
+
}
|
|
529
|
+
const { files, removed, models } = plan.value;
|
|
530
|
+
console.log(`
|
|
531
|
+
Regenerate plan: ${target}
|
|
532
|
+
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
533
|
+
\u2022 ${models} model(s) read from app/Models and the framework defaults
|
|
534
|
+
\u2022 ${files.length} migration file(s) will be written
|
|
535
|
+
\u2022 ${removed.length} existing file(s) will be removed
|
|
536
|
+
\u2022 These files are tracked in git, so review with \`git diff\` afterwards
|
|
537
|
+
\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
|
|
538
|
+
`);
|
|
539
|
+
if (options.dryRun) {
|
|
540
|
+
await outro("Dry run. Nothing was written.", { startTime: perf, useSeconds: !0 });
|
|
541
|
+
process.exit(ExitCode.Success);
|
|
542
|
+
}
|
|
543
|
+
if (!isCI && hasTTY && process.stdin.isTTY) {
|
|
544
|
+
await log.flush();
|
|
545
|
+
if (!await confirm({ message: `Replace ${removed.length} migration file(s) with ${files.length} generated for ${target}?`, initial: !1 })) {
|
|
546
|
+
await outro("Cancelled. Nothing was written.", { startTime: perf, useSeconds: !0 });
|
|
547
|
+
process.exit(ExitCode.Success);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
const result = await regenerateMigrationCorpus({ dialect: target });
|
|
551
|
+
if (result.isErr) {
|
|
552
|
+
log.syncError(result.error.message);
|
|
553
|
+
process.exit(ExitCode.FatalError);
|
|
554
|
+
}
|
|
555
|
+
log.success(`Wrote ${result.value.files.length} ${target} migration file(s) to database/migrations.`);
|
|
556
|
+
log.info("Review the change with `git diff`, then run `./buddy migrate`.");
|
|
557
|
+
await outro("Regenerated.", { startTime: perf, useSeconds: !0 });
|
|
558
|
+
process.exit(ExitCode.Success);
|
|
559
|
+
});
|
|
504
560
|
onUnknownSubcommand(buddy, "migrate");
|
|
505
561
|
}
|