An agnostic tool that abstracts migration operations
- postgres
- mysql
Make sure you use the project's golang version by running
asdf install
Build project by running
make build
-
database: Database URL.
Set throughDB_HOST
environment variable or by using command flag--database
. -
driver: Driver name.
Defaults to postgres.
Set through command flag--driver
. -
dir: Migrations directory.
Set through command flag--dir
.
Requires: database, dir, driver
Execute migrations by running
./bin/mgr8 apply <up|down> <number_of_migrations>
- number_of_migrations: Number of migrations to run (Optional). If not specified, runs only one.
Mgr8 keeps a copy of the latest schema at .mgr8/reference.sql
. Copy the first schema version to this location, or simply run ./bin/mgr8 generate init <schemafile>
which will do the same.
Then run ./bin/mgr8 generate diff <schemafile>
to generate migrations with respect to the reference file. This will also update the reference.
When committing to a repository, check if the reference and the latest schema match. The command ./bin/mgr8 generate check <schemafile>
can be used, as it returns 0 if the files match.
To generate an empty migration (e.g. for DML), use ./bin/mgr8 generate empty
.
Run make install-tools
to install tooling dependencies.
Run a testing database with
docker compose up [-d] <database_name>
Available databases: postgres, mysql
Passing the -d
flag is optional and will run the container in detached mode, it won't block the terminal but you won't see database logs nor be able to close the container by using ctrl+c.
Point to database by setting env DB_HOST.
For postgres use DB_HOST=postgres://root:root@localhost:5432/database_name?sslmode=disable
Use make mock
to generate necessary mocks.
Use make test
, make display-coverage
and make coverage-report
.
To add a new mock, add new lines to the mock
command in the Makefile.
Executing migrations with postgres driver
./bin/mgr8 apply up --database=postgres://root:root@localhost:5432/core?sslmode=disable --dir=./migrations
Executing migrations with mysql driver
./bin/mgr8 apply up --database=root:root@tcp\(localhost:3306\)/core --dir=./migrations --driver=mysql