stepwise-migrations 1.0.26 → 1.0.28
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/README.md +1 -1
- package/dist/src/index.js +80 -2
- package/dist/test/index.test.js +12 -11
- package/package.json +1 -1
- package/src/db/pg.ts +1 -2
- package/src/index.ts +101 -5
- package/src/utils.ts +1 -5
- package/test/index.test.ts +12 -19
package/README.md
CHANGED
package/dist/src/index.js
CHANGED
@@ -14,12 +14,90 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
};
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
16
16
|
const yargs_1 = __importDefault(require("yargs"));
|
17
|
+
const helpers_1 = require("yargs/helpers");
|
17
18
|
const commands_1 = require("./commands");
|
18
19
|
const utils_1 = require("./utils");
|
19
20
|
const main = () => __awaiter(void 0, void 0, void 0, function* () {
|
20
|
-
const argv = (0, yargs_1.default)(process.argv
|
21
|
+
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
|
22
|
+
.command("migrate", "Migrate the database to the latest version", (yargs) => {
|
23
|
+
return yargs.options({
|
24
|
+
napply: {
|
25
|
+
type: "number",
|
26
|
+
describe: "Number of migrations to apply, defaults to all",
|
27
|
+
},
|
28
|
+
});
|
29
|
+
})
|
30
|
+
.command("validate", "Validate the migration files and the stepwise_migration_events table")
|
31
|
+
.command("info", "Show information about the current state of the migrations in the database and in the migration files")
|
32
|
+
.command("status", "Only show the status of the migrations in the database, not the migration files")
|
33
|
+
.command("undo", "Rollback the database to the previous version", (yargs) => {
|
34
|
+
return yargs.options({
|
35
|
+
nundo: {
|
36
|
+
type: "number",
|
37
|
+
describe: "Number of migrations to undo, defaults to 1",
|
38
|
+
},
|
39
|
+
});
|
40
|
+
})
|
41
|
+
.command("get-applied-script", "Get the script for the last applied migration", (yargs) => {
|
42
|
+
return yargs.options({
|
43
|
+
filename: {
|
44
|
+
type: "string",
|
45
|
+
describe: "The filename to get the script for",
|
46
|
+
},
|
47
|
+
});
|
48
|
+
})
|
49
|
+
.command("drop", "Drop the tables, schema and migration history table")
|
50
|
+
.command("baseline", "Without applying any migrations, set the migration table state to a specific version", (yargs) => {
|
51
|
+
return yargs.options({
|
52
|
+
filename: {
|
53
|
+
type: "string",
|
54
|
+
describe: "The filename to baseline to",
|
55
|
+
default: null,
|
56
|
+
},
|
57
|
+
});
|
58
|
+
})
|
59
|
+
.options({
|
60
|
+
connection: {
|
61
|
+
type: "string",
|
62
|
+
demandOption: true,
|
63
|
+
alias: "c",
|
64
|
+
describe: "The connection string to use to connect to the database",
|
65
|
+
},
|
66
|
+
schema: {
|
67
|
+
type: "string",
|
68
|
+
default: "public",
|
69
|
+
alias: "s",
|
70
|
+
describe: "The schema to use for the migrations",
|
71
|
+
},
|
72
|
+
ssl: {
|
73
|
+
type: "boolean",
|
74
|
+
default: false,
|
75
|
+
describe: "Whether to use SSL for the connection",
|
76
|
+
},
|
77
|
+
path: {
|
78
|
+
type: "string",
|
79
|
+
default: "./",
|
80
|
+
alias: "p",
|
81
|
+
describe: "The path to the migrations directory",
|
82
|
+
},
|
83
|
+
})
|
84
|
+
.example([
|
85
|
+
[
|
86
|
+
`npx stepwise-migrations migrate \\
|
87
|
+
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \\
|
88
|
+
--schema=myschema \\
|
89
|
+
--path=./test/migrations-template/`,
|
90
|
+
"Migrate the database to the latest version",
|
91
|
+
],
|
92
|
+
])
|
93
|
+
.wrap(yargs_1.default.terminalWidth())
|
94
|
+
.demandCommand(1)
|
95
|
+
.recommendCommands()
|
96
|
+
.strict()
|
97
|
+
.help()
|
98
|
+
.version(false)
|
99
|
+
.parse();
|
21
100
|
const args = (0, utils_1.parseArgs)(argv);
|
22
|
-
(0, utils_1.validateArgs)(args);
|
23
101
|
const command = args.command;
|
24
102
|
if (command === "migrate") {
|
25
103
|
yield (0, commands_1.migrateCommand)(args);
|
package/dist/test/index.test.js
CHANGED
@@ -12,7 +12,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
13
|
};
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
-
const node_assert_1 = __importDefault(require("node:assert"));
|
16
15
|
const node_fs_1 = __importDefault(require("node:fs"));
|
17
16
|
const node_test_1 = require("node:test");
|
18
17
|
const utils_1 = require("./utils");
|
@@ -29,16 +28,18 @@ const executeCommand = (command, path = "", extraArgs = "") => (0, utils_1.execu
|
|
29
28
|
`);
|
30
29
|
(0, node_test_1.describe)("valid migrations", () => __awaiter(void 0, void 0, void 0, function* () {
|
31
30
|
(0, node_test_1.beforeEach)(() => __awaiter(void 0, void 0, void 0, function* () {
|
32
|
-
|
33
|
-
|
34
|
-
|
31
|
+
(0, utils_1.assertIncludesAll)(yield executeCommand("drop", ""), [
|
32
|
+
"Dropping the tables, schema and migration history table... done!",
|
33
|
+
]);
|
35
34
|
node_fs_1.default.rmSync(paths.valid, { recursive: true, force: true });
|
36
35
|
node_fs_1.default.cpSync("./test/migrations-template", paths.valid, {
|
37
36
|
recursive: true,
|
38
37
|
});
|
39
38
|
}));
|
40
39
|
(0, node_test_1.it)("migrate without params", () => __awaiter(void 0, void 0, void 0, function* () {
|
41
|
-
(0, utils_1.assertIncludesAll)(yield (0, utils_1.execute)("npm exec stepwise-migrations"), [
|
40
|
+
(0, utils_1.assertIncludesAll)(yield (0, utils_1.execute)("npm exec stepwise-migrations"), [
|
41
|
+
"stepwise-migrations <command>",
|
42
|
+
]);
|
42
43
|
}));
|
43
44
|
(0, node_test_1.it)("baseline", () => __awaiter(void 0, void 0, void 0, function* () {
|
44
45
|
(0, utils_1.assertIncludesAll)(yield executeCommand("baseline", paths.valid), [
|
@@ -106,22 +107,22 @@ const executeCommand = (command, path = "", extraArgs = "") => (0, utils_1.execu
|
|
106
107
|
]);
|
107
108
|
}));
|
108
109
|
}));
|
109
|
-
|
110
|
+
node_test_1.describe.only("invalid migrations", () => __awaiter(void 0, void 0, void 0, function* () {
|
110
111
|
(0, node_test_1.beforeEach)(() => __awaiter(void 0, void 0, void 0, function* () {
|
111
|
-
|
112
|
-
|
113
|
-
|
112
|
+
(0, utils_1.assertIncludesAll)(yield executeCommand("drop", ""), [
|
113
|
+
"Dropping the tables, schema and migration history table... done!",
|
114
|
+
]);
|
114
115
|
node_fs_1.default.rmSync(paths.invalid, { recursive: true, force: true });
|
115
116
|
node_fs_1.default.cpSync("./test/migrations-template", paths.invalid, {
|
116
117
|
recursive: true,
|
117
118
|
});
|
118
119
|
}));
|
119
|
-
|
120
|
+
node_test_1.it.only("missing undo migration", () => __awaiter(void 0, void 0, void 0, function* () {
|
120
121
|
(0, utils_1.assertIncludesAll)(yield executeCommand("migrate", paths.invalid), [
|
121
122
|
"All done!",
|
122
123
|
]);
|
123
124
|
node_fs_1.default.unlinkSync("./test/migrations-invalid/v3_third.undo.sql");
|
124
|
-
(0, utils_1.assertIncludesAll)(yield executeCommand("undo", paths.invalid, "--
|
125
|
+
(0, utils_1.assertIncludesAll)(yield executeCommand("undo", paths.invalid, "--nundo=2"), ["Error: not enough sequential (from last) undo migrations to apply"]);
|
125
126
|
}));
|
126
127
|
(0, node_test_1.it)("alter migration", () => __awaiter(void 0, void 0, void 0, function* () {
|
127
128
|
(0, utils_1.assertIncludesAll)(yield executeCommand("migrate", paths.invalid), [
|
package/package.json
CHANGED
package/src/db/pg.ts
CHANGED
@@ -10,10 +10,9 @@ pg.types.setTypeParser(1082, function (stringValue) {
|
|
10
10
|
return stringValue; //1082 for date type
|
11
11
|
});
|
12
12
|
|
13
|
-
export const _dbConnect = async ({
|
13
|
+
export const _dbConnect = async ({ connection, schema }: Args) => {
|
14
14
|
const pool = new Pool({
|
15
15
|
connectionString: connection,
|
16
|
-
ssl: ssl === "true",
|
17
16
|
});
|
18
17
|
|
19
18
|
let client: PoolClient | undefined;
|
package/src/index.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
#!/usr/bin/env node
|
2
2
|
|
3
3
|
import yargs from "yargs";
|
4
|
+
import { hideBin } from "yargs/helpers";
|
4
5
|
import {
|
5
6
|
auditCommand,
|
6
7
|
baselineCommand,
|
@@ -12,14 +13,109 @@ import {
|
|
12
13
|
undoCommand,
|
13
14
|
validateCommand,
|
14
15
|
} from "./commands";
|
15
|
-
import { parseArgs, usage
|
16
|
-
|
16
|
+
import { parseArgs, usage } from "./utils";
|
17
17
|
const main = async () => {
|
18
|
-
const argv
|
18
|
+
const argv = yargs(hideBin(process.argv))
|
19
|
+
.command(
|
20
|
+
"migrate",
|
21
|
+
"Migrate the database to the latest version",
|
22
|
+
(yargs) => {
|
23
|
+
return yargs.options({
|
24
|
+
napply: {
|
25
|
+
type: "number",
|
26
|
+
describe: "Number of migrations to apply, defaults to all",
|
27
|
+
},
|
28
|
+
});
|
29
|
+
}
|
30
|
+
)
|
31
|
+
.command(
|
32
|
+
"validate",
|
33
|
+
"Validate the migration files and the stepwise_migration_events table"
|
34
|
+
)
|
35
|
+
.command(
|
36
|
+
"info",
|
37
|
+
"Show information about the current state of the migrations in the database and in the migration files"
|
38
|
+
)
|
39
|
+
.command(
|
40
|
+
"status",
|
41
|
+
"Only show the status of the migrations in the database, not the migration files"
|
42
|
+
)
|
43
|
+
.command(
|
44
|
+
"undo",
|
45
|
+
"Rollback the database to the previous version",
|
46
|
+
(yargs) => {
|
47
|
+
return yargs.options({
|
48
|
+
nundo: {
|
49
|
+
type: "number",
|
50
|
+
describe: "Number of migrations to undo, defaults to 1",
|
51
|
+
},
|
52
|
+
});
|
53
|
+
}
|
54
|
+
)
|
55
|
+
.command(
|
56
|
+
"get-applied-script",
|
57
|
+
"Get the script for the last applied migration",
|
58
|
+
(yargs) => {
|
59
|
+
return yargs.options({
|
60
|
+
filename: {
|
61
|
+
type: "string",
|
62
|
+
describe: "The filename to get the script for",
|
63
|
+
},
|
64
|
+
});
|
65
|
+
}
|
66
|
+
)
|
67
|
+
.command("drop", "Drop the tables, schema and migration history table")
|
68
|
+
.command(
|
69
|
+
"baseline",
|
70
|
+
"Without applying any migrations, set the migration table state to a specific version",
|
71
|
+
(yargs) => {
|
72
|
+
return yargs.options({
|
73
|
+
filename: {
|
74
|
+
type: "string",
|
75
|
+
describe: "The filename to baseline to",
|
76
|
+
default: null,
|
77
|
+
},
|
78
|
+
});
|
79
|
+
}
|
80
|
+
)
|
81
|
+
.options({
|
82
|
+
connection: {
|
83
|
+
type: "string",
|
84
|
+
demandOption: true,
|
85
|
+
alias: "c",
|
86
|
+
describe: "The connection string to use to connect to the database",
|
87
|
+
},
|
88
|
+
schema: {
|
89
|
+
type: "string",
|
90
|
+
default: "public",
|
91
|
+
alias: "s",
|
92
|
+
describe: "The schema to use for the migrations",
|
93
|
+
},
|
94
|
+
path: {
|
95
|
+
type: "string",
|
96
|
+
default: "./",
|
97
|
+
alias: "p",
|
98
|
+
describe: "The path to the migrations directory",
|
99
|
+
},
|
100
|
+
})
|
101
|
+
.example([
|
102
|
+
[
|
103
|
+
`npx stepwise-migrations migrate \\
|
104
|
+
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \\
|
105
|
+
--schema=myschema \\
|
106
|
+
--path=./test/migrations-template/`,
|
107
|
+
"Migrate the database to the latest version",
|
108
|
+
],
|
109
|
+
])
|
110
|
+
.wrap(yargs.terminalWidth())
|
111
|
+
.demandCommand(1)
|
112
|
+
.recommendCommands()
|
113
|
+
.strict()
|
114
|
+
.help()
|
115
|
+
.version(false)
|
116
|
+
.parse();
|
19
117
|
|
20
118
|
const args = parseArgs(argv);
|
21
|
-
validateArgs(args);
|
22
|
-
|
23
119
|
const command = args.command;
|
24
120
|
|
25
121
|
if (command === "migrate") {
|
package/src/utils.ts
CHANGED
@@ -34,7 +34,6 @@ Options:
|
|
34
34
|
--connection <connection> The connection string to use to connect to the database
|
35
35
|
--schema <schema> The schema to use for the migrations (default: public)
|
36
36
|
--path <path> The path to the migrations directory
|
37
|
-
--ssl true/false Whether to use SSL for the connection (default: false)
|
38
37
|
--napply Number of up migrations to apply (default: all)
|
39
38
|
--nundo Number of undo migrations to apply (default: 1)
|
40
39
|
--filename (get-applied-script) The filename to get the script for (default: last applied migration)
|
@@ -42,7 +41,7 @@ Options:
|
|
42
41
|
|
43
42
|
Example:
|
44
43
|
npx stepwise-migrations migrate \
|
45
|
-
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \
|
44
|
+
--connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase\?sslmode=require \
|
46
45
|
--schema=myschema \
|
47
46
|
--path=./test/migrations-template/
|
48
47
|
`;
|
@@ -55,7 +54,6 @@ export type Args = {
|
|
55
54
|
filePath: string;
|
56
55
|
connection: string;
|
57
56
|
filename?: string;
|
58
|
-
ssl: string;
|
59
57
|
};
|
60
58
|
|
61
59
|
export const parseArgs = (argv: any): Args => {
|
@@ -65,7 +63,6 @@ export const parseArgs = (argv: any): Args => {
|
|
65
63
|
const nundo = argv.nundo || 1;
|
66
64
|
const filePath = argv.path;
|
67
65
|
const connection = argv.connection;
|
68
|
-
const ssl = argv.ssl ?? "false";
|
69
66
|
const filename = argv.filename;
|
70
67
|
|
71
68
|
return {
|
@@ -75,7 +72,6 @@ export const parseArgs = (argv: any): Args => {
|
|
75
72
|
nundo,
|
76
73
|
filePath,
|
77
74
|
connection,
|
78
|
-
ssl,
|
79
75
|
filename,
|
80
76
|
};
|
81
77
|
};
|
package/test/index.test.ts
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
import assert from "node:assert";
|
2
1
|
import fs from "node:fs";
|
3
2
|
import { beforeEach, describe, it } from "node:test";
|
4
3
|
import { assertIncludesAll, assertIncludesExcludesAll, execute } from "./utils";
|
@@ -23,13 +22,9 @@ const executeCommand = (
|
|
23
22
|
|
24
23
|
describe("valid migrations", async () => {
|
25
24
|
beforeEach(async () => {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
"Dropping the tables, schema and migration history table... done!"
|
30
|
-
)
|
31
|
-
);
|
32
|
-
assert.ok(exitCode === 0);
|
25
|
+
assertIncludesAll(await executeCommand("drop", ""), [
|
26
|
+
"Dropping the tables, schema and migration history table... done!",
|
27
|
+
]);
|
33
28
|
|
34
29
|
fs.rmSync(paths.valid, { recursive: true, force: true });
|
35
30
|
fs.cpSync("./test/migrations-template", paths.valid, {
|
@@ -38,7 +33,9 @@ describe("valid migrations", async () => {
|
|
38
33
|
});
|
39
34
|
|
40
35
|
it("migrate without params", async () => {
|
41
|
-
assertIncludesAll(await execute("npm exec stepwise-migrations"), [
|
36
|
+
assertIncludesAll(await execute("npm exec stepwise-migrations"), [
|
37
|
+
"stepwise-migrations <command>",
|
38
|
+
]);
|
42
39
|
});
|
43
40
|
|
44
41
|
it("baseline", async () => {
|
@@ -131,15 +128,11 @@ describe("valid migrations", async () => {
|
|
131
128
|
});
|
132
129
|
});
|
133
130
|
|
134
|
-
describe("invalid migrations", async () => {
|
131
|
+
describe.only("invalid migrations", async () => {
|
135
132
|
beforeEach(async () => {
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
"Dropping the tables, schema and migration history table... done!"
|
140
|
-
)
|
141
|
-
);
|
142
|
-
assert.ok(exitCode === 0);
|
133
|
+
assertIncludesAll(await executeCommand("drop", ""), [
|
134
|
+
"Dropping the tables, schema and migration history table... done!",
|
135
|
+
]);
|
143
136
|
|
144
137
|
fs.rmSync(paths.invalid, { recursive: true, force: true });
|
145
138
|
fs.cpSync("./test/migrations-template", paths.invalid, {
|
@@ -147,7 +140,7 @@ describe("invalid migrations", async () => {
|
|
147
140
|
});
|
148
141
|
});
|
149
142
|
|
150
|
-
it("missing undo migration", async () => {
|
143
|
+
it.only("missing undo migration", async () => {
|
151
144
|
assertIncludesAll(await executeCommand("migrate", paths.invalid), [
|
152
145
|
"All done!",
|
153
146
|
]);
|
@@ -155,7 +148,7 @@ describe("invalid migrations", async () => {
|
|
155
148
|
fs.unlinkSync("./test/migrations-invalid/v3_third.undo.sql");
|
156
149
|
|
157
150
|
assertIncludesAll(
|
158
|
-
await executeCommand("undo", paths.invalid, "--
|
151
|
+
await executeCommand("undo", paths.invalid, "--nundo=2"),
|
159
152
|
["Error: not enough sequential (from last) undo migrations to apply"]
|
160
153
|
);
|
161
154
|
});
|