Skip to content

Adding '+refinery NO TRANSACTION' directive #391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ This would stop developer 1's migration from ever running if you were using cont

refinery works by creating a table that keeps all the applied migrations' versions and their metadata. When you [run](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.run) the migrations `Runner`, refinery compares the applied migrations with the ones to be applied, checking for [divergent](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_abort_divergent) and [missing](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_abort_missing) and executing unapplied migrations.\
By default, refinery runs each migration in a single transaction. Alternatively, you can also configure refinery to wrap the entire execution of all migrations in a single transaction by setting [set_grouped](https://docs.rs/refinery/latest/refinery/struct.Runner.html#method.set_grouped) to true.
Directive `-- +refinery NO TRANSACTION` can be used to escape running a migration within a transaction. [!IMPORTANT]: `set_grouped` is incompatible with the no transaction directive.
The rust crate intentionally ignores new migration files until your sourcecode is rebuild. This prevents accidental migrations and altering the database schema without any code changes. We can also bake the migrations into the binary, so no additional files are needed when deployed.

### Rollback
Expand Down
4 changes: 4 additions & 0 deletions examples/migrations/V4__insert_entries_to_cars.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- +refinery NO TRANSACTION
BEGIN;
INSERT INTO cars(id, name, brand) VALUES (2, "muscle", "toyota");
COMMIT;
3 changes: 2 additions & 1 deletion refinery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ for more examples refer to the [examples](https://github.com/rust-db/refinery/tr

pub use refinery_core::config;
pub use refinery_core::{
error, load_sql_migrations, Error, Migration, Report, Runner, SchemaVersion, Target,
error, load_sql_migrations, Error, Migration, MigrationFlags, Report, Runner, SchemaVersion,
Target,
};
#[doc(hidden)]
pub use refinery_core::{AsyncMigrate, Migrate};
Expand Down
16 changes: 14 additions & 2 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,38 @@ mod mysql {
fn get_migrations() -> Vec<Migration> {
embed_migrations!("./tests/migrations");

let migration1 =
Migration::unapplied("V1__initial.rs", &migrations::V1__initial::migration()).unwrap();
let migration1 = Migration::unapplied(
"V1__initial.rs",
&migrations::V1__initial::migration(),
Default::default(),
)
.unwrap();

let migration2 = Migration::unapplied(
"V2__add_cars_and_motos_table.sql",
include_str!("./migrations/V1-2/V2__add_cars_and_motos_table.sql"),
Default::default(),
)
.unwrap();

let migration3 = Migration::unapplied(
"V3__add_brand_to_cars_table",
include_str!("./migrations/V3/V3__add_brand_to_cars_table.sql"),
Default::default(),
)
.unwrap();

let migration4 = Migration::unapplied(
"V4__add_year_to_motos_table.rs",
&migrations::V4__add_year_to_motos_table::migration(),
Default::default(),
)
.unwrap();

let migration5 = Migration::unapplied(
"V5__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();

Expand Down Expand Up @@ -471,6 +479,7 @@ mod mysql {
let migration = Migration::unapplied(
"V4__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();
let err = conn
Expand Down Expand Up @@ -507,6 +516,7 @@ mod mysql {
let migration = Migration::unapplied(
"V2__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();
let err = conn
Expand Down Expand Up @@ -550,12 +560,14 @@ mod mysql {
"city varchar(255)",
");"
),
Default::default(),
)
.unwrap();

let migration2 = Migration::unapplied(
"V2__add_cars_table",
include_str!("./migrations_missing/V2__add_cars_table.sql"),
Default::default(),
)
.unwrap();
let err = conn
Expand Down
16 changes: 14 additions & 2 deletions refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,38 @@ mod mysql_async {
fn get_migrations() -> Vec<Migration> {
embed_migrations!("./tests/migrations");

let migration1 =
Migration::unapplied("V1__initial.rs", &migrations::V1__initial::migration()).unwrap();
let migration1 = Migration::unapplied(
"V1__initial.rs",
&migrations::V1__initial::migration(),
Default::default(),
)
.unwrap();

let migration2 = Migration::unapplied(
"V2__add_cars_and_motos_table.sql",
include_str!("./migrations/V1-2/V2__add_cars_and_motos_table.sql"),
Default::default(),
)
.unwrap();

let migration3 = Migration::unapplied(
"V3__add_brand_to_cars_table",
include_str!("./migrations/V3/V3__add_brand_to_cars_table.sql"),
Default::default(),
)
.unwrap();

let migration4 = Migration::unapplied(
"V4__add_year_to_motos_table.rs",
&migrations::V4__add_year_to_motos_table::migration(),
Default::default(),
)
.unwrap();

let migration5 = Migration::unapplied(
"V5__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();

Expand Down Expand Up @@ -481,6 +489,7 @@ mod mysql_async {
let migration = Migration::unapplied(
"V4__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();

Expand Down Expand Up @@ -526,6 +535,7 @@ mod mysql_async {
let migration = Migration::unapplied(
"V2__add_year_field_to_cars",
"ALTER TABLE cars ADD year INTEGER;",
Default::default(),
)
.unwrap();

Expand Down Expand Up @@ -573,12 +583,14 @@ mod mysql_async {
"city varchar(255)",
");"
),
Default::default(),
)
.unwrap();

let migration2 = Migration::unapplied(
"V2__add_cars_table",
include_str!("./migrations_missing/V2__add_cars_table.sql"),
Default::default(),
)
.unwrap();
let err = pool
Expand Down
Loading
Loading