|
| 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 | +``` |
0 commit comments