stepwise-migrations 1.0.16 → 1.0.17

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
@@ -41,18 +41,20 @@ Options:
41
41
  --path <path> The path to the migrations directory
42
42
  --ssl true/false Whether to use SSL for the connection (default: false)
43
43
  --napply Number of up migrations to apply (default: all)
44
- --nundo Number of down migrations to apply (default: 1)
44
+ --nundo Number of undo migrations to apply (default: 1)
45
45
  --filename The filename to get the script for (default: last applied migration)
46
46
 
47
47
  Example:
48
48
  npx stepwise-migrations migrate \
49
49
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydatabase \
50
50
  --schema=myschema \
51
- --path=./db/migration/
51
+ --path=./test/migrations-template/
52
52
  ```
53
53
 
54
54
  ## Examples
55
55
 
56
+ [comment]: <> (Start of examples)
57
+
56
58
  ### Migrate
57
59
 
58
60
  If all files are in a valid state, runs all the "up" migrations that have not been applied yet.
@@ -61,7 +63,7 @@ If all files are in a valid state, runs all the "up" migrations that have not be
61
63
  npx stepwise-migrations migrate \
62
64
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
63
65
  --schema=myschema \
64
- --path=./db/migration/
66
+ --path=./test/migrations-template/
65
67
  ```
66
68
 
67
69
  <details>
@@ -71,36 +73,46 @@ npx stepwise-migrations migrate \
71
73
  ```text
72
74
  Creating schema myschema... done!
73
75
  Creating stepwise_migration_events table... done!
74
- Applying migration v1_connect_session_table.sql... done!
75
- Applying migration v2_auth.sql... done!
76
- All done! Applied 2 migrations
77
- Migration state:
78
- ┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
79
- (index) id │ name │ applied_by │ applied_at │
80
- ├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
81
- 0 │ 1 'v1_connect_session_table.sql''postgres''2024-11-24 05:40:41.211617'
82
- │ 1 │ 2 │ 'v2_auth.sql' │ 'postgres' │ '2024-11-24 05:40:41.214732' │
83
- └─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
84
- Unapplied migrations:
76
+ Applying versioned migration v1_first.sql... done!
77
+ Applying versioned migration v2_second.sql... done!
78
+ Applying versioned migration v3_third.sql... done!
79
+ Applying repeatable migration v0_get_number.repeatable.sql... done!
80
+ All done! Applied 4 migrations
81
+ All applied versioned migrations:
82
+ ┌─────────┬────┬─────────────┬─────────────────┬────────────┬──────────────────────────────┐
83
+ (index)idtype │ filename applied_by applied_at
84
+ ├─────────┼────┼─────────────┼─────────────────┼────────────┼──────────────────────────────┤
85
+ │ 0 │ 1 │ 'versioned' │ 'v1_first.sql' │ 'postgres' │ '2024-11-25 15:25:55.799253' │
86
+ 1 │ 2 │ 'versioned' │ 'v2_second.sql' │ 'postgres' │ '2024-11-25 15:25:55.80306' │
87
+ │ 2 │ 3 │ 'versioned' │ 'v3_third.sql' │ 'postgres' │ '2024-11-25 15:25:55.80534' │
88
+ └─────────┴────┴─────────────┴─────────────────┴────────────┴──────────────────────────────┘
89
+ All applied repeatable migrations:
90
+ ┌─────────┬────┬──────────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
91
+ │ (index) │ id │ type │ filename │ applied_by │ applied_at │
92
+ ├─────────┼────┼──────────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
93
+ │ 0 │ 4 │ 'repeatable' │ 'v0_get_number.repeatable.sql' │ 'postgres' │ '2024-11-25 15:25:55.807375' │
94
+ └─────────┴────┴──────────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
95
+ Unapplied versioned migrations:
85
96
  ┌─────────┐
86
97
  │ (index) │
87
98
  ├─────────┤
99
+ └─────────┘
88
100
  ```
89
101
 
90
102
  </details>
91
103
 
92
104
  ### Undo
93
105
 
