Skip to content

Commit

Permalink
Merge branch 'main' into int8-versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jxs authored Aug 14, 2024
2 parents 0beea31 + 068858e commit d8e0182
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 124 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[env]
LIBSQLITE3_FLAGS = { value = "-DSQLITE_ENABLE_MATH_FUNCTIONS", force = true }
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[workspace]
resolver = "2"
members = [
"refinery",
"refinery_cli",
"refinery_core",
"refinery_macros",
"examples"
"examples",
]
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,18 @@ By default, refinery runs each migration in a single transaction. Alternatively,
refinery's design was based on [flyway](https://flywaydb.org/) and so, it shares its earlier [philosophy](https://web.archive.org/web/20191226033347/https://flywaydb.org/documentation/command/undo#important-notes) on undo/rollback migrations.
Flyway has since changed it's opinion but refinery hasn't. To undo/rollback a migration, you have to generate a new one and write specifically what you want to undo.

## MSRV
## Support for Additional Database Drivers

refinery aims to support stable Rust, the previous Rust version, and nightly.
While initially it seemed beneficial to support as many aditional drivers as possible in this repo, with the current bandwidth available by the maintainers it's preferable to create them and maintain them on external repositories (see [here](https://github.com/rust-db/refinery/pull/264#issuecomment-1419198667) for context).

Notable external database drivers:

