Commit Graph

93 Commits

Author SHA1 Message Date
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
fabianlindfors
480bcf8af1 Add remove_index action 2022-01-12 01:12:08 +01:00
fabianlindfors
8e08941007 Let actions complete without transaction
Some migrations can be completed without needing a transaction for
atomicity. We should avoid transactions for DDL as far as possible to
avoid interfering with other queries and holding locks too long. It's
possible that all actions could be completed without a transaction but
I'm not sure of that yet.

Some migrations also won't work with transactions. For example dropping
an index using `DROP INDEX CONCURRENTLY` doesn't work inside a
transation.

Next step is to perform the same change for aborts.
2022-01-12 00:53:15 +01:00
fabianlindfors
8a130490a0 Use one transaction for each action during abort
This is a continuation on the last two commits. We want to keep
transactions as short-lived as possible when performing schema changes.
2022-01-12 00:06:54 +01:00
fabianlindfors
e27b5d2a23 Use one transaction for each action during completion
This builds on the previous commit to make the transactions even more
granular when completing a migration. Keeping the transaction span short
is important to avoid interfering with other queries.
2022-01-11 23:25:55 +01:00
fabianlindfors
c0ec6ca031 Use one transaction for each migration when completing
We previously used one transaction across all the migrations being
completed, which is not ideal from a locking perspective. Preferably we
want to keep the transactions as short-lived as possible to avoid
interfering with other queries. The next step will be to use one
transaction for each action.

To achieve this, we need to introduce a new intermediate state called
`Completing` which tracks which migrations have been completed so far.
2022-01-11 23:17:40 +01:00
fabianlindfors
bd7aab5675 Use CreateTableBuilder for alter_column tests 2022-01-11 22:35:14 +01:00
fabianlindfors
6f4a575c3c Add support for changing default value with alter_column
Changing the default value only affects the new schema, the old schema
will still use the existing default value. This is to make sure a new
default value doesn't break the old application.
2022-01-10 22:45:00 +01:00
fabianlindfors
c76ea1fba6 Add builder for create_table migration
The builder will help simplify the tests and avoid boilerplate updates
when need fields are added to the create_table migration. We might want
to add builders all migrations later.
2022-01-10 21:59:20 +01:00
fabianlindfors
435a40695a Bump crate version to 0.1.1 2022-01-08 16:09:23 +01:00
fabianlindfors
07774e87b6 Fix incorrect create_table examples 2022-01-06 16:54:35 +01:00
fabianlindfors
af2dbcb06e Remove truncation of migrations during abort
The migrations aren't updated so they shouldn't truncated during an
abort either. Also fixes the indexing for contexts during aborts.
2022-01-05 14:42:47 +01:00
fabianlindfors
54d9dd1b44 Fix incorrect type tag for alter_column migration 2022-01-05 14:15:40 +01:00
fabianlindfors
80cea7f94e Fix bad SQL query in abort for remove_column 2022-01-05 14:15:16 +01:00
fabianlindfors
afea8e304a Use IF EXISTS when dropping helpers 2022-01-05 14:13:54 +01:00
fabianlindfors
751c1975d7 Fix invalid example in README 2022-01-05 14:04:40 +01:00
fabianlindfors
27ec085337 Make foreign_keys optionally when creating tables 2022-01-05 14:03:56 +01:00
fabianlindfors
bf75e00c29 Add release badge to README 2022-01-04 16:22:23 +01:00
fabianlindfors
98cd2751ca Add cargo installation to README 2022-01-04 16:14:40 +01:00
fabianlindfors
3c977cb299 Fix incorrect output path in release pipeline
When using the --target flag, Cargo will output binaries to a different
folder: target/{TARGET}/release/reshape.
2022-01-04 15:50:27 +01:00
fabianlindfors
64d05dad46 Add missing target arg to build command 2022-01-04 15:35:09 +01:00
fabianlindfors
bd50686d32 Remove unused bimap dependency 2022-01-04 15:08:12 +01:00
fabianlindfors
6b21f8dc00 Add missing cleanup assertions to tests 2022-01-04 15:06:40 +01:00
fabianlindfors
cc07512f8c Fix clippy warnings 2022-01-03 22:15:53 +01:00
fabianlindfors
9b21ebfb86 Add check for dangling changes at end of tests
This checks for dangling temporary columns, triggers and function which
were used by migrations but should be removed once the migration has
been completed or aborted.
2022-01-03 22:09:16 +01:00
fabianlindfors
1ddfe95bf0 Add backtrace feature to anyhow
Improved backtrace support for errors in Rust is currently in progress:
https://github.com/rust-lang/rust/issues/53487. Adding the feature lets
anyhow add backtraces already which greatly improves readability of
errors in tests.
2022-01-03 22:03:10 +01:00
fabianlindfors
24c3f28b86 Refactor column tracking in Schema
Column tracking refers to how temporary columns for a column are
tracked. This changes the tracking to use a single vector, the first
element is the original column name and subsequent ones are temporary
columns.
2022-01-03 22:01:16 +01:00
fabianlindfors
33682db021 Remove schema param from migration complete function
Migrations shouldn't need to know about any temporary schema changes
as they should already have been applied once the migration is going
to be completed.
2022-01-03 21:56:13 +01:00
fabianlindfors
bffb602e02 Add Postgres compatibility note to README 2022-01-03 13:38:22 +01:00
fabianlindfors
f34e2b88f2 Adjust README 2022-01-03 13:24:55 +01:00
fabianlindfors
da8a5c3c77 Upgrade clap to 3.0.0 2022-01-03 00:05:54 +01:00
fabianlindfors
0bac1245cd Add context to errors 2022-01-03 00:03:23 +01:00
fabianlindfors
6e005144f4 Rename Context to MigrationContext
Context collides with the anyhow::Context trait.
2022-01-02 23:40:34 +01:00
fabianlindfors
ef82acd480 Return anyhow::Result from main function
This should improve the way errors are displayed for end users with a
list of causes.
2022-01-02 23:34:06 +01:00
fabianlindfors
78c0093412 Add intermediate Applying state
This state is set as soon as a migration is started, before any
migrations are run. If a migration unexpectedly fails and doesn't abort
properly (which should happen automatically), the state will be left
dangling in `Applying`. In that case, it's fine for the user to run
migrate again as all migrations are idempotent.
2022-01-02 23:29:39 +01:00
fabianlindfors
0fe6569b8c Automatically abort when a migration fails 2022-01-02 02:14:37 +01:00
fabianlindfors
c65985e5f9 Change update_schema method to not return Result
`update_schema`shouldn't be able to fail. Any failures should be caught
in the `migrate` method.
2022-01-02 01:33:36 +01:00
fabianlindfors
86254abe63 Add metadata to Cargo.toml 2022-01-01 17:35:16 +01:00