Commit Graph

130 Commits

Author SHA1 Message Date
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
ef6fe64918 Fix incorrect minimum Rust version 2022-02-03 14:25:06 +01:00
fabianlindfors
8b2efd9624 Add minimum Rust version to Cargo.toml 2022-02-03 14:22:58 +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
5984c1c507 Fix typo in README 2022-01-25 22:26:03 +01:00
fabianlindfors
3a6c57c145 Add JSON extraction example to README 2022-01-25 22:14:47 +01:00
fabianlindfors
f6d6f28546 Fix misaligned delimiters in test output 2022-01-25 21:55:46 +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
f754ac0abc Version 0.4.0 2022-01-24 11:44:26 +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
b3766ba2b6 Add link to blog post to README 2022-01-16 23:24:29 +01:00
fabianlindfors
49b01bbb28 Version 0.3.1 2022-01-16 23:00:46 +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
a7d70f80b8 Use TOML for migrations in test framework
Previously we set up migrations directly as structs which doesn't match
how users will create them. For the new test framework, we'll instead
write TOML for migrations. This brings the tests closer to how an end
user will interact with Reshape and also lets us write tests to match
the examples in the documentation.
2022-01-15 14:47:28 +01:00
fabianlindfors
945e6374d8 Add test framework
This reduces the amount of boilerplate required to run a test and also
has the added benefit of extending tests as we can now easily test both the
completion and abort paths. So far two tests have been ported over,
create_table and add_column, with the rest to follow.

The framework also improves the output of tests, adding sections clearly
indicating in which part of the migration process a test failed.
2022-01-14 23:49:19 +01:00
fabianlindfors
0e0c24b789 Adjust README introduction and disclaimer
Tones down the disclaimer a bit as I feel reasonable certain Reshape
won't lose any data. Also adds some details about locking and downtime
to the introduction.
2022-01-14 13:14:16 +01:00
fabianlindfors
f3ead43cde Bump Dockerfile Rust version to 1.58 2022-01-14 12:59:01 +01:00
fabianlindfors
17376ce3b7 Version 0.3.0 2022-01-14 12:38:21 +01:00
fabianlindfors
353a5c22b1 Add option to set index type for add_index 2022-01-14 12:34:38 +01:00
fabianlindfors
5691ad261b Use CreateTableBuilder in failure test 2022-01-14 11:58:21 +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
b86e6d53ca Run lint and test actions on PR 2022-01-13 14:32:57 +01:00
Fabian Lindfors
d76dbab5ba
Merge pull request #3 from barrywoolgar/patch-1
Explain the new concept at the top of the README
2022-01-13 14:27:24 +01:00
Barry Woolgar
b3ead8b2e2
Explain the new concept at the top of the README
This allows people new to the project to understand how Reshape works before getting into the technical detail of setting it up and using it (which they may or may not want to do just yet).
2022-01-13 10:36:25 +00:00
fabianlindfors
9cd9bd395b Add quotes for all identifiers in queries 2022-01-13 00:34:42 +01:00
fabianlindfors
0dd8272e9f Publish to crates.io with each release 2022-01-13 00:07:43 +01:00
fabianlindfors
24e1d4b902 Version 0.2.0 2022-01-12 13:19:27 +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