- [Klickhouse](https://github.com/Protryon/klickhouse) ([Clickhouse](https://clickhouse.tech/docs/en/) database driver with refinery support)

## Async

Starting with version 0.2 refinery supports [tokio-postgres](https://crates.io/crates/tokio-postgres), [`mysql_async`](https://crates.io/crates/mysql_async)
and [Tiberius](https://github.com/prisma/tiberius)
For Rusqlite, the best way to run migrations in an async context is to run them inside tokio's [`spawn_blocking`](https://docs.rs/tokio/1.10.0/tokio/task/fn.spawn_blocking.html) for example.
## MSRV

refinery aims to support stable Rust, the previous Rust version, and nightly.

## Contributing

Expand Down
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enums = ["refinery/enums"]

[dependencies]
refinery = { path = "../refinery", features = ["rusqlite"] }
rusqlite = "0.29"
rusqlite = "0.31"
barrel = { version = "0.7", features = ["sqlite3"] }
log = "0.4"
env_logger = "0.11"
104 changes: 35 additions & 69 deletions refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ mod postgres {
use assert_cmd::prelude::*;
use predicates::str::contains;
use refinery::{
config::{Config, ConfigDbType},
embed_migrations,
error::Kind,
Migrate, Migration, Runner, Target,
config::Config, embed_migrations, error::Kind, Migrate, Migration, Runner, Target,
};
use refinery_core::postgres::{Client, NoTls};
use std::process::Command;
use std::str::FromStr;
use time::OffsetDateTime;

const DEFAULT_TABLE_NAME: &str = "refinery_schema_history";
Expand All @@ -37,6 +35,7 @@ mod postgres {
embed_migrations!("./tests/migrations_int8");
}


fn db_uri() -> String {
std::env::var("DB_URI").unwrap_or("postgres://postgres@localhost:5432/postgres".to_string())
}
Expand Down Expand Up @@ -74,36 +73,32 @@ mod postgres {
vec![migration1, migration2, migration3, migration4, migration5]
}

fn clean_database() {
let mut client =
Client::connect("postgres://postgres@localhost:5432/template1", NoTls).unwrap();
fn prep_database() {
let uri = db_uri();

let mut client = Client::connect(&db_uri(), NoTls).unwrap();

client
.execute(
"SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname='postgres'",
&[],
)
.execute("DROP SCHEMA IF EXISTS public CASCADE", &[])
.unwrap();
client
.execute("CREATE SCHEMA IF NOT EXISTS public", &[])
.unwrap();
client.execute("DROP DATABASE POSTGRES", &[]).unwrap();
client.execute("CREATE DATABASE POSTGRES", &[]).unwrap();
}

fn run_test<T>(test: T)
where
T: FnOnce() + std::panic::UnwindSafe,
T: FnOnce(),
{
let result = std::panic::catch_unwind(test);

clean_database();
prep_database();

assert!(result.is_ok())
test();
}

#[test]
fn report_contains_applied_migrations() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -132,8 +127,7 @@ mod postgres {
#[test]
fn creates_migration_table() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
embedded::migrations::runner().run(&mut client).unwrap();
for row in &client
.query(
Expand All @@ -154,8 +148,7 @@ mod postgres {
#[test]
fn creates_migration_table_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(true)
Expand All @@ -181,8 +174,7 @@ mod postgres {
#[test]
fn applies_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();
embedded::migrations::runner().run(&mut client).unwrap();
client
.execute(
Expand Down Expand Up @@ -233,8 +225,7 @@ mod postgres {
#[test]
fn applies_migration_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(false)
Expand All @@ -259,8 +250,7 @@ mod postgres {
#[test]
fn updates_schema_history() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand All @@ -280,8 +270,7 @@ mod postgres {
#[test]
fn updates_schema_history_grouped_transaction() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner()
.set_grouped(false)
Expand All @@ -303,8 +292,7 @@ mod postgres {
#[test]
fn updates_to_last_working_if_not_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let result = broken::migrations::runner().run(&mut client);

Expand Down Expand Up @@ -348,8 +336,7 @@ mod postgres {
#[test]
fn doesnt_update_to_last_working_if_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let result = broken::migrations::runner()
.set_grouped(true)
Expand All @@ -368,8 +355,7 @@ mod postgres {
#[test]
fn gets_applied_migrations() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -397,8 +383,7 @@ mod postgres {
#[test]
fn applies_new_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -429,8 +414,7 @@ mod postgres {
#[test]
fn migrates_to_target_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Version(3))
Expand Down Expand Up @@ -465,8 +449,7 @@ mod postgres {
#[test]
fn migrates_to_target_migration_grouped() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Version(3))
Expand Down Expand Up @@ -502,8 +485,7 @@ mod postgres {
#[test]
fn aborts_on_missing_migration_on_filesystem() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -536,8 +518,7 @@ mod postgres {
#[test]
fn aborts_on_divergent_migration() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

embedded::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -571,8 +552,7 @@ mod postgres {
#[test]
fn aborts_on_missing_migration_on_database() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

missing::migrations::runner().run(&mut client).unwrap();

Expand Down Expand Up @@ -616,11 +596,7 @@ mod postgres {
#[test]
fn migrates_from_config() {
run_test(|| {
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
let mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -656,11 +632,7 @@ mod postgres {
#[test]
fn migrate_from_config_report_contains_migrations() {
run_test(|| {
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
let mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand Down Expand Up @@ -696,11 +668,7 @@ mod postgres {
#[test]
fn migrate_from_config_report_returns_last_applied_migration() {
run_test(|| {
let mut config = Config::new(ConfigDbType::Postgres)
.set_db_name("postgres")
.set_db_user("postgres")
.set_db_host("localhost")
.set_db_port("5432");
let mut config = Config::from_str(&db_uri()).unwrap();

let migrations = get_migrations();
let runner = Runner::new(&migrations)
Expand All @@ -725,8 +693,7 @@ mod postgres {
#[test]
fn doesnt_run_migrations_if_fake() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::Fake)
Expand Down Expand Up @@ -760,8 +727,7 @@ mod postgres {
#[test]
fn doesnt_run_migrations_if_fake_version() {
run_test(|| {
let mut client =
Client::connect("postgres://postgres@localhost:5432/postgres", NoTls).unwrap();
let mut client = Client::connect(&db_uri(), NoTls).unwrap();

let report = embedded::migrations::runner()
.set_target(Target::FakeVersion(2))
Expand Down
4 changes: 2 additions & 2 deletions refinery_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ int8-versions = ["refinery-core/int8-versions"]
[dependencies]
refinery-core = { version = "0.8.14", path = "../refinery_core", default-features = false, features = ["toml"] }
clap = { version = "4", features = ["derive"] }
human-panic = "1.1.3"
human-panic = "2"
toml = "0.8"
env_logger = "0.10"
env_logger = "0.11"
log = "0.4"
anyhow = "1"
regex = "1"
Expand Down
4 changes: 2 additions & 2 deletions refinery_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ pub struct MigrateArgs {
#[clap(long, default_value = "refinery_schema_history")]
pub table_name: String,

/// Migrate even if divergent migrations are found
/// Should abort if divergent migrations are found
#[clap(short)]
pub divergent: bool,

/// Migrate even if missing migrations are found
/// Should abort if missing migrations are found
#[clap(short)]
pub missing: bool,
}
6 changes: 3 additions & 3 deletions refinery_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ description = "This crate should not be used directly, it is internally related
license = "MIT OR Apache-2.0"
documentation = "https://docs.rs/refinery/"
repository = "https://github.com/rust-db/refinery"
edition = "2018"
edition = "2021"

[features]
default = []
rusqlite-bundled = ["rusqlite", "rusqlite/bundled"]
tiberius = ["dep:tiberius", "futures", "tokio", "tokio/net"]
tiberius-config = ["tiberius", "tokio", "tokio-util"]
tiberius-config = ["tiberius", "tokio", "tokio-util", "serde"]
tokio-postgres = ["dep:tokio-postgres", "tokio", "tokio/rt"]
mysql_async = ["dep:mysql_async"]
serde = ["dep:serde"]
Expand All @@ -33,7 +33,7 @@ walkdir = "2.3.1"
rusqlite = { version = ">= 0.23, <= 0.31", optional = true }
postgres = { version = ">=0.17, <= 0.19", optional = true }
tokio-postgres = { version = ">= 0.5, <= 0.7", optional = true }
mysql = { version = ">= 21.0.0, <= 24", optional = true, default-features = false, features = ["minimal"] }
mysql = { version = ">= 21.0.0, <= 25", optional = true, default-features = false, features = ["minimal"] }
mysql_async = { version = ">= 0.28, <= 0.34", optional = true, default-features = false, features = ["minimal"] }
tiberius = { version = ">= 0.7, <= 0.12", optional = true, default-features = false }
tokio = { version = "1.0", optional = true }
Expand Down
Loading

0 comments on commit d8e0182

Please sign in to comment.