trek is a simple database migrator. It has the commands:
createapplylistset-migrated
$ trek create path/migration.sql
path/2020-07-09T06-21-12_migration.sqlIf a migration needs to run outside of a transaction, then the
--no-transaction flag may be passed:
$ trek create --no-transaction path/migration.sql
path/2020-07-09T06-21-12_migration_NO-TRANSACTION.sqlcreate NAME will make a VERSION-NAME file in the path pointed at by NAME using the current date time as %Y-%m-%dT%H-%M-%S.
create will return a exit code of 4 if the directory to the migration does not exist. Otherwise it will return 1 if an unknown error occurs.
$ trek apply DIRPATH
{
"migrations": [
{
"hash": "TBCJw6kwo28hSb39YPrLk9fI4IlToG17s+PwG8JQLUk=",
"version": "2020-07-12T06:21:21Z"
},
{
"hash": "A+Gb17iQkxBuDsOvNKfQfi30U5I510vfaa0E0UEnWjc=",
"version": "2020-07-12T06:21:27Z"
},
{
"hash": "ECrdIikOzM8ZZG1uCMXpYdQtTkGFO/+nLwmFi6PeVHo=",
"version": "2020-07-12T06:21:32Z"
}
],
"created_at": "2020-07-12T18:29:04.7395Z",
"id": "KgnJYCdJlarNU25q8SZDCPIfXxs="
}apply executes the the non-applied migrations (*.sql files) in DIRPATH together in a single READ COMMITTED transaction.
If the file name contains _NO-TRANSACTION as the last portion of the name
(before *.sql), then it will be run without a transaction. With multiple
files, they will be split into contiguous, ordered groups, so that adjacent
files will be run in the same transaction, and NO_TRANSACTION files will each
be run separately.
apply returns a JSON list of migrations entries as seen in the example above.
- If any of the sql files fail the transaction will be aborted and
trekwill return an exit code of1. - if any
*.sqlfiles cannot be parsed in theNAME-VERSION.sqlformatapplywill return exit code2.
trek set-migrated --start-version=2020-07-12T05:00:00 --end-version=2020-07-12T07:00:00 DIRPATH
{
"migrations": [
{
"hash": "TBCJw6kwo28hSb39YPrLk9fI4IlToG17s+PwG8JQLUk=",
"version": "2020-07-12T06:21:21Z"
},
{
"hash": "A+Gb17iQkxBuDsOvNKfQfi30U5I510vfaa0E0UEnWjc=",
"version": "2020-07-12T06:21:27Z"
},
{
"hash": "ECrdIikOzM8ZZG1uCMXpYdQtTkGFO/+nLwmFi6PeVHo=",
"version": "2020-07-12T06:21:32Z"
}
],
"created_at": "2020-07-12T18:29:04.7395Z",
"id": "KgnJYCdJlarNU25q8SZDCPIfXxs="
}set-migrated adds database entries for all of the migrations that have not been applied between the range specified by --start-version and --end-version inclusively.
If --start-version or --end-version are not specified then the first or last migration is used in the specified folder DIRPATH.
On success set-migrated returns the migration group of the migrations it added to the database.
- if any
*.sqlfiles cannot be parsed in theNAME-VERSION.sqlformatapplywill return exit code2.
trek list
[ {
"migrations": [
{
"hash": "TBCJw6kwo28hSb39YPrLk9fI4IlToG17s+PwG8JQLUk=",
"version": "2020-07-12T06:21:21Z"
},
{
"hash": "A+Gb17iQkxBuDsOvNKfQfi30U5I510vfaa0E0UEnWjc=",
"version": "2020-07-12T06:21:27Z"
},
{
"hash": "ECrdIikOzM8ZZG1uCMXpYdQtTkGFO/+nLwmFi6PeVHo=",
"version": "2020-07-12T06:21:32Z"
}
],
"created_at": "2020-07-12T18:29:04.7395Z",
"id": "KgnJYCdJlarNU25q8SZDCPIfXxs="
}
]
list will list all the applied migration groups.
You can configure trek by setting the standard postgres connection environment parameters or via command line arguments. See apply --help for details.