Skip to content

Commit

Permalink
Add CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune committed Nov 16, 2021
1 parent 6fbfbb1 commit f3e56fb
Show file tree
Hide file tree
Showing 9 changed files with 1,098 additions and 73 deletions.
139 changes: 130 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,130 @@ import { PostgresMigrate } "https://raw.githubusercontent.com/udibo/migrate/0.2.
## Usage
### CLI (TODO)
### CLI
To use the command line interface, you must create a script that will initialize
the Migrate instance and call the run command from [cli.ts](cli.ts).
the Migrate instance and call the run command from [cli.ts](cli.ts). An example
can be found [here](#postgres-cli).
See [deno docs](https://doc.deno.land/https/deno.land/x/[email protected]/cli.ts)
for more information.
#### Command: init
Initializes the migration table for tracking which migrations have been applied.
```
$ ./migrate.ts init
Connecting to database
Creating migration table if it does not exist
Created migration table
```
#### Command: load
Loads all migrations current path values into the migration table.
```
$ ./migrate.ts load
Connecting to database
Acquiring migrate lock
Acquired migrate lock
Loading migrations
2 new migrations found
1 migration updated
No migrations deleted
Releasing migrate lock
Released migrate lock
Done
```
#### Command: status
Outputs the status of all migrations. By default it just outputs the counts.
```
$ ./migrate.ts status
Connecting to database
Checking loaded migrations
Status:
Total: 5
Applied: 4
File moved: 1
File deleted: 1
Not applied: 1
```
If the --details or -d flag is provided, it will log the filenames of migrations
that have not been applied or have been changed since being applied.
```
$ ./migrate.ts status --details
Connecting to database
Checking loaded migrations
Status:
Total: 5
Applied: 4
File moved: 1
2_user_add_kyle.sql -> 2_user_add_kyle.ts
File deleted: 1
3_user_add_staff.sql
Not applied: 1
4_user_add_column_email.sql
```
#### Command: list
Outputs a list of migrations. By default it outputs all migrations.
```
$ ./migrate.ts list
Connecting to database
Checking loaded migrations
All migrations:
0_user_create.sql
applied at: Tue Nov 09 2021 12:10:32 GMT-0600 (Central Standard Time)
1_user_add_admin.sql
applied at: Wed Nov 11 2021 18:31:08 GMT-0600 (Central Standard Time)
2_user_add_kyle.sql
applied at: Sat Nov 13 2021 05:31:08 GMT-0600 (Central Standard Time)
file moved to: 2_user_add_kyle.ts
3_user_add_staff.sql
applied at: Mon Nov 15 2021 15:31:08 GMT-0600 (Central Standard Time)
file deleted
4_user_add_column_email.sql
not applied
```
If the --filter flag is provided, it will filter the migrations to only include
migrations that match the filter. The filter options are applied, unapplied,
renamed, and deleted.
```
$ ./migrate.ts list --filter=unapplied
Unapplied migrations:
4_user_add_column_email.sql
```
#### Command: apply
Applies all unapplied migrations and outputs the filenames.
```
$ ./migrate.ts apply
Connecting to database
Acquiring migrate lock
Acquired migrate lock
Checking loaded migrations
2 unapplied migrations
Applying migration: 0_user_create.sql
Applying migration: 1_user_add_column_email.sql
Finished applying all migrations
Releasing migrate lock
Released migrate lock
Done
```
### Postgres
Examples of how to use migrate with postgres can be found
Expand All @@ -51,8 +167,8 @@ for more information.
A basic migrate script that will apply all unapplied migrations.
To use this script, copy [migrate.ts](examples/postgres/migrate.ts) and update
it with your migrate configuration.
To use this script, copy [migrate_basic.ts](examples/postgres/migrate_basic.ts)
and update it with your migrate configuration.
```
$ ./migrate_basic.ts
Expand All @@ -72,16 +188,21 @@ Released advisory lock
Done
```
#### Postgres CLI (TODO)
#### Postgres CLI
A CLI for the migration tool.
To use this script, copy [migrate_basic.ts](examples/postgres/migrate_basic.ts)
and update it with your migrate configuration.
To use this script, copy [migrate.ts](examples/postgres/migrate.ts) and update
it with your migrate configuration.
```sh
```
$ ./migrate.ts status
# TODO
Connecting to database
Checking loaded migrations
Status:
Total: 5
Applied: 4
Not applied: 1
```
See [CLI](#cli) for more information about available CLI commands.
Expand Down
43 changes: 11 additions & 32 deletions basic.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
// add test coverage for it using Deno.run

import { applyMigrations, init, loadMigrations } from "./cli.ts";
import { Migrate } from "./migrate.ts";

export async function apply(migrate: Migrate): Promise<void> {
console.log("Connecting to database");
try {
await migrate.connect();
} catch (error) {
console.error("Failed to connect to database");
console.log("Failed to connect to database");
throw error;
}

console.log("Acquiring advisory lock");
console.log("Acquiring migrate lock");
const lock = await migrate.lock();
console.log("Acquired advisory lock");
console.log("Acquired migrate lock");

try {
console.log("Creating migration table if it does not exist");
await migrate.init();
console.log("Created migration table");
} catch {
console.log("Migration table already exists");
}
await init(migrate);
const migrations = await loadMigrations(migrate);
await applyMigrations(migrate, migrations);

console.log("Loading migrations");
await migrate.load();

console.log("Checking for unapplied migrations");
const migrations = await migrate.getUnapplied();
const migrationTerm = `migration${migrations.length !== 1 ? "s" : ""}`;
console.log(
`${migrations.length || "No"} unapplied ${migrationTerm} found`,
);
if (migrations.length) {
for (const migration of migrations) {
console.log(`Applying migration: ${migration.path}`);
await migrate.apply(migration);
}
console.log("Finished applying all migrations");
}

console.log("Releasing advisory lock");
console.log("Releasing migrate lock");
await lock.release();
console.log("Released advisory lock");
await migrate.end();
console.log("Released migrate lock");

console.log("Done");
await migrate.end();
}
49 changes: 30 additions & 19 deletions basic_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { resolve } from "./deps.ts";
import { delay, resolve } from "./deps.ts";
import { PostgresMigrate } from "./postgres.ts";
import { assertEquals, test, TestSuite } from "./test_deps.ts";
import {
Expand All @@ -7,6 +7,7 @@ import {
InitializedMigrateTest,
options,
} from "./test_postgres.ts";
import "./basic.ts";

const applyTests = new TestSuite({
name: "apply",
Expand Down Expand Up @@ -48,18 +49,18 @@ test(
decoder.decode(output),
`\
Connecting to database
Acquiring advisory lock
Acquired advisory lock
Acquiring migrate lock
Acquired migrate lock
Creating migration table if it does not exist
Created migration table
Loading migrations
Checking for unapplied migrations
2 unapplied migrations found
2 new migrations found
2 unapplied migrations
Applying migration: 0_user_create.sql
Applying migration: 1_user_add_column_email.sql
Finished applying all migrations
Releasing advisory lock
Released advisory lock
Releasing migrate lock
Released migrate lock
Done
`,
);
Expand All @@ -75,6 +76,9 @@ test(applyTests, "applies unapplied migrations", async ({ migrate }) => {
await migrate.load();
const migrations = await migrate.getUnapplied();
await migrate.apply(migrations[0]);
await migrate.end();
await delay(1);

const process = Deno.run({
cmd: [
resolve(migrate.migrationsDir, "../migrate_basic.ts"),
Expand All @@ -88,17 +92,19 @@ test(applyTests, "applies unapplied migrations", async ({ migrate }) => {
decoder.decode(output),
`\
Connecting to database
Acquiring advisory lock
Acquired advisory lock
Acquiring migrate lock
Acquired migrate lock
Creating migration table if it does not exist
Migration table already exists
Loading migrations
Checking for unapplied migrations
1 unapplied migration found
No new migrations found
No migrations updated
No migrations deleted
1 unapplied migration
Applying migration: 1_user_add_column_email.sql
Finished applying all migrations
Releasing advisory lock
Released advisory lock
Releasing migrate lock
Released migrate lock
Done
`,
);
Expand All @@ -115,6 +121,9 @@ test(applyTests, "no unapplied migrations", async ({ migrate }) => {
for (const migration of migrations) {
await migrate.apply(migration);
}
await migrate.end();
await delay(1);

const process = Deno.run({
cmd: [
resolve(migrate.migrationsDir, "../migrate_basic.ts"),
Expand All @@ -128,15 +137,17 @@ test(applyTests, "no unapplied migrations", async ({ migrate }) => {
decoder.decode(output),
`\
Connecting to database
Acquiring advisory lock
Acquired advisory lock
Acquiring migrate lock
Acquired migrate lock
Creating migration table if it does not exist
Migration table already exists
Loading migrations
Checking for unapplied migrations
No unapplied migrations found
Releasing advisory lock
Released advisory lock
No new migrations found
No migrations updated
No migrations deleted
No unapplied migrations
Releasing migrate lock
Released migrate lock
Done
`,
);
Expand Down
Loading

0 comments on commit f3e56fb

Please sign in to comment.