From 261e6fdbc6ccd1792aa57559e196128f3af9be68 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 7 Jun 2021 00:37:05 +0300 Subject: [PATCH] chore: remove unused code --- migra_cli/src/database/connection.rs | 1 - migra_cli/src/database/migration.rs | 177 -------------------------- migra_cli/src/database/mod.rs | 3 - migra_cli/src/database/transaction.rs | 33 ----- migra_cli/src/error.rs | 3 - migra_cli/src/main.rs | 6 +- 6 files changed, 4 insertions(+), 219 deletions(-) delete mode 100644 migra_cli/src/database/connection.rs delete mode 100644 migra_cli/src/database/migration.rs delete mode 100644 migra_cli/src/database/mod.rs delete mode 100644 migra_cli/src/database/transaction.rs diff --git a/migra_cli/src/database/connection.rs b/migra_cli/src/database/connection.rs deleted file mode 100644 index 8b13789..0000000 --- a/migra_cli/src/database/connection.rs +++ /dev/null @@ -1 +0,0 @@ - diff --git a/migra_cli/src/database/migration.rs b/migra_cli/src/database/migration.rs deleted file mode 100644 index af2ab30..0000000 --- a/migra_cli/src/database/migration.rs +++ /dev/null @@ -1,177 +0,0 @@ -use super::client_rusqlite::Connection::AnyConnection; -use crate::Config; -use crate::StdResult; -use std::fs; -use std::path::{Path, PathBuf}; - -#[derive(Debug, Clone)] -pub struct Migration { - upgrade_sql_file_path: PathBuf, - downgrade_sql_file_path: PathBuf, - name: String, -} - -impl Migration { - pub(crate) fn new(directory: &Path) -> Option { - if directory.is_dir() { - let name = directory - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or_default(); - let upgrade_sql_file_path = directory.join("up.sql"); - let downgrade_sql_file_path = directory.join("down.sql"); - - if upgrade_sql_file_path.exists() && downgrade_sql_file_path.exists() { - return Some(Migration { - upgrade_sql_file_path, - downgrade_sql_file_path, - name: String::from(name), - }); - } - } - - None - } -} - -impl Migration { - pub fn name(&self) -> &String { - &self.name - } - - 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) -> migra::StdResult { - let content = fs::read_to_string(&self.downgrade_sql_file_path)?; - Ok(content) - } -} - -#[derive(Debug)] -pub struct MigrationManager { - migrations_table_name: String, -} - -impl MigrationManager { - fn new(migrations_table_name: &str) -> Self { - MigrationManager { - migrations_table_name: migrations_table_name.to_owned(), - } - } -} - -impl From<&Config> for MigrationManager { - fn from(config: &Config) -> Self { - MigrationManager::new(&config.migrations.table_name()) - } -} - -pub fn is_migrations_table_not_found(error: D) -> bool { - let error_message = error.to_string(); - - fn is_postgres_error(error_message: &str) -> bool { - error_message.contains("relation") && error_message.ends_with("does not exist") - } - - fn is_mysql_error(error_message: &str) -> bool { - error_message.contains("ERROR 1146 (42S02)") - } - - fn is_sqlite_error(error_message: &str) -> bool { - error_message.starts_with("no such table:") - } - - is_postgres_error(&error_message) - || is_mysql_error(&error_message) - || is_sqlite_error(&error_message) -} - -pub trait ManageMigration { - fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> migra::StdResult<()>; - - fn create_migrations_table(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - - fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult; - - fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult; - - fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult>; - - fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> { - let content = migration.upgrade_sql_content()?; - - self.create_migrations_table(conn)?; - self.apply_sql(conn, &content)?; - self.insert_migration_info(conn, migration.name())?; - - Ok(()) - } - - fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> { - let content = migration.downgrade_sql_content()?; - - self.apply_sql(conn, &content)?; - self.delete_migration_info(conn, migration.name())?; - - Ok(()) - } -} - -impl ManageMigration for MigrationManager { - 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) -> 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) -> migra::StdResult { - conn.execute( - &format!( - "INSERT INTO {} (name) VALUES ($1)", - &self.migrations_table_name - ), - &[&name], - ) - } - - fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult { - conn.execute( - &format!( - "DELETE FROM {} WHERE name = $1", - &self.migrations_table_name - ), - &[&name], - ) - } - - fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult> { - let res = conn - .query( - &format!( - "SELECT name FROM {} ORDER BY id DESC", - &self.migrations_table_name - ), - &[], - ) - .or_else(|e| { - if is_migrations_table_not_found(&e) { - Ok(Vec::new()) - } else { - Err(e) - } - })?; - - let applied_migration_names: Vec = res - .into_iter() - .filter_map(|row| row.first().cloned()) - .collect(); - - Ok(applied_migration_names) - } -} diff --git a/migra_cli/src/database/mod.rs b/migra_cli/src/database/mod.rs deleted file mode 100644 index 4f719c4..0000000 --- a/migra_cli/src/database/mod.rs +++ /dev/null @@ -1,3 +0,0 @@ -pub(crate) mod connection; -// pub(crate) mod migration; -// pub(crate) mod transaction; diff --git a/migra_cli/src/database/transaction.rs b/migra_cli/src/database/transaction.rs deleted file mode 100644 index ca8abca..0000000 --- a/migra_cli/src/database/transaction.rs +++ /dev/null @@ -1,33 +0,0 @@ -use super::client_rusqlite::Connection::AnyConnection; -use crate::errors::StdResult; - -pub trait ManageTransaction { - fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - - fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; - - fn commit_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>; -} - -#[derive(Debug)] -pub struct TransactionManager; - -impl TransactionManager { - pub fn new() -> Self { - TransactionManager - } -} - -impl ManageTransaction for TransactionManager { - fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { - conn.batch_execute("BEGIN") - } - - fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> { - conn.batch_execute("ROLLBACK") - } - - 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 ed2cf4f..d266314 100644 --- a/migra_cli/src/error.rs +++ b/migra_cli/src/error.rs @@ -8,8 +8,6 @@ pub type MigraResult = result::Result; #[derive(Debug)] pub enum Error { - FailedDatabaseConnection, - RootNotFound, MissedEnvVar(String), @@ -23,7 +21,6 @@ impl fmt::Display for Error { Error::MissedEnvVar(ref name) => { write!(fmt, r#"Missed "{}" environment variable"#, name) } - Error::FailedDatabaseConnection => fmt.write_str("Failed database connection"), Error::Io(ref error) => write!(fmt, "{}", error), } } diff --git a/migra_cli/src/main.rs b/migra_cli/src/main.rs index 85148f6..78dbad5 100644 --- a/migra_cli/src/main.rs +++ b/migra_cli/src/main.rs @@ -4,8 +4,10 @@ #[macro_use] extern crate cfg_if; -#[cfg(not(any(feature = "postgres", feature = "mysql")))] -compile_error!(r#"Either features "postgres" or "mysql" must be enabled for "migra" crate"#); +#[cfg(not(any(feature = "postgres", feature = "mysql", feature = "sqlite")))] +compile_error!( + r#"Either features "postgres", "mysql" or "sqlite" must be enabled for "migra-cli" crate"# +); mod app; mod client;