Skip to content

Commit fec8a48

Browse files
committed
[#240] Improve cli ux and add cli example / docs
* BREAKING? Enhanced Store protocol with close alias for disconnect * BREAKING? connect now returns the store (this) * create (migration) fn now resolves absolute file path for migration * Enhanced docs for Store protocol * CLI can load config from file * CLI can load config from env * CLI can load config from cli args * CLI merges configs in this order: file, env, args * Added some tests for CLI parsing * Added status command, to display migration status: - connection info - so we know which server we are connecting to - migrations directory - so we know where we get migrations from - the latest applied migration - the list of not-applied migrations - any migrations applied and not present ?!
1 parent cc2648e commit fec8a48

File tree

14 files changed

+487
-106
lines changed

14 files changed

+487
-106
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ migratus.iml
1616
.lsp/.cache
1717
.portal
1818
.cpcache
19-
.calva/output-window/
19+
.calva
2020
.clj-kondo/

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
### Unreleased
2+
* Improved CLI interface
3+
* Improved CLI documentation
4+
* Added example project
5+
6+
### 1.5.3
7+
* [Improved CLI options](https://github.com/yogthos/migratus/pull/251)
8+
* More improvements in CLI
9+
110
### 1.5.2
211
* [CLI options](https://github.com/yogthos/migratus/pull/244)
312
* [logging improvements](https://github.com/yogthos/migratus/pull/245)

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ The environment variable associated with the `database.table` key will replace `
130130

131131
### Setup
132132

133+
Migratus can be used as a library in your project or as a CLI tool.
134+
There is also an option (from a third party) to run a migratus as native binary - for PostgreSQL only.
135+
136+
#### Using Migratus as a library in your project
137+
133138
- Add Migratus as a dependency to your `project.clj`
134139
```clojure
135140
:dependencies [[migratus <VERSION>]]
@@ -209,6 +214,14 @@ It is possible to pass a `java.sql.Connection` or `javax.sql.DataSource` in plac
209214
(def config {:db {:datasource (hk/make-datasource datasource-options)}})
210215
```
211216

217+
#### Using migratus as a command line (cli) tool
218+
219+
Migratus exposes a CLI interface via `migratus.cli` namespace.
220+
It uses [tools.cli](https://github.com/clojure/tools.cli) for argument parsing.
221+
222+
223+
224+
212225
#### Running as native image (Postgres only)
213226

214227
[PGMig](https://github.com/leafclick/pgmig) is a standalone tool built with migratus that's compiled as a standalone GraalVM native image executable.
@@ -386,7 +399,7 @@ This is intended for use with http://2ndquadrant.com/en/resources/pglogical/ and
386399
| `migratus.core/rollback` | Run `down` for the last migration that was run. |
387400
| `migratus.core/rollback-until-just-after` | Run `down` all migrations after `migration-id`. This only considers completed migrations, and will not migrate up. |
388401
| `migratus.core/up` | Run `up` for the specified migration ids. Will skip any migration that is already up. |
389-
| `migratus.core/down` | Run `down` for the specified migration ids. Will skip any migration that is already down.
402+
| `migratus.core/down` | Run `down` for the specified migration ids. Will skip any migration that is already down.
390403
| `migratus.core/reset` | Reset the database by down-ing all migrations successfully applied, then up-ing all migratinos.
391404
| `migratus.core/pending-list` | Returns a list of pending migrations. |
392405
| `migratus.core/migrate-until-just-before` | Run `up` for for any pending migrations which precede the given migration id (good for testing migrations). |

examples/postgres/README.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Example using migratus with Postgres
2+
3+
This is an example project that uses migratus to apply migrations to a PostgreSQL database.
4+
5+
TODO: Add instructions on how to use migratus via code.
6+
7+
## Using migratus via cli
8+
9+
### Setup your database
10+
11+
If you have an existing database, skip this step.
12+
This guide uses docker for PostgreSQL setup, but you can setup PostgreSQL any way you like.
13+
14+
Bellow is a short guide on how to manage a PostgreSQL db as a container for the purpose of the guide.
15+
16+
For more information on PostgreSQL see [postgres image](https://hub.docker.com/_/postgres/)
17+
18+
```shell
19+
# Run a postgresql instance as a container named migratus-pg.
20+
# We ask PostgreSQL to create a database named migratus_example_db
21+
docker run --name migratus-pg --detach -p 5432:5432 \
22+
-e POSTGRES_PASSWORD=migrate-me \
23+
-e POSTGRES_DB=migratus_example_db \
24+
-v migratus_data:/var/lib/postgresql/data \
25+
postgres:latest
26+
27+
# If all is well, we should see the container running
28+
docker ps
29+
30+
> CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
31+
> c37a91d27631 postgres:latest "docker-entrypoint.s…" 23 seconds ago Up 23 seconds 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp migratus-pg
32+
33+
# View the data volume for postgres
34+
docker volume ls
35+
36+
# And we can view the logs (in another terminal perhaps ?!)
37+
docker logs -f migratus-pg
38+
39+
# You can stop and start the container
40+
docker container stop migratus-pg
41+
docker container start migratus-pg
42+
43+
# We can remove the container once you are done, or if you want to reset everything
44+
docker container rm --force --volumes migratus-pg
45+
```
46+
47+
### Setup migratus cli
48+
49+
We use migratus cli via `deps.edn` aliases.
50+
See the `deps.edn` file in this project for details.
51+
52+
The file should look like this
53+
```clojure
54+
{:paths ["resources"]
55+
:deps {io.github.yogthos/migratus {:mvn/version "RELEASE"}
56+
org.postgresql/postgresql {:mvn/version "42.6.0"}}
57+
:aliases
58+
{:migratus {:jvm-opts ["-Dclojure.main.report=stderr"]
59+
:main-opts ["-m" "migratus.cli"]}}}
60+
```
61+
62+
If you have such a configuration, we can use `clojure` or `clj` tool to drive the CLI.
63+
Since Migratus is a clojure library, we need to run it via clojure like this `clojure -M:migratus --help`
64+
65+
There is also a bash script `migratus.sh` that does the same: `./migratus.sh --help`
66+
67+
68+
Commands with migratus
69+
70+
```shell
71+
72+
# We export the configuration as env variable, but we can use cli or file as well
73+
export MIGRATUS_CONFIG='{:store :database :db {:jdbcUrl "jdbc:postgresql://localhost:5432/migratus_example_db?user=postgres&password=migrate-me"}}'
74+
75+
clojure -M:migratus status
76+
77+
```

examples/postgres/deps.edn

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{;; migration sql files will be on "resources"
2+
:paths ["src" "resources"]
3+
;; we need migratus and postgresql jdbc driver on the classpath
4+
:deps {io.github.yogthos/migratus {:local/root "../.."}
5+
org.postgresql/postgresql {:mvn/version "42.6.0"}}
6+
:aliases
7+
{:migratus {:jvm-opts [;; print clojure errors to standard out instead of cli
8+
"-Dclojure.main.report=stderr"]
9+
;; Run migratus.cli -main fn
10+
:main-opts ["-m" "migratus.cli"]}}}

examples/postgres/migratus.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
# Run migratus passing all args to it
4+
clojure -J-Dclojure.main.report=stderr -M:migratus "$@"
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns migratus.examples.postgres
2+
(:require [migratus.cli :as cli]))
3+
4+
5+
6+
(comment
7+
8+
(apply cli/-main ["list"])
9+
10+
11+
12+
)

0 commit comments

Comments
 (0)