diff --git a/migra/src/managers.rs b/migra/src/managers.rs index 06ce79e..3a7c4e1 100644 --- a/migra/src/managers.rs +++ b/migra/src/managers.rs @@ -25,7 +25,10 @@ pub trait ManageTransaction: BatchExecute { pub trait ManageMigrations: BatchExecute { fn apply_sql(&mut self, sql: &str) -> MigraResult<()> { - self.batch_execute(sql).map_err(|_| Error::FailedApplySql) + self.batch_execute(sql).map_err(|err| { + dbg!(err); + Error::FailedApplySql + }) } fn create_migrations_table(&mut self) -> MigraResult<()>; diff --git a/migra_cli/src/app.rs b/migra_cli/src/app.rs index f7dc945..7baccb5 100644 --- a/migra_cli/src/app.rs +++ b/migra_cli/src/app.rs @@ -1,5 +1,5 @@ use crate::commands; -use crate::error::{MigraResult, StdResult}; +use crate::error::MigraResult; use crate::opts::Command; use crate::AppOpt; use crate::Config; @@ -24,7 +24,7 @@ impl App { Config::read(self.config_path()) } - pub fn run_command(&self) -> StdResult<()> { + pub fn run_command(&self) -> migra::StdResult<()> { match self.app_opt.command.clone() { Command::Init => { commands::initialize_migra_manifest(self)?; diff --git a/migra_cli/src/client.rs b/migra_cli/src/client.rs index fa87919..cf0a272 100644 --- a/migra_cli/src/client.rs +++ b/migra_cli/src/client.rs @@ -1,4 +1,5 @@ use crate::config::SupportedDatabaseClient; +use crate::Config; #[cfg(feature = "mysql")] use migra::clients::MysqlClient; #[cfg(feature = "postgres")] @@ -10,19 +11,38 @@ use migra::clients::{AnyClient, OpenDatabaseConnection}; pub fn create( client_kind: &SupportedDatabaseClient, connection_string: &str, + migrations_table_name: &str, ) -> migra::Result { let client: AnyClient = match client_kind { #[cfg(feature = "postgres")] - SupportedDatabaseClient::Postgres => Box::new(PostgresClient::new(&connection_string)?), + SupportedDatabaseClient::Postgres => Box::new(PostgresClient::manual( + connection_string, + migrations_table_name, + )?), #[cfg(feature = "mysql")] - SupportedDatabaseClient::Mysql => Box::new(MysqlClient::new(&connection_string)?), + SupportedDatabaseClient::Mysql => Box::new(MysqlClient::manual( + connection_string, + migrations_table_name, + )?), #[cfg(feature = "sqlite")] - SupportedDatabaseClient::Sqlite => Box::new(SqliteClient::new(&connection_string)?), + SupportedDatabaseClient::Sqlite => Box::new(SqliteClient::manual( + connection_string, + migrations_table_name, + )?), }; Ok(client) } +pub fn create_from_config(config: &Config) -> migra::StdResult { + create( + &config.database.client(), + &config.database.connection_string()?, + &config.migrations.table_name(), + ) + .map_err(From::from) +} + pub fn with_transaction( client: &mut AnyClient, trx_fn: &mut TrxFnMut, diff --git a/migra_cli/src/commands/apply.rs b/migra_cli/src/commands/apply.rs index 6364974..f62d6c8 100644 --- a/migra_cli/src/commands/apply.rs +++ b/migra_cli/src/commands/apply.rs @@ -1,13 +1,13 @@ use crate::app::App; use crate::client::maybe_with_transaction; use crate::opts::ApplyCommandOpt; -use crate::StdResult; -pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> StdResult<()> { +pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResult<()> { let config = app.config()?; let mut client = crate::client::create( &config.database.client(), &config.database.connection_string()?, + &config.migrations.table_name(), )?; let file_contents = cmd_opts diff --git a/migra_cli/src/commands/downgrade.rs b/migra_cli/src/commands/downgrade.rs index 26ce1b4..b85c284 100644 --- a/migra_cli/src/commands/downgrade.rs +++ b/migra_cli/src/commands/downgrade.rs @@ -2,15 +2,14 @@ use crate::app::App; use crate::client; use crate::client::maybe_with_transaction; use crate::opts::DowngradeCommandOpt; -use crate::StdResult; use std::cmp; -pub(crate) fn rollback_applied_migrations(app: &App, opts: &DowngradeCommandOpt) -> StdResult<()> { +pub(crate) fn rollback_applied_migrations( + app: &App, + opts: &DowngradeCommandOpt, +) -> migra::StdResult<()> { let config = app.config()?; - let mut client = client::create( - &config.database.client(), - &config.database.connection_string()?, - )?; + let mut client = client::create_from_config(&config)?; client.create_migrations_table()?; diff --git a/migra_cli/src/commands/init.rs b/migra_cli/src/commands/init.rs index cd2c4da..6735414 100644 --- a/migra_cli/src/commands/init.rs +++ b/migra_cli/src/commands/init.rs @@ -1,9 +1,8 @@ use crate::app::App; use crate::config::{Config, MIGRA_TOML_FILENAME}; -use crate::StdResult; use std::path::PathBuf; -pub(crate) fn initialize_migra_manifest(app: &App) -> StdResult<()> { +pub(crate) fn initialize_migra_manifest(app: &App) -> migra::StdResult<()> { let config_path = app.config_path().cloned().map_or_else( || PathBuf::from(MIGRA_TOML_FILENAME), |mut config_path| { diff --git a/migra_cli/src/commands/list.rs b/migra_cli/src/commands/list.rs index da70a3c..e7af72a 100644 --- a/migra_cli/src/commands/list.rs +++ b/migra_cli/src/commands/list.rs @@ -1,15 +1,19 @@ use crate::app::App; use crate::client; -use crate::error::{Error, StdResult}; +use crate::error::Error; use migra::migration; const EM_DASH: char = '—'; -pub(crate) fn print_migration_lists(app: &App) -> StdResult<()> { +pub(crate) fn print_migration_lists(app: &App) -> migra::StdResult<()> { let config = app.config()?; let applied_migrations = match config.database.connection_string() { Ok(ref database_connection_string) => { - let mut client = client::create(&config.database.client(), database_connection_string)?; + let mut client = client::create( + &config.database.client(), + database_connection_string, + &config.migrations.table_name(), + )?; let applied_migrations = client .get_applied_migrations() .unwrap_or_else(|_| migration::List::new()); diff --git a/migra_cli/src/commands/make.rs b/migra_cli/src/commands/make.rs index ec12675..91d41e4 100644 --- a/migra_cli/src/commands/make.rs +++ b/migra_cli/src/commands/make.rs @@ -1,10 +1,9 @@ use crate::app::App; use crate::opts::MakeCommandOpt; -use crate::StdResult; use chrono::Local; use std::fs; -pub(crate) fn make_migration(app: &App, opts: &MakeCommandOpt) -> StdResult<()> { +pub(crate) fn make_migration(app: &App, opts: &MakeCommandOpt) -> migra::StdResult<()> { let config = app.config()?; let date_format = config.migrations.date_format(); let formatted_current_timestamp = Local::now().format(&date_format); diff --git a/migra_cli/src/commands/upgrade.rs b/migra_cli/src/commands/upgrade.rs index 5298773..ab52a6d 100644 --- a/migra_cli/src/commands/upgrade.rs +++ b/migra_cli/src/commands/upgrade.rs @@ -2,15 +2,14 @@ use crate::app::App; use crate::client; use crate::client::maybe_with_transaction; use crate::opts::UpgradeCommandOpt; -use crate::StdResult; use migra::migration; -pub(crate) fn upgrade_pending_migrations(app: &App, opts: &UpgradeCommandOpt) -> StdResult<()> { +pub(crate) fn upgrade_pending_migrations( + app: &App, + opts: &UpgradeCommandOpt, +) -> migra::StdResult<()> { let config = app.config()?; - let mut client = client::create( - &config.database.client(), - &config.database.connection_string()?, - )?; + let mut client = client::create_from_config(&config)?; client.create_migrations_table()?; diff --git a/migra_cli/src/config.rs b/migra_cli/src/config.rs index feb1e1a..1b73bed 100644 --- a/migra_cli/src/config.rs +++ b/migra_cli/src/config.rs @@ -228,7 +228,7 @@ impl MigrationsConfig { pub(crate) const MIGRA_TOML_FILENAME: &str = "Migra.toml"; #[derive(Debug, Serialize, Deserialize)] -pub(crate) struct Config { +pub struct Config { #[serde(skip)] manifest_root: PathBuf, diff --git a/migra_cli/src/database/migration.rs b/migra_cli/src/database/migration.rs index 048b585..af2ab30 100644 --- a/migra_cli/src/database/migration.rs +++ b/migra_cli/src/database/migration.rs @@ -39,12 +39,12 @@ impl Migration { &self.name } - fn upgrade_sql_content(&self) -> StdResult { + fn upgrade_sql_content(&self) -> migra::StdResult { let content = fs::read_to_string(&self.upgrade_sql_file_path)?; Ok(content) } - fn downgrade_sql_content(&self) -> StdResult { + fn downgrade_sql_content(&self) -> migra::StdResult { let content = fs::read_to_string(&self.downgrade_sql_file_path)?; Ok(content) } @@ -90,17 +90,17 @@ pub fn is_migrations_table_not_found(error: D) -> bool { } pub trait ManageMigration { - fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()>; + fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> migra::StdResult<()>; - fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()>; + fn create_migrations_table(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult; + fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult; - fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult; + fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult; - fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult>; + fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult>; - fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> { + fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> { let content = migration.upgrade_sql_content()?; self.create_migrations_table(conn)?; @@ -110,7 +110,7 @@ pub trait ManageMigration { Ok(()) } - fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> { + fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> { let content = migration.downgrade_sql_content()?; self.apply_sql(conn, &content)?; @@ -121,16 +121,16 @@ pub trait ManageMigration { } impl ManageMigration for MigrationManager { - fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()> { + fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> migra::StdResult<()> { conn.batch_execute(sql_content) } - fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> { + fn create_migrations_table(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { let stmt = conn.create_migration_table_stmt(&self.migrations_table_name); conn.batch_execute(&stmt) } - fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult { + fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult { conn.execute( &format!( "INSERT INTO {} (name) VALUES ($1)", @@ -140,7 +140,7 @@ impl ManageMigration for MigrationManager { ) } - fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult { + fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult { conn.execute( &format!( "DELETE FROM {} WHERE name = $1", @@ -150,7 +150,7 @@ impl ManageMigration for MigrationManager { ) } - fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult> { + fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult> { let res = conn .query( &format!( diff --git a/migra_cli/src/database/transaction.rs b/migra_cli/src/database/transaction.rs index ffc38b0..74f86fd 100644 --- a/migra_cli/src/database/transaction.rs +++ b/migra_cli/src/database/transaction.rs @@ -2,11 +2,11 @@ use super::client_rusqlite::Connection::AnyConnection; use crate::error::StdResult; pub trait ManageTransaction { - fn begin_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>; + fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - fn rollback_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>; + fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - fn commit_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>; + fn commit_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; } #[derive(Debug)] @@ -19,15 +19,15 @@ impl TransactionManager { } impl ManageTransaction for TransactionManager { - fn begin_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> { + fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { conn.batch_execute("BEGIN") } - fn rollback_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> { + fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { conn.batch_execute("ROLLBACK") } - fn commit_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> { + fn commit_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { conn.batch_execute("COMMIT") } } diff --git a/migra_cli/src/error.rs b/migra_cli/src/error.rs index ddcd6ae..ed2cf4f 100644 --- a/migra_cli/src/error.rs +++ b/migra_cli/src/error.rs @@ -4,7 +4,6 @@ use std::io; use std::mem; use std::result; -pub type StdResult = result::Result>; pub type MigraResult = result::Result; #[derive(Debug)] diff --git a/migra_cli/src/main.rs b/migra_cli/src/main.rs index 263a108..dcae36a 100644 --- a/migra_cli/src/main.rs +++ b/migra_cli/src/main.rs @@ -16,12 +16,11 @@ pub use error::Error; mod opts; -use crate::error::StdResult; use app::App; use config::Config; use opts::{AppOpt, StructOpt}; -fn main() -> StdResult<()> { +fn main() -> migra::StdResult<()> { #[cfg(feature = "dotenv")] dotenv::dotenv().ok();