Skip to content

Commit

Permalink
break refinery into refinery_core to remove code duplication (#70) cl…
Browse files Browse the repository at this point in the history
…oses #58
  • Loading branch information
jxs authored Mar 16, 2020
1 parent 3a96663 commit d7144e5
Show file tree
Hide file tree
Showing 32 changed files with 152 additions and 101 deletions.
9 changes: 0 additions & 9 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,6 @@ jobs:
steps:
- checkout
- run: cd refinery && cargo test --features tokio,mysql_async --test mysql_async -- --test-threads 1
# test-windows:
# executor:
# name: win/default
# shell: bash.exe
# steps:
# - checkout
# - run: choco install rust --version << pipeline.parameters.stable >>
# - run: choco install mingw
# - run: cd refinery && CARGO_NET_GIT_FETCH_WITH_CLI=true cargo test --lib
workflows:
version: 2
build_and_test:
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
members = [
"refinery",
"refinery_cli",
"refinery_core",
"refinery_macros"
]
33 changes: 10 additions & 23 deletions refinery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,22 @@ edition = "2018"

[features]
default = []
rusqlite-bundled = ["rusqlite", "rusqlite/bundled"]
rusqlite-bundled = ["refinery-core/rusqlite-bundled"]
rusqlite = ["refinery-core/rusqlite"]
postgres = ["refinery-core/postgres"]
mysql = ["refinery-core/mysql"]
tokio-postgres = ["refinery-core/tokio-postgres"]
mysql_async = ["refinery-core/mysql_async"]
tokio = ["refinery-core/tokio"]

[dependencies]
refinery-macros = { version = "0.2", path = "../refinery_macros" }
lazy_static = "1"
regex = "1"
log = "0.4"
chrono = "0.4"
serde = { version = "1", features = ["derive"] }
cfg-if = "0.1.10"
thiserror = "1"
async-trait = "0.1"
toml = "0.5"
siphasher = "0.3"

rusqlite = {version = "0.21", optional = true}
postgres = {version = "0.17", optional = true}
mysql = {version = "17", optional = true}
tokio-postgres = { version = "0.5", optional = true }
mysql_async = { version = "0.21", optional = true }

tokio = { version = "0.2", features = ["full"], optional = true }
refinery-core= { version = "0.2.0", path = "../refinery_core" }
refinery-macros= { version = "0.2.0", path = "../refinery_macros" }

[dev-dependencies]
barrel = { version = "0.6", features = ["sqlite3", "pg", "mysql"] }
futures = "0.3"
assert_cmd = "0.12"
predicates = "1"
tempfile = "3"

[package.metadata.docs.rs]
all-features = true
chrono = "0.4"
14 changes: 3 additions & 11 deletions refinery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Currently, [`Postgres`](https://crates.io/crates/postgres), [`Rusqlite`](https:/
[`include_migration_mods!`]: macro.include_migration_mods.html
### Example
```rust,no_run
```rust,ignore
use rusqlite::Connection;
mod embedded {
Expand All @@ -32,14 +32,6 @@ embedded::migrations::runner().run(&mut conn).unwrap();
for more examples refer to the [examples](https://github.com/rust-db/refinery/tree/master/examples)
*/

pub mod config;
mod drivers;
mod error;
mod runner;
mod traits;

pub use crate::error::Error;
pub use crate::runner::{AppliedMigration, Migration, Runner};
pub use crate::traits::r#async::AsyncMigrate;
pub use crate::traits::sync::Migrate;
pub use refinery_core::config;
pub use refinery_core::{AppliedMigration, AsyncMigrate, Error, Migrate, Migration, Runner};
pub use refinery_macros::{embed_migrations, include_migration_mods};
1 change: 1 addition & 0 deletions refinery/tests/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod mysql {
config::{migrate_from_config, Config, ConfigDbType},
Error, Migrate, Migration,
};
use refinery_core::mysql;
use std::process::Command;

mod embedded {
Expand Down
3 changes: 2 additions & 1 deletion refinery/tests/mysql_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ mod mysql_async {
use super::mod_migrations;
use chrono::{DateTime, Local};
use futures::FutureExt;
use mysql_async::prelude::Queryable;
use refinery::{
config::{migrate_from_config_async, Config, ConfigDbType},
AsyncMigrate, Error, Migration,
};
use refinery_core::mysql_async::prelude::Queryable;
use refinery_core::{mysql_async, tokio};
use std::panic::AssertUnwindSafe;

fn get_migrations() -> Vec<Migration> {
Expand Down
2 changes: 1 addition & 1 deletion refinery/tests/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ mod postgres {
use super::mod_migrations;
use assert_cmd::prelude::*;
use chrono::{DateTime, Local};
use postgres::{Client, NoTls};
use predicates::str::contains;
use refinery::{
config::{migrate_from_config, Config, ConfigDbType},
Error, Migrate, Migration,
};
use refinery_core::postgres::{Client, NoTls};
use std::process::Command;

mod embedded {
Expand Down
2 changes: 1 addition & 1 deletion refinery/tests/rusqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod rusqlite {
config::{migrate_from_config, Config, ConfigDbType},
Error, Migrate, Migration,
};
use rusqlite::{Connection, OptionalExtension, NO_PARAMS};
use refinery_core::rusqlite::{Connection, OptionalExtension, NO_PARAMS};
use std::fs::{self, File};
use std::process::Command;

Expand Down
3 changes: 2 additions & 1 deletion refinery/tests/tokio_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ mod tokio_postgres {
config::{migrate_from_config_async, Config, ConfigDbType},
AsyncMigrate, Error, Migration,
};
use refinery_core::tokio_postgres::NoTls;
use refinery_core::{tokio, tokio_postgres};
use std::panic::AssertUnwindSafe;
use tokio_postgres::NoTls;

fn get_migrations() -> Vec<Migration> {
let migration1 = Migration::from_filename(
Expand Down
10 changes: 5 additions & 5 deletions refinery_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ path = "src/main.rs"

[features]
default = ["mysql", "postgresql", "sqlite-bundled"]
postgresql = ["refinery/postgres"]
mysql = ["refinery/mysql"]
sqlite = ["refinery/rusqlite"]
sqlite-bundled = ["refinery/rusqlite-bundled"]
postgresql = ["refinery-core/postgres"]
mysql = ["refinery-core/mysql"]
sqlite = ["refinery-core/rusqlite"]
sqlite-bundled = ["refinery-core/rusqlite-bundled"]

[dependencies]
refinery = { version = "0.2", path = "../refinery", default-features = false }
refinery-core = { version = "0.2", path = "../refinery_core", default-features = false }
clap = { version = "2", features = ["wrap_help"] }
human-panic = "1"
toml = "0.5"
Expand Down
3 changes: 1 addition & 2 deletions refinery_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
mod cli;
mod migrate;
mod setup;
mod util;

use anyhow::Error;
use env_logger::{Builder, Target};
Expand All @@ -24,7 +23,7 @@ fn main() -> Result<(), Error> {
let mut builder = Builder::new();
builder
.format(|buf, record| writeln!(buf, "{}", record.args()))
.filter(Some("refinery::traits"), LevelFilter::Info)
.filter(Some("refinery_core::traits"), LevelFilter::Info)
.target(Target::Stdout)
.init();

Expand Down
8 changes: 3 additions & 5 deletions refinery_cli/src/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use std::path::Path;

use anyhow::{Context, Result};
use clap::ArgMatches;
use refinery::{
use refinery_core::{
config::{migrate_from_config, Config},
Migration,
find_migration_files, Migration, MigrationType,
};

use crate::util::find_migration_files;

pub fn handle_migration_command(args: &ArgMatches) -> Result<()> {
//safe to call unwrap as we specified default values
let config_location = args.value_of("config").unwrap();
Expand All @@ -35,7 +33,7 @@ fn run_files_migrations(
//safe to call unwrap as we specified default value
let path = arg.value_of("path").unwrap();
let path = Path::new(path);
let migration_files_path = find_migration_files(path)?;
let migration_files_path = find_migration_files(path, MigrationType::Sql)?;
let mut migrations = Vec::new();
for path in migration_files_path {
let sql = std::fs::read_to_string(path.as_path())
Expand Down
2 changes: 1 addition & 1 deletion refinery_cli/src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use anyhow::{anyhow, Result};
use clap::ArgMatches;
use refinery::config::{Config, ConfigDbType};
use refinery_core::config::{Config, ConfigDbType};
use std::fs::File;
use std::io::{self, Write};

Expand Down
25 changes: 0 additions & 25 deletions refinery_cli/src/util.rs

This file was deleted.

37 changes: 37 additions & 0 deletions refinery_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "refinery-core"
version = "0.2.0"
authors = ["Katharina Fey <kookie@spacekookie.de>", "João Oliveira <hello@jxs.pt>"]
description = "This crate should not be used directly, it is internaly related to Refinery"
license = "MIT OR Apache-2.0"
documentation = "https://docs.rs/refinery/"
repository = "https://github.com/rust-db/refinery"
edition = "2018"

[features]
default = []
rusqlite-bundled = ["rusqlite", "rusqlite/bundled"]

[dependencies]
lazy_static = "1"
regex = "1"
log = "0.4"
chrono = "0.4"
serde = { version = "1", features = ["derive"] }
cfg-if = "0.1.10"
thiserror = "1"
async-trait = "0.1"
toml = "0.5"
siphasher = "0.3"
walkdir = "2.3.1"

rusqlite = {version = "0.21", optional = true}
postgres = {version = "0.17", optional = true}
mysql = {version = "17", optional = true}
tokio-postgres = { version = "0.5", optional = true }
mysql_async = { version = "0.21", optional = true }

tokio = { version = "0.2", features = ["full"], optional = true }

[package.metadata.docs.rs]
all-features = true
File renamed without changes.
8 changes: 5 additions & 3 deletions refinery/src/config.rs → refinery_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use serde::{Deserialize, Serialize};
use std::fs;
use std::path::{Path, PathBuf};

//refinery config file used by migrate_from_config
// refinery config file used by migrate_from_config if migration from a Config struct is prefered instead of using the macros
// Config can either be instanced with [`Config::new`] or retrieved from a config file with [`Config::from_file_location`]
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub main: Main,
main: Main,
}

#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Debug)]
Expand All @@ -19,6 +20,7 @@ pub enum ConfigDbType {
}

impl Config {
/// create a new config instance
pub fn new(db_type: ConfigDbType) -> Config {
Config {
main: Main {
Expand Down Expand Up @@ -129,7 +131,7 @@ impl Config {
}

#[derive(Serialize, Deserialize, Debug)]
pub struct Main {
struct Main {
db_type: ConfigDbType,
db_path: Option<PathBuf>,
db_host: Option<String>,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions refinery_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*!
Powerful SQL migration toolkit for Rust.
`refinery` makes running migrations for different databases as easy as possible.
It works by running your migrations on a provided database connection, either by embedding them on your Rust code, or via `refinery_cli`.\
Currently, [`Postgres`](https://crates.io/crates/postgres), [`Rusqlite`](https://crates.io/crates/rusqlite), and [`Mysql`](https://crates.io/crates/mysql) are supported.\
`refinery` works best with [`Barrel`](https://crates.io/crates/barrel) but you can also have your migrations on .sql files or use any other Rust crate for schema generation.
## Usage
- Migrations can be defined in .sql files or Rust modules that must have a function called `migration()` that returns a [`std::string::String`]
- Migrations, both .sql files and Rust modules must be named in the format `V{1}__{2}.rs ` where `{1}` represents the migration version and `{2}` the name.
- Migrations can be run either by embedding them on your Rust code with [`embed_migrations!`] and [`include_migration_mods!`] macros, or via `refinery_cli`.
[`embed_migrations!`]: macro.embed_migrations.html
[`include_migration_mods!`]: macro.include_migration_mods.html
### Example
```rust,no_run
use rusqlite::Connection;
mod embedded {
use refinery::embed_migrations;
embed_migrations!("./tests/sql_migrations");
}
let mut conn = Connection::open_in_memory().unwrap();
embedded::migrations::runner().run(&mut conn).unwrap();
```
for more examples refer to the [examples](https://github.com/rust-db/refinery/tree/master/examples)
*/

pub mod config;
mod drivers;
mod error;
mod runner;
mod traits;
mod util;

pub use crate::error::Error;
pub use crate::runner::{AppliedMigration, Migration, Runner};
pub use crate::traits::r#async::AsyncMigrate;
pub use crate::traits::sync::Migrate;
pub use crate::util::{find_migration_files, MigrationType};

#[cfg(feature = "rusqlite")]
pub use rusqlite;

#[cfg(feature = "postgres")]
pub use postgres;

#[cfg(feature = "mysql")]
pub use mysql;

#[cfg(feature = "tokio-postgres")]
pub use tokio_postgres;

#[cfg(feature = "mysql_async")]
pub use mysql_async;

#[cfg(feature = "tokio")]
pub use tokio;
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit d7144e5

Please sign in to comment.