stepwise-migrations 1.0.26 → 1.0.27

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 CHANGED
@@ -62,7 +62,7 @@ There are three types of migrations:
62
62
  ## Usage
63
63
 
64
64
  ```text
65
- Usage: stepwise-migrations [command] [options]
65
+ Usage: stepwise-migrations <command> [options]
66
66
 
67
67
  Commands:
68
68
  migrate
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.slice(2)).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);
@@ -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
- const { output, error, exitCode } = yield executeCommand("drop", "");
33
- node_assert_1.default.ok(output.includes("Dropping the tables, schema and migration history table... done!"));
34
- node_assert_1.default.ok(exitCode === 0);
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"), ["Usage"]);
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
- (0, node_test_1.describe)("invalid migrations", () => __awaiter(void 0, void 0, void 0, function* () {
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
- const { output, error, exitCode } = yield executeCommand("drop", "");
112
- node_assert_1.default.ok(output.includes("Dropping the tables, schema and migration history table... done!"));
113
- node_assert_1.default.ok(exitCode === 0);
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
- (0, node_test_1.it)("missing undo migration", () => __awaiter(void 0, void 0, void 0, function* () {
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, "--nundos=2"), ["Error: not enough sequential (from last) undo migrations to apply"]);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stepwise-migrations",
3
- "version": "1.0.26",
3
+ "version": "1.0.27",
4
4
  "description": "A JavaScript CLI tool for managing Raw SQL migrations in a Postgres database. Loosely based on flyway.",
5
5
  "main": "./dist/src/index.js",
6
6
  "scripts": {
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,114 @@ import {
12
13
  undoCommand,
13
14
  validateCommand,
14
15
  } from "./commands";
15
- import { parseArgs, usage, validateArgs } from "./utils";
16
-
16
+ import { parseArgs, usage } from "./utils";
17
17
  const main = async () => {
18
- const argv: any = yargs(process.argv.slice(2)).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
+ ssl: {
95
+ type: "boolean",
96
+ default: false,
97
+ describe: "Whether to use SSL for the connection",
98
+ },
99
+ path: {
100
+ type: "string",
101
+ default: "./",
102
+ alias: "p",
103
+ describe: "The path to the migrations directory",
104
+ },
105
+ })
106
+ .example([
107
+ [
108
+ `npx stepwise-migrations migrate \\
109
+ --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \\
110
+ --schema=myschema \\
111
+ --path=./test/migrations-template/`,
112
+ "Migrate the database to the latest version",
113
+ ],
114
+ ])
115
+ .wrap(yargs.terminalWidth())
116
+ .demandCommand(1)
117
+ .recommendCommands()
118
+ .strict()
119
+ .help()
120
+ .version(false)
121
+ .parse();
19
122
 
20
123
  const args = parseArgs(argv);
21
- validateArgs(args);
22
-
23
124
  const command = args.command;
24
125
 
25
126
  if (command === "migrate") {
@@ -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
- const { output, error, exitCode } = await executeCommand("drop", "");
27
- assert.ok(
28
- output.includes(
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"), ["Usage"]);
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
- const { output, error, exitCode } = await executeCommand("drop", "");
137
- assert.ok(
138
- output.includes(
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, "--nundos=2"),
151
+ await executeCommand("undo", paths.invalid, "--nundo=2"),
159
152
  ["Error: not enough sequential (from last) undo migrations to apply"]
160
153
  );
161
154
  });