94
- Runs a single down migration for the last applied migration.
95
- Can run multiple down migrations if the `--nundo` option is provided.
106
+ Runs a single undo migration for the last applied migration.
107
+ Can run multiple undo migrations if the `--nundo` option is provided.
96
108
 
97
109
  Command:
98
110
 
99
111
  ```bash
100
- npx stepwise-migrations down \
112
+ npx stepwise-migrations undo \
101
113
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
102
114
  --schema=myschema \
103
- --path=./db/migration/
115
+ --path=./test/migrations-template/
104
116
  ```
105
117
 
106
118
  <details>
@@ -108,20 +120,34 @@ npx stepwise-migrations down \
108
120
  <summary>Example output</summary>
109
121
 
110
122
  ```text
111
- Applying down migration v2_auth.undo.sql... done!
112
- All done! Applied 1 down migration
113
- Migration state:
114
- ┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
115
- (index) id │ name │ applied_by │ applied_at │
116
- ├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
117
- │ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
118
- └─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
119
- Unapplied migrations:
120
- ┌─────────┬───────────────┐
121
- │ (index) │ filename │
122
- ├─────────┼───────────────┤
123
- │ 0 │ 'v2_auth.sql' │
124
- └─────────┴───────────────┘
123
+ [
124
+ {
125
+ type: 'undo',
126
+ filename: 'v3_third.undo.sql',
127
+ script: 'drop table third;'
128
+ }
129
+ ]
130
+ Applying undo migration v3_third.undo.sql... done!
131
+ All done! Performed 1 undo migration
132
+ All applied versioned migrations:
133
+ ┌─────────┬────┬─────────────┬─────────────────┬────────────┬──────────────────────────────┐
134
+ │ (index) │ id │ type │ filename │ applied_by │ applied_at │
135
+ ├─────────┼────┼─────────────┼─────────────────┼────────────┼──────────────────────────────┤
136
+ │ 0 │ 1 │ 'versioned' │ 'v1_first.sql' │ 'postgres' │ '2024-11-25 15:25:55.799253' │
137
+ │ 1 │ 2 │ 'versioned' │ 'v2_second.sql' │ 'postgres' │ '2024-11-25 15:25:55.80306' │
138
+ │ 2 │ 3 │ 'versioned' │ 'v3_third.sql' │ 'postgres' │ '2024-11-25 15:25:55.80534' │
139
+ └─────────┴────┴─────────────┴─────────────────┴────────────┴──────────────────────────────┘
140
+ All applied repeatable migrations:
141
+ ┌─────────┬────┬──────────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
142
+ │ (index) │ id │ type │ filename │ applied_by │ applied_at │
143
+ ├─────────┼────┼──────────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
144
+ │ 0 │ 4 │ 'repeatable' │ 'v0_get_number.repeatable.sql' │ 'postgres' │ '2024-11-25 15:25:55.807375' │
145
+ └─────────┴────┴──────────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
146
+ Unapplied versioned migrations:
147
+ ┌─────────┐
148
+ │ (index) │
149
+ ├─────────┤
150
+ └─────────┘
125
151
  ```
126
152
 
127
153
  </details>
@@ -134,7 +160,7 @@ Validates the migration files and the stepwise_migration_events table.
134
160
  npx stepwise-migrations validate \
135
161
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
136
162
  --schema=myschema \
137
- --path=./db/migration/
163
+ --path=./test/migrations-template/
138
164
  ```
139
165
 
140
166
  <details>
@@ -143,18 +169,25 @@ npx stepwise-migrations validate \
143
169
 
144
170
  ```text
145
171
  Validation passed
146
- Migration state:
147
- ┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
148
- │ (index) │ id │ name │ applied_by │ applied_at │
149
- ├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
150
- │ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
151
- └─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
152
- Unapplied migrations:
153
- ┌─────────┬───────────────┐
154
- │ (index) │ filename │
155
- ├─────────┼───────────────┤
156
- │ 0 │ 'v2_auth.sql' │
157
- └─────────┴───────────────┘
172
+ All applied versioned migrations:
173
+ ┌─────────┬────┬─────────────┬─────────────────┬────────────┬──────────────────────────────┐
174
+ │ (index) │ id │ type filename │ applied_by │ applied_at │
175
+ ├─────────┼────┼─────────────┼─────────────────┼────────────┼──────────────────────────────┤
176
+ │ 0 │ 1 │ 'versioned' │ 'v1_first.sql' │ 'postgres' │ '2024-11-25 15:25:55.799253' │
177
+ │ 1 │ 2 │ 'versioned' │ 'v2_second.sql' │ 'postgres' │ '2024-11-25 15:25:55.80306' │
178
+ └─────────┴────┴─────────────┴─────────────────┴────────────┴──────────────────────────────┘
179
+ All applied repeatable migrations:
180
+ ┌─────────┬────┬──────────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
181
+ │ (index) │ id │ type │ filename │ applied_by │ applied_at │
182
+ ├─────────┼────┼──────────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
183
+ │ 0 │ 4 │ 'repeatable' │ 'v0_get_number.repeatable.sql' │ 'postgres' │ '2024-11-25 15:25:55.807375' │
184
+ └─────────┴────┴──────────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
185
+ Unapplied versioned migrations:
186
+ ┌─────────┬─────────────┬────────────────┐
187
+ │ (index) │ type │ filename │
188
+ ├─────────┼─────────────┼────────────────┤
189
+ │ 0 │ 'versioned' │ 'v3_third.sql' │
190
+ └─────────┴─────────────┴────────────────┘
158
191
  ```
159
192
 
160
193
  </details>
@@ -163,16 +196,17 @@ Unapplied migrations:
163
196
 
164
197
  <summary>Example output - script changed error</summary>
165
198
 
166
- ```sql
167
- Error: migration v1_connect_session_table.sql has been modified, aborting.
168
- "expire" timestamp(6) NOT NULL
169
- )
170
- WITH (OIDS=FALSE);
171
- -ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
172
-
173
- +ALTER TABLE "session" ADD CONSTRAINT "session_pkey" PRIMARY KEY ("sid") NOT DEFERRABLE INITIALLY IMMEDIATE;
199
+ ```
200
+ There were errors loading the migration state. Please fix the errors and try again.
201
+ - Versioned migration v1_first.sql has been altered. Cannot migrate in current state.
202
+
203
+ @@ -2,3 +2,5 @@ create table first (
204
+ id serial primary key,
205
+ name text not null
206
+ );
174
207
  +
175
- +ALTER TABLE "session" ADD INDEX "session_sid" ON "session" (sid);
208
+ +ALTER TABLE first ADD COLUMN age int;
209
+
176
210
  ```
177
211
 
178
212
  </details>
@@ -185,7 +219,7 @@ Shows the audit history for the migrations in the database.
185
219
  npx stepwise-migrations audit \
186
220
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
187
221
  --schema=myschema \
188
- --path=./db/migration/
222
+ --path=./test/migrations-template/
189
223
  ```
190
224
 
191
225
  <details>
@@ -193,14 +227,16 @@ npx stepwise-migrations audit \
193
227
  <summary>Example output</summary>
194
228
 
195
229
  ```text
196
- Audit history:
197
- ┌─────────┬────┬────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
198
- │ (index) │ id │ type name │ applied_by │ applied_at │
199
- ├─────────┼────┼────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
200
- │ 0 │ 1 │ 'up' │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
201
- │ 1 │ 2 │ 'up' │ 'v2_auth.sql' │ 'postgres' │ '2024-11-24 05:40:41.214732'
202
- │ 2 │ 3 │ 'down' │ 'v2_auth.undo.sql' │ 'postgres' │ '2024-11-24 05:41:34.541462'
203
- └─────────┴────┴────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
230
+ Event history:
231
+ ┌─────────┬────┬──────────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
232
+ │ (index) │ id │ type filename │ applied_by │ applied_at │
233
+ ├─────────┼────┼──────────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
234
+ │ 0 │ 1 │ 'versioned' │ 'v1_first.sql' │ 'postgres' │ '2024-11-25 15:25:55.799253' │
235
+ │ 1 │ 2 │ 'versioned' │ 'v2_second.sql' │ 'postgres' │ '2024-11-25 15:25:55.80306'
236
+ │ 2 │ 3 │ 'versioned' │ 'v3_third.sql' │ 'postgres' │ '2024-11-25 15:25:55.80534'
237
+ │ 3 │ 4 │ 'repeatable' │ 'v0_get_number.repeatable.sql' │ 'postgres' │ '2024-11-25 15:25:55.807375' │
238
+ │ 4 │ 5 │ 'undo' │ 'v3_third.undo.sql' │ 'postgres' │ '2024-11-25 15:25:56.588007' │
239
+ └─────────┴────┴──────────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
204
240
  ```
205
241
 
206
242
  </details>
@@ -215,7 +251,7 @@ Command:
215
251
  npx stepwise-migrations info \
216
252
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
217
253
  --schema=myschema \
218
- --path=./db/migration/
254
+ --path=./test/migrations-template/
219
255
  ```
220
256
 
221
257
  <details>
@@ -223,17 +259,30 @@ npx stepwise-migrations info \
223
259
  <summary>Example output</summary>
224
260
 
225
261
  ```text
226
- Migration state:
227
- ┌─────────┬────┬────────────────────────────────┬────────────┬──────────────────────────────┐
228
- │ (index) │ id │ name │ applied_by │ applied_at │
229
- ├─────────┼────┼────────────────────────────────┼────────────┼──────────────────────────────┤
230
- │ 0 │ 1 │ 'v1_connect_session_table.sql' │ 'postgres' │ '2024-11-24 05:40:41.211617' │
231
- └─────────┴────┴────────────────────────────────┴────────────┴──────────────────────────────┘
262
+ All applied versioned migrations:
263
+ ┌─────────┬────┬─────────────┬─────────────────┬────────────┬──────────────────────────────┐
264
+ │ (index) │ id │ type filename │ applied_by │ applied_at │
265
+ ├─────────┼────┼─────────────┼─────────────────┼────────────┼──────────────────────────────┤
266
+ │ 0 │ 1 │ 'versioned' │ 'v1_first.sql' │ 'postgres' │ '2024-11-25 15:25:55.799253' │
267
+ │ 1 │ 2 │ 'versioned' │ 'v2_second.sql' │ 'postgres' │ '2024-11-25 15:25:55.80306' │
268
+ └─────────┴────┴─────────────┴─────────────────┴────────────┴──────────────────────────────┘
269
+ All applied repeatable migrations:
270
+ ┌─────────┬────┬──────────────┬────────────────────────────────┬────────────┬──────────────────────────────┐
271
+ │ (index) │ id │ type │ filename │ applied_by │ applied_at │
272
+ ├─────────┼────┼──────────────┼────────────────────────────────┼────────────┼──────────────────────────────┤
273
+ │ 0 │ 4 │ 'repeatable' │ 'v0_get_number.repeatable.sql' │ 'postgres' │ '2024-11-25 15:25:55.807375' │
274
+ └─────────┴────┴──────────────┴────────────────────────────────┴────────────┴──────────────────────────────┘
275
+ Unapplied versioned migrations:
276
+ ┌─────────┬─────────────┬────────────────┐
277
+ │ (index) │ type │ filename │
278
+ ├─────────┼─────────────┼────────────────┤
279
+ │ 0 │ 'versioned' │ 'v3_third.sql' │
280
+ └─────────┴─────────────┴────────────────┘
232
281
  ```
233
282
 
234
283
  </details>
235
284
 
236
- ### Get Script
285
+ ### Get Applied Script
237
286
 
238
287
  Gets the script for the last applied migration.
239
288
  Can get the script for a specific migration if the `--filename` option is provided.
@@ -241,24 +290,22 @@ Can get the script for a specific migration if the `--filename` option is provid
241
290
  Command:
242
291
 
243
292
  ```bash
244
- npx stepwise-migrations get-script --filename v2_auth.sql \
293
+ npx stepwise-migrations get-applied-script --filename v1_first.sql \
245
294
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
246
295
  --schema=myschema \
247
- --path=./db/migration/
296
+ --path=./test/migrations-template/
248
297
  ```
249
298
 
250
299
  <details>
251
300
 
252
301
  <summary>Example output</summary>
253
302
 
254
- ```sql
255
- CREATE TABLE "users" (
256
- id bigserial primary key,
257
- email text unique not null,
258
- first_name text not null,
259
- last_name text not null,
260
- created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
303
+ ```text
304
+ create table first (
305
+ id serial primary key,
306
+ name text not null
261
307
  );
308
+
262
309
  ```
263
310
 
264
311
  </details>
@@ -272,7 +319,8 @@ Command:
272
319
  ```bash
273
320
  npx stepwise-migrations drop \
274
321
  --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
275
- --schema=myschema
322
+ --schema=myschema \
323
+ --path=./test/migrations-template/
276
324
  ```
277
325
 
278
326
  <details>
@@ -280,7 +328,9 @@ npx stepwise-migrations drop \
280
328
  <summary>Example output</summary>
281
329
 
282
330
  ```text
283
- Dropping the tables, schema and migration stepwise_migration_events table... done!
331
+ Dropping the tables, schema and migration history table... done!
284
332
  ```
285
333
 
286
334
  </details>
335
+
336
+ [comment]: <> (End of examples)
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const promises_1 = __importDefault(require("fs/promises"));
16
+ const utils_1 = require("../test/utils");
17
+ const next = (readme, from) => __awaiter(void 0, void 0, void 0, function* () {
18
+ const end = readme.indexOf("[comment]: <> (End of examples)");
19
+ if (readme.indexOf("```bash", from) < from ||
20
+ readme.indexOf("```bash", from) >= end) {
21
+ return { nextReadme: readme, nextIndex: -1 };
22
+ }
23
+ const startScript = readme.indexOf("```bash", from) + 8;
24
+ const endScript = readme.indexOf("```", startScript);
25
+ const script = readme.slice(startScript, endScript);
26
+ console.log(script);
27
+ const { output } = yield (0, utils_1.execute)(script);
28
+ // console.log(output);
29
+ const startOutput = readme.indexOf("```text", endScript) + 8;
30
+ const endOutput = readme.indexOf("```", startOutput);
31
+ return {
32
+ nextReadme: readme.slice(0, startOutput) + output + readme.slice(endOutput),
33
+ nextIndex: endOutput + 3,
34
+ };
35
+ });
36
+ (() => __awaiter(void 0, void 0, void 0, function* () {
37
+ let readme = yield promises_1.default.readFile("./README.md", "utf8");
38
+ const { output } = yield (0, utils_1.execute)(`npm exec stepwise-migrations drop -- \\
39
+ --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
40
+ --schema=myschema \\
41
+ --path=./test/migrations-template/ `);
42
+ let index = readme.indexOf("[comment]: <> (Start of examples)");
43
+ let i = 0;
44
+ while (true) {
45
+ const { nextReadme, nextIndex } = yield next(readme, index);
46
+ if (nextIndex === -1) {
47
+ break;
48
+ }
49
+ readme = nextReadme;
50
+ index = nextIndex;
51
+ i++;
52
+ // if (i > 1) break;
53
+ }
54
+ yield promises_1.default.writeFile("./README.md", readme);
55
+ }))();
package/dist/src/state.js CHANGED
@@ -103,7 +103,7 @@ const eventsToApplied = (events) => {
103
103
  }
104
104
  else if ((0, exports.getUndoFilename)(appliedVersionedMigrations[appliedVersionedMigrations.length - 1]
105
105
  .filename) !== event.filename) {
106
- errors.push("Events table is in a bad state: down migration does not match the most recently applied migration");
106
+ errors.push("Events table is in a bad state: undo migration does not match the most recently applied migration");
107
107
  break;
108
108
  }
109
109
  else {
package/dist/src/utils.js CHANGED
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.sliceFromFirstNull = exports.exitIfNotInitialized = exports.abortIfErrors = exports.fileExists = exports.printMigrationHistory = exports.printMigrationHistoryAndUnappliedMigrations = exports.readMigrationFiles = exports.filenameToType = exports.validateArgs = exports.usage = void 0;
16
16
  const promises_1 = __importDefault(require("fs/promises"));
17
+ const git_diff_1 = __importDefault(require("git-diff"));
17
18
  const path_1 = __importDefault(require("path"));
18
19
  exports.usage = `
19
20
  Usage: stepwise-migrations [command] [options]
@@ -34,7 +35,7 @@ Options:
34
35
  --path <path> The path to the migrations directory
35
36
  --ssl true/false Whether to use SSL for the connection (default: false)
36
37
  --napply Number of up migrations to apply (default: all)
37
- --nundo Number of down migrations to apply (default: 1)
38
+ --nundo Number of undo migrations to apply (default: 1)
38
39
 
39
40
  Example:
40
41
  npx stepwise-migrations migrate \\
@@ -87,7 +88,9 @@ const readMigrationFiles = (directory, appliedVersionedMigrations) => __awaiter(
87
88
  if (file &&
88
89
  file.type === "versioned" &&
89
90
  file.script !== appliedMigration.script) {
90
- errors.push(`Versioned migration ${appliedMigration.filename} has been altered. Cannot migrate in current state.`);
91
+ errors.push(`Versioned migration ${appliedMigration.filename} has been altered. Cannot migrate in current state. \n\n${(0, git_diff_1.default)(appliedMigration.script, file.script, {
92
+ color: true,
93
+ })}\n\n`);
91
94
  }
92
95
  }
93
96
  return { files: results, errors };
@@ -16,8 +16,8 @@ const node_assert_1 = __importDefault(require("node:assert"));
16
16
  const node_fs_1 = __importDefault(require("node:fs"));
17
17
  const node_test_1 = require("node:test");
18
18
  const utils_1 = require("./utils");
19
- const connection = "postgresql://postgres:postgres@127.0.0.1:5432/stepwise-db";
20
- const schema = "stepwise";
19
+ const connection = "postgresql://postgres:postgres@127.0.0.1:5432/mydb";
20
+ const schema = "testschema";
21
21
  const paths = {
22
22
  valid: "./test/migrations-valid",
23
23
  invalid: "./test/migrations-invalid",
@@ -1,9 +1,9 @@
1
1
  services:
2
- stepwise-db:
3
- container_name: stepwise-db
2
+ mydb:
3
+ container_name: mydb
4
4
  image: postgres:17.2
5
5
  environment:
6
- POSTGRES_DB: stepwise-db
6
+ POSTGRES_DB: mydb
7
7
  POSTGRES_USER: postgres
8
8
  POSTGRES_PASSWORD: postgres
9
9
  healthcheck:
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "stepwise-migrations",
3
- "version": "1.0.16",
3
+ "version": "1.0.17",
4
4
  "description": "",
5
- "main": "dist/src/index.js",
5
+ "main": "./dist/src/index.js",
6
6
  "scripts": {
7
7
  "build": "tsc",
8
8
  "test": "npm run build; glob -c \"tsx --test --test-reporter spec \" \"./test/**/*.test.ts\"",
@@ -24,7 +24,7 @@
24
24
  "tsx": "^4.19.2"
25
25
  },
26
26
  "bin": {
27
- "stepwise-migrations": "dist/src/index.js"
27
+ "stepwise-migrations": "./dist/src/index.js"
28
28
  },
29
29
  "dependencies": {
30
30
  "git-diff": "^2.0.6",
@@ -0,0 +1,50 @@
1
+ import fs from "fs/promises";
2
+ import { execute } from "../test/utils";
3
+
4
+ const next = async (readme: string, from: number) => {
5
+ const end = readme.indexOf("[comment]: <> (End of examples)");
6
+
7
+ if (
8
+ readme.indexOf("```bash", from) < from ||
9
+ readme.indexOf("```bash", from) >= end
10
+ ) {
11
+ return { nextReadme: readme, nextIndex: -1 };
12
+ }
13
+
14
+ const startScript = readme.indexOf("```bash", from) + 8;
15
+ const endScript = readme.indexOf("```", startScript);
16
+ const script = readme.slice(startScript, endScript);
17
+ console.log(script);
18
+ const { output } = await execute(script);
19
+ // console.log(output);
20
+ const startOutput = readme.indexOf("```text", endScript) + 8;
21
+ const endOutput = readme.indexOf("```", startOutput);
22
+ return {
23
+ nextReadme: readme.slice(0, startOutput) + output + readme.slice(endOutput),
24
+ nextIndex: endOutput + 3,
25
+ };
26
+ };
27
+
28
+ (async () => {
29
+ let readme = await fs.readFile("./README.md", "utf8");
30
+ const { output } = await execute(`npm exec stepwise-migrations drop -- \\
31
+ --connection=postgresql://postgres:postgres@127.0.0.1:5432/mydb \
32
+ --schema=myschema \\
33
+ --path=./test/migrations-template/ `);
34
+
35
+ let index = readme.indexOf("[comment]: <> (Start of examples)");
36
+ let i = 0;
37
+ while (true) {
38
+ const { nextReadme, nextIndex } = await next(readme, index);
39
+ if (nextIndex === -1) {
40
+ break;
41
+ }
42
+
43
+ readme = nextReadme;
44
+ index = nextIndex;
45
+ i++;
46
+ // if (i > 1) break;
47
+ }
48
+
49
+ await fs.writeFile("./README.md", readme);
50
+ })();
package/src/state.ts CHANGED
@@ -146,7 +146,7 @@ export const eventsToApplied = (
146
146
  ) !== event.filename
147
147
  ) {
148
148
  errors.push(
149
- "Events table is in a bad state: down migration does not match the most recently applied migration"
149
+ "Events table is in a bad state: undo migration does not match the most recently applied migration"
150
150
  );
151
151
  break;
152
152
  } else {
package/src/utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import fs from "fs/promises";
2
+ import gitDiff from "git-diff";
2
3
  import path from "path";
3
4
  import {
4
5
  AppliedMigration,
@@ -26,7 +27,7 @@ Options:
26
27
  --path <path> The path to the migrations directory
27
28
  --ssl true/false Whether to use SSL for the connection (default: false)
28
29
  --napply Number of up migrations to apply (default: all)
29
- --nundo Number of down migrations to apply (default: 1)
30
+ --nundo Number of undo migrations to apply (default: 1)
30
31
 
31
32
  Example:
32
33
  npx stepwise-migrations migrate \\
@@ -89,7 +90,15 @@ export const readMigrationFiles = async (
89
90
  file.script !== appliedMigration.script
90
91
  ) {
91
92
  errors.push(
92
- `Versioned migration ${appliedMigration.filename} has been altered. Cannot migrate in current state.`
93
+ `Versioned migration ${
94
+ appliedMigration.filename
95
+ } has been altered. Cannot migrate in current state. \n\n${gitDiff(
96
+ appliedMigration.script,
97
+ file.script,
98
+ {
99
+ color: true,
100
+ }
101
+ )}\n\n`
93
102
  );
94
103
  }
95
104
  }
@@ -2,8 +2,8 @@ import assert from "node:assert";
2
2
  import fs from "node:fs";
3
3
  import { beforeEach, describe, it } from "node:test";
4
4
  import { assertIncludesAll, assertIncludesExcludesAll, execute } from "./utils";
5
- const connection = "postgresql://postgres:postgres@127.0.0.1:5432/stepwise-db";
6
- const schema = "stepwise";
5
+ const connection = "postgresql://postgres:postgres@127.0.0.1:5432/mydb";
6
+ const schema = "testschema";
7
7
 
8
8
  const paths = {
9
9
  valid: "./test/migrations-valid",
@@ -2,3 +2,5 @@ create table first (
2
2
  id serial primary key,
3
3
  name text not null
4
4
  );
5
+
6
+ ALTER TABLE first ADD COLUMN age int;