Skip to content

Commit f3e56fb

Browse files
committed
Add CLI
1 parent 6fbfbb1 commit f3e56fb

File tree

9 files changed

+1098
-73
lines changed

9 files changed

+1098
-73
lines changed

README.md

Lines changed: 130 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,130 @@ import { PostgresMigrate } "https://raw.githubusercontent.com/udibo/migrate/0.2.
2828
2929
## Usage
3030
31-
### CLI (TODO)
31+
### CLI
3232
3333
To use the command line interface, you must create a script that will initialize
34-
the Migrate instance and call the run command from [cli.ts](cli.ts).
34+
the Migrate instance and call the run command from [cli.ts](cli.ts). An example
35+
can be found [here](#postgres-cli).
3536
3637
See [deno docs](https://doc.deno.land/https/deno.land/x/migrate@0.2.0/cli.ts)
3738
for more information.
3839
40+
#### Command: init
41+
42+
Initializes the migration table for tracking which migrations have been applied.
43+
44+
```
45+
$ ./migrate.ts init
46+
Connecting to database
47+
Creating migration table if it does not exist
48+
Created migration table
49+
```
50+
51+
#### Command: load
52+
53+
Loads all migrations current path values into the migration table.
54+
55+
```
56+
$ ./migrate.ts load
57+
Connecting to database
58+
Acquiring migrate lock
59+
Acquired migrate lock
60+
Loading migrations
61+
2 new migrations found
62+
1 migration updated
63+
No migrations deleted
64+
Releasing migrate lock
65+
Released migrate lock
66+
Done
67+
```
68+
69+
#### Command: status
70+
71+
Outputs the status of all migrations. By default it just outputs the counts.
72+
73+
```
74+
$ ./migrate.ts status
75+
Connecting to database
76+
Checking loaded migrations
77+
Status:
78+
Total: 5
79+
Applied: 4
80+
File moved: 1
81+
File deleted: 1
82+
Not applied: 1
83+
```
84+
85+
If the --details or -d flag is provided, it will log the filenames of migrations
86+
that have not been applied or have been changed since being applied.
87+
88+
```
89+
$ ./migrate.ts status --details
90+
Connecting to database
91+
Checking loaded migrations
92+
Status:
93+
Total: 5
94+
Applied: 4
95+
File moved: 1
96+
2_user_add_kyle.sql -> 2_user_add_kyle.ts
97+
File deleted: 1
98+
3_user_add_staff.sql
99+
Not applied: 1
100+
4_user_add_column_email.sql
101+
```
102+
103+
#### Command: list
104+
105+
Outputs a list of migrations. By default it outputs all migrations.
106+
107+
```
108+
$ ./migrate.ts list
109+
Connecting to database
110+
Checking loaded migrations
111+
All migrations:
112+
0_user_create.sql
113+
applied at: Tue Nov 09 2021 12:10:32 GMT-0600 (Central Standard Time)
114+
1_user_add_admin.sql
115+
applied at: Wed Nov 11 2021 18:31:08 GMT-0600 (Central Standard Time)
116+
2_user_add_kyle.sql
117+
applied at: Sat Nov 13 2021 05:31:08 GMT-0600 (Central Standard Time)
118+
file moved to: 2_user_add_kyle.ts
119+
3_user_add_staff.sql
120+
applied at: Mon Nov 15 2021 15:31:08 GMT-0600 (Central Standard Time)
121+
file deleted
122+
4_user_add_column_email.sql
123+
not applied
124+
```
125+
126+
If the --filter flag is provided, it will filter the migrations to only include
127+
migrations that match the filter. The filter options are applied, unapplied,
128+
renamed, and deleted.
129+
130+
```
131+
$ ./migrate.ts list --filter=unapplied
132+
Unapplied migrations:
133+
4_user_add_column_email.sql
134+
```
135+
136+
#### Command: apply
137+
138+
Applies all unapplied migrations and outputs the filenames.
139+
140+
```
141+
$ ./migrate.ts apply
142+
Connecting to database
143+
Acquiring migrate lock
144+
Acquired migrate lock
145+
Checking loaded migrations
146+
2 unapplied migrations
147+
Applying migration: 0_user_create.sql
148+
Applying migration: 1_user_add_column_email.sql
149+
Finished applying all migrations
150+
Releasing migrate lock
151+
Released migrate lock
152+
Done
153+
```
154+
39155
### Postgres
40156
41157
Examples of how to use migrate with postgres can be found
@@ -51,8 +167,8 @@ for more information.
51167
52168
A basic migrate script that will apply all unapplied migrations.
53169
54-
To use this script, copy [migrate.ts](examples/postgres/migrate.ts) and update
55-
it with your migrate configuration.
170+
To use this script, copy [migrate_basic.ts](examples/postgres/migrate_basic.ts)
171+
and update it with your migrate configuration.
56172
57173
```
58174
$ ./migrate_basic.ts
@@ -72,16 +188,21 @@ Released advisory lock
72188
Done
73189
```
74190
75-
#### Postgres CLI (TODO)
191+
#### Postgres CLI
76192
77193
A CLI for the migration tool.
78194
79-
To use this script, copy [migrate_basic.ts](examples/postgres/migrate_basic.ts)
80-
and update it with your migrate configuration.
195+
To use this script, copy [migrate.ts](examples/postgres/migrate.ts) and update
196+
it with your migrate configuration.
81197
82-
```sh
198+
```
83199
$ ./migrate.ts status
84-
# TODO
200+
Connecting to database
201+
Checking loaded migrations
202+
Status:
203+
Total: 5
204+
Applied: 4
205+
Not applied: 1
85206
```
86207
87208
See [CLI](#cli) for more information about available CLI commands.

basic.ts

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,27 @@
1-
// add test coverage for it using Deno.run
2-
1+
import { applyMigrations, init, loadMigrations } from "./cli.ts";
32
import { Migrate } from "./migrate.ts";
43

54
export async function apply(migrate: Migrate): Promise<void> {
65
console.log("Connecting to database");
76
try {
87
await migrate.connect();
98
} catch (error) {
10-
console.error("Failed to connect to database");
9+
console.log("Failed to connect to database");
1110
throw error;
1211
}
1312

14-
console.log("Acquiring advisory lock");
13+
console.log("Acquiring migrate lock");
1514
const lock = await migrate.lock();
16-
console.log("Acquired advisory lock");
15+
console.log("Acquired migrate lock");
1716

18-
try {
19-
console.log("Creating migration table if it does not exist");
20-
await migrate.init();
21-
console.log("Created migration table");
22-
} catch {
23-
console.log("Migration table already exists");
24-
}
17+
await init(migrate);
18+
const migrations = await loadMigrations(migrate);
19+
await applyMigrations(migrate, migrations);
2520

26-
console.log("Loading migrations");
27-
await migrate.load();
28-
29-
console.log("Checking for unapplied migrations");
30-
const migrations = await migrate.getUnapplied();
31-
const migrationTerm = `migration${migrations.length !== 1 ? "s" : ""}`;
32-
console.log(
33-
`${migrations.length || "No"} unapplied ${migrationTerm} found`,
34-
);
35-
if (migrations.length) {
36-
for (const migration of migrations) {
37-
console.log(`Applying migration: ${migration.path}`);
38-
await migrate.apply(migration);
39-
}
40-
console.log("Finished applying all migrations");
41-
}
42-
43-
console.log("Releasing advisory lock");
21+
console.log("Releasing migrate lock");
4422
await lock.release();
45-
console.log("Released advisory lock");
46-
await migrate.end();
23+
console.log("Released migrate lock");
24+
4725
console.log("Done");
26+
await migrate.end();
4827
}

basic_test.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { resolve } from "./deps.ts";
1+
import { delay, resolve } from "./deps.ts";
22
import { PostgresMigrate } from "./postgres.ts";
33
import { assertEquals, test, TestSuite } from "./test_deps.ts";
44
import {
@@ -7,6 +7,7 @@ import {
77
InitializedMigrateTest,
88
options,
99
} from "./test_postgres.ts";
10+
import "./basic.ts";
1011

1112
const applyTests = new TestSuite({
1213
name: "apply",
@@ -48,18 +49,18 @@ test(
4849
decoder.decode(output),
4950
`\
5051
Connecting to database
51-
Acquiring advisory lock
52-
Acquired advisory lock
52+
Acquiring migrate lock
53+
Acquired migrate lock
5354
Creating migration table if it does not exist
5455
Created migration table
5556
Loading migrations
56-
Checking for unapplied migrations
57-
2 unapplied migrations found
57+
2 new migrations found
58+
2 unapplied migrations
5859
Applying migration: 0_user_create.sql
5960
Applying migration: 1_user_add_column_email.sql
6061
Finished applying all migrations
61-
Releasing advisory lock
62-
Released advisory lock
62+
Releasing migrate lock
63+
Released migrate lock
6364
Done
6465
`,
6566
);
@@ -75,6 +76,9 @@ test(applyTests, "applies unapplied migrations", async ({ migrate }) => {
7576
await migrate.load();
7677
const migrations = await migrate.getUnapplied();
7778
await migrate.apply(migrations[0]);
79+
await migrate.end();
80+
await delay(1);
81+
7882
const process = Deno.run({
7983
cmd: [
8084
resolve(migrate.migrationsDir, "../migrate_basic.ts"),
@@ -88,17 +92,19 @@ test(applyTests, "applies unapplied migrations", async ({ migrate }) => {
8892
decoder.decode(output),
8993
`\
9094
Connecting to database
91-
Acquiring advisory lock
92-
Acquired advisory lock
95+
Acquiring migrate lock
96+
Acquired migrate lock
9397
Creating migration table if it does not exist
9498
Migration table already exists
9599
Loading migrations
96-
Checking for unapplied migrations
97-
1 unapplied migration found
100+
No new migrations found
101+
No migrations updated
102+
No migrations deleted
103+
1 unapplied migration
98104
Applying migration: 1_user_add_column_email.sql
99105
Finished applying all migrations
100-
Releasing advisory lock
101-
Released advisory lock
106+
Releasing migrate lock
107+
Released migrate lock
102108
Done
103109
`,
104110
);
@@ -115,6 +121,9 @@ test(applyTests, "no unapplied migrations", async ({ migrate }) => {
115121
for (const migration of migrations) {
116122
await migrate.apply(migration);
117123
}
124+
await migrate.end();
125+
await delay(1);
126+
118127
const process = Deno.run({
119128
cmd: [
120129
resolve(migrate.migrationsDir, "../migrate_basic.ts"),
@@ -128,15 +137,17 @@ test(applyTests, "no unapplied migrations", async ({ migrate }) => {
128137
decoder.decode(output),
129138
`\
130139
Connecting to database
131-
Acquiring advisory lock
132-
Acquired advisory lock
140+
Acquiring migrate lock
141+
Acquired migrate lock
133142
Creating migration table if it does not exist
134143
Migration table already exists
135144
Loading migrations
136-
Checking for unapplied migrations
137-
No unapplied migrations found
138-
Releasing advisory lock
139-
Released advisory lock
145+
No new migrations found
146+
No migrations updated
147+
No migrations deleted
148+
No unapplied migrations
149+
Releasing migrate lock
150+
Released migrate lock
140151
Done
141152
`,
142153
);

0 commit comments

Comments
 (0)