1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
# SQL migrations in Praefect
SQL migration files are stored in `/internal/praefect/datastore/migrations`.
The underlying migration engine we use is [`github.com/rubenv/sql-migrate`](https://github.com/rubenv/sql-migrate).
To generate a new migration, run the `_support/new-migration` script from the top level of your Gitaly checkout.
Praefect SQL migrations should be applied automatically when you deploy Praefect. If you want to run them manually, run:
```shell
praefect -config /path/to/config.toml sql-migrate
```
By default, the migration will ignore any unknown migrations that are
not known by the Praefect binary.
The `-ignore-unknown=false` will disable this behavior:
```shell
praefect -config /path/to/config.toml sql-migrate -ignore-unknown=false
```
## Showing the status of migrations
To see which migrations have been applied, run:
```shell
praefect -config /path/to/config.toml sql-migrate-status
```
For example, the output may look like:
```plaintext
+----------------------------------------+--------------------------------------+
| MIGRATION | APPLIED |
+----------------------------------------+--------------------------------------+
| 20200109161404_hello_world | 2020-02-26 16:00:32.486129 -0800 PST |
| 20200113151438_1_test_migration | 2020-02-26 16:00:32.486871 -0800 PST |
| 20200224220728_job_queue | 2020-03-25 16:27:21.384917 -0700 PDT |
| 20200324001604_add_sql_election_tables | no |
| 20200401010230_add_some_table | unknown migration |
+----------------------------------------+--------------------------------------+
```
The first column contains the migration ID, and the second contains one of three items:
1. The date on which the migration was applied
1. `no` if the migration has not yet been applied
1. `unknown migration` if the migration is not known by the current Praefect binary
## Rolling back migrations
Rolling back SQL migrations in Praefect works a little differently
from ActiveRecord. It is a three step process.
### 1. Decide how many steps you want to roll back
Count the number of migrations you want to roll back.
### 2. Perform a dry run and verify that the right migrations are getting rolled back
```shell
praefect -config /path/to/config.toml sql-migrate-down NUM_ROLLBACK
```
This will perform a dry run and print the list of migrations that
would be rolled back. Verify that these are the migrations you want to
roll back.
### 3. Perform the rollback
We use the same command as before, but we pass `-f` to indicate we
want destructive changes (the rollbacks) to happen.
```shell
praefect -config /path/to/config.toml sql-migrate-down -f NUM_ROLLBACK
```
|