-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,098 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
@@ -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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.