Commit Graph

93 Commits

Author SHA1 Message Date
fabianlindfors
c8cd6f438e Add quotes for column names 2024-03-17 17:29:04 +01:00
fabianlindfors
4c316aeacd Use safe NOT NULL checking during abort for complex remove column action 2024-01-21 22:59:15 +01:00
fabianlindfors
bf1fe13d5f Add reverse trigger for complex up and down tranformations 2024-01-20 02:00:39 +01:00
fabianlindfors
de44fdd5f0 Remove completed TODO comment 2024-01-11 23:17:57 +01:00
fabianlindfors
97397c4d0a Handle NOT NULL for column removal with complex down 2024-01-11 23:16:44 +01:00
fabianlindfors
6034dfebbd Add up option to create_table 2023-11-22 22:18:52 +01:00
fabianlindfors
4e5cc5c4ff Add support for complex up function in 2023-11-22 00:04:45 +01:00
fabianlindfors
b4baef2bc8 Add support for user defined types 2023-11-21 23:23:23 +01:00
fabianlindfors
9512c80de9 Add support for complex down function in 2023-11-21 23:22:20 +01:00
iko
7be33f7b7e
Added yet more quotes (#23) 2023-04-05 23:36:12 +02:00
iko
21b064532a
Added missing quotes (#21) 2023-01-28 17:06:22 +01:00
fabianlindfors
7be3afa336 Fix error not stopping migration 2023-01-09 17:26:57 +01:00
fabianlindfors
5a6aff1290 Fix migration files not sorted naturally
Closes #15
2022-07-26 16:00:08 +02:00
April Schleck
ce7a0b38ee
Output the failing filename when showing a parse error (#13)
Co-authored-by: April Schleck <april@exclusivelyducks.com>
2022-07-26 15:59:16 +02:00
fabianlindfors
df938fa4d4 Update docs and error messages with new CLI commands 2022-04-22 10:45:03 +02:00
fabianlindfors
88fca45c06 Add new custom action
This action enables migrations to run custom SQL and is meant to be used
when no other actions fit. It doesn't provide any guarantees for
zero-downtime.
2022-04-21 23:42:41 +02:00
fabianlindfors
696a6e57da Switch CLI command names
All the existing commands have been replaced with new commands which
read a bit better and leave better room for more commands in the future.

The following changes have been made:
- `reshape start` -> `reshape migration start`
- `reshape complete` -> `reshape migration complete`
- `reshape abort` -> `reshape migration abort`
- `reshape generate-schema-query` -> `reshape schema-query`

The old commands are still available but are marked as deprecated in the
help output and will be removed in the future. The README will be
updated later.

Closes #10
2022-04-19 23:24:29 +02:00
fabianlindfors
336e73394d Remove remove command from CLI
The `remove` command isn't very useful to end users and is also
dangerous as it will delete all data.
2022-02-04 17:56:36 +01:00
fabianlindfors
7cc4f23db9 Fix changing nullability not working with alter_column
The existing test to set a column as NOT NULL didn't actually check that
the constraint was added, which has also been fixed. I also added a new
test for the opposite change, making a NOT NULL column nullable. The
README has also been updated with a new example for setting a column as
NOT NULL.
2022-02-04 17:47:39 +01:00
fabianlindfors
a07979a97a Use .env file for environment variables 2022-02-03 23:57:02 +01:00
fabianlindfors
8129fdbf41 Add env var alternatives for all connection options
Also fixes a bug where the `--database` option would do nothing.
2022-02-03 23:48:59 +01:00
fabianlindfors
5529d224b6 Replace POSTGRES_URL env var with DB_URL
Also fixes the documentation which was incorrect before.
2022-02-03 23:27:44 +01:00
fabianlindfors
033ac5b2f1 Add version and description to CLI 2022-02-03 23:26:50 +01:00
fabianlindfors
3a4f0f7f9a Remove autocompletion of first migration
For Reshape to work with an existing database, the first migration
shouldn't autocomplete as then there can't be a zero-downtime
deployment. The user should instead use `--complete` if they don't care
about zero-downtime, for example when setting up a dev or CI
environment. We should update the `--complete` flag to apply and
complete each migration one by one for efficiency.
2022-02-01 09:50:02 +01:00
fabianlindfors
8888557c38 Check for new schema rather than old in helper
We want it to be possible for Reshape to be adopted for existing
databases with an existing schema. To make this possible, we can't check
if the schema path matches the current migration as there won't be a
current migration at first. Instead we'll compare against the target
migration.

The next step in making Reshape work with existing databases is to not
automatically complete the first migration. This will not be safe for an
existing application adopting Reshape.
2022-01-31 19:53:49 +01:00
fabianlindfors
4e93a39be0 Add remove_foreign_key action 2022-01-27 23:33:23 +01:00
fabianlindfors
6fcab3c88d Fix clippy warning for match on DB error 2022-01-25 21:45:48 +01:00
fabianlindfors
40fb39741d Add automatic retry of queries
Database queries will now be retried if possible using exponential
backoff with jitter. This should help protect against connection
problems and also timeouts caused by us now setting lock_timeout to
avoid blocking other queries. If such a timeout occurs, we should fail
to let waiting queries execute and then try again until the blocking
queries have completed.
2022-01-25 16:27:33 +01:00
fabianlindfors
c3369a4c12 Set lock_timeout to limit blocking of other queries
When attempting to acquire a lock for DDL queries, Postgres will wait
for any long-running queries to complete first. It will also block any
other queries that need access, which causes a queue of queries until we
release our lock. This has the effect of long-running queries blocking
other queries when we perform migrations, which is equivalent to
downtime if the query runs for long enough.

To avoid this, we set the `lock_timeout` setting. This puts an upper
bound on how long we will wait for a lock and also for how long other
queries can be blocked by a long-running query. The next step will be to
add automatic retries to handle timeouts gracefully.

Reference: https://medium.com/paypal-tech/postgresql-at-scale-database-schema-changes-without-downtime-20d3749ed680
2022-01-25 10:45:04 +01:00
fabianlindfors
814620a215 Add new add_foreign_key action 2022-01-21 18:00:41 +01:00
fabianlindfors
9d1b7c4082 Use helper function to find column in table 2022-01-21 17:14:12 +01:00
fabianlindfors
b6f1a0c6a4 Use real table and column names for foreign keys in create_table 2022-01-21 17:07:46 +01:00
fabianlindfors
4e0e112eb2 Safely remove indices when removing column 2022-01-20 19:31:21 +01:00
fabianlindfors
16f64181f0 Add new remove_enum action 2022-01-19 23:14:43 +01:00
fabianlindfors
953be4d3eb Add new create_enum action 2022-01-19 23:00:45 +01:00
fabianlindfors
1680906e16 Add support for unique and non-B-tree indices in alter_column 2022-01-16 00:36:57 +01:00
fabianlindfors
9cac25299a Refactor tests to use test framework
This has the added benefit of removing the need for builders and the
derive_builder dependency.
2022-01-15 16:15:31 +01:00
fabianlindfors
35a52ebd81 Fix issue where changes had to be set for alter_column
Some type of changes don't require anything in `changes`, for example
when changing the values of a column with `up` and `down`.
2022-01-15 15:53:55 +01:00
fabianlindfors
353a5c22b1 Add option to set index type for add_index 2022-01-14 12:34:38 +01:00
fabianlindfors
dd90cac03a Break out Index struct from add_index
This makes the format of `add_index` a bit more consistent with
`add_column` which has a nested definition for the column. This also
sets up for us to allow adding indices in `create_table`.
2022-01-14 11:53:31 +01:00
fabianlindfors
2a3fda7a90 Add option to create unique index with add_index 2022-01-14 11:44:12 +01:00
fabianlindfors
88da0d2364 Use advisory lock to avoid conflicts between instances
This protects all database access using a Postgres advisory lock. This
way we can avoid problems when two instances of Reshape try to change
the same database at the same time.
2022-01-14 11:25:32 +01:00
fabianlindfors
d28110611c Fix clippy warnings 2022-01-14 00:12:57 +01:00
fabianlindfors
2d04d33b95 Switch State struct into enum
With the old fields in the `State` broken out we can now make `State` a
proper enum, replacing the nested `Status`. The naming with both `State`
and `Status` was a bit confusing.
2022-01-14 00:12:01 +01:00
fabianlindfors
b1b79e26e5 Store version under separate key in reshape.data table
The version is not currently in used but will probably be useful later
when we want Reshape to be backwards compatbility. Having the version
stored in the database will then allow us to perform schema migrations
on the metadata of Reshape.
2022-01-13 23:56:06 +01:00
fabianlindfors
7c48974308 Use separate table for completed migrations
Before, the entire state was stored as a JSON encoded blob including all
completed migrations. This commits moves the migrations into a dedicated
table, which prevents the encoded from growing indefinitely. The size of
the state probably wouldn't have become a problem but it feels weird for
a schema migration tool to not use a proper schema.

Next, the version stored in the state struct will be moved to a separate
key in the `reshape.data` table and `State` will become an enum with the
same structure as `Status`. The naming right now is a bit confusing.
2022-01-13 23:46:07 +01:00
fabianlindfors
86b649d14a Copy indices to temporary columns for alter_column
Until now, using alter_column would cause indices to be lost as they
weren't copied to the temporary column. This fixes that for indices that
consist of one or more actual columns. It won't work for indices on
expressions.
2022-01-13 18:19:54 +01:00
fabianlindfors
9cd9bd395b Add quotes for all identifiers in queries 2022-01-13 00:34:42 +01:00
fabianlindfors
c787111c77 Remove transaction from abort
All migrations can be aborted idempotently at the moment and hence we
don't need to use a transaction for atomicity.
2022-01-12 13:15:26 +01:00
fabianlindfors
b617c8dcda Drop index concurrently when aborting add_index 2022-01-12 01:13:23 +01:00