@boxctl/migrate 1.1.2 → 2.0.1

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/src/bootstrap.js DELETED
@@ -1,18 +0,0 @@
1
- // src/bootstrap.js
2
- import db from "./db.js";
3
-
4
- export async function bootstrap() {
5
- try {
6
- await db.query(`
7
- CREATE TABLE IF NOT EXISTS migrations (
8
- id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
9
- name VARCHAR(255) NOT NULL UNIQUE,
10
- ran_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
11
- )
12
- `);
13
- } catch (err) {
14
- console.error("Failed to initialize migrations table:");
15
- console.error(err.message);
16
- process.exit(1);
17
- }
18
- }
@@ -1,101 +0,0 @@
1
- // src/commands/down.js
2
- import { readFileSync, existsSync } from "fs";
3
- import { join } from "path";
4
- import { createInterface } from "readline";
5
- import db from "../db.js";
6
- import { bootstrap } from "../bootstrap.js";
7
- import { env } from "../env.js";
8
-
9
- const MIGRATIONS_DIR = "./migrations";
10
-
11
- function prompt(question) {
12
- return new Promise((resolve) => {
13
- const rl = createInterface({
14
- input: process.stdin,
15
- output: process.stdout,
16
- });
17
-
18
- const onSigint = () => {
19
- rl.close();
20
- console.log("\nAborted.");
21
- process.exit(0);
22
- };
23
-
24
- process.on("SIGINT", onSigint);
25
-
26
- rl.question(question, (answer) => {
27
- process.removeListener("SIGINT", onSigint);
28
- rl.close();
29
- resolve(answer.trim().toLowerCase());
30
- });
31
- });
32
- }
33
-
34
- export async function down(n = 1) {
35
- const count = parseInt(n, 10);
36
-
37
- if (isNaN(count) || count < 1) {
38
- console.error(
39
- "Usage: migrate down [n] — n must be a positive integer",
40
- );
41
- process.exit(1);
42
- }
43
-
44
- await bootstrap();
45
-
46
- // get applied migrations, most recent first
47
- const [rows] = await db.query(
48
- "SELECT name FROM migrations ORDER BY ran_at DESC, id DESC LIMIT ?",
49
- [count],
50
- );
51
-
52
- if (rows.length === 0) {
53
- console.log("Nothing to revert. No migrations have been applied.");
54
- return;
55
- }
56
-
57
- const toRevert = rows.map((r) => r.name);
58
-
59
- console.log(`About to revert ${toRevert.length} migration(s):`);
60
- toRevert.forEach((name) => console.log(` - ${name}`));
61
-
62
- if (env.isProd) {
63
- console.warn("\n Warning: you are on production.\n");
64
- }
65
-
66
- const answer = await prompt("Are you sure? [y/N]: ");
67
-
68
- if (answer !== "y") {
69
- console.log("Aborted.");
70
- return;
71
- }
72
-
73
- for (const name of toRevert) {
74
- const downFile = join(MIGRATIONS_DIR, `${name}.down.sql`);
75
-
76
- if (!existsSync(downFile)) {
77
- console.error(`Down file not found for migration: ${name}`);
78
- console.error(`Expected: ${downFile}`);
79
- process.exit(1);
80
- }
81
-
82
- const sql = readFileSync(downFile, "utf8").trim();
83
-
84
- if (!sql || sql.startsWith("--")) {
85
- console.error(`Down file is empty for migration: ${name}`);
86
- process.exit(1);
87
- }
88
-
89
- console.log(` Reverting: ${name}`);
90
- try {
91
- await db.query(sql);
92
- await db.query("DELETE FROM migrations WHERE name = ?", [name]);
93
- } catch (err) {
94
- console.error(`Revert failed: ${name}`);
95
- console.error(err.message);
96
- process.exit(1);
97
- }
98
- }
99
-
100
- console.log("Done.");
101
- }