From e81298d1ba6605cdc4b2640ec9c53d6a2bfd8a8a Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 8 Apr 2021 00:49:38 +0300 Subject: [PATCH] chore: move db statements to another trait --- migra-cli/src/database/clients/mysql.rs | 7 ++++--- migra-cli/src/database/clients/postgres.rs | 7 ++++--- migra-cli/src/database/connection.rs | 6 ++++-- migra-cli/src/database/migration.rs | 2 +- migra-cli/src/database/mod.rs | 4 +++- 5 files changed, 16 insertions(+), 10 deletions(-) diff --git a/migra-cli/src/database/clients/mysql.rs b/migra-cli/src/database/clients/mysql.rs index 5baca6a..fed21b4 100644 --- a/migra-cli/src/database/clients/mysql.rs +++ b/migra-cli/src/database/clients/mysql.rs @@ -22,15 +22,16 @@ impl OpenDatabaseConnection for MySqlConnection { } } -impl DatabaseConnection for MySqlConnection { - fn migration_table_stmt(&self) -> String { +impl DatabaseStatements for MySqlConnection { + fn create_migration_table_stmt(&self) -> &'static str { r#"CREATE TABLE IF NOT EXISTS migrations ( id int AUTO_INCREMENT PRIMARY KEY, name varchar(256) NOT NULL UNIQUE )"# - .to_string() } +} +impl DatabaseConnection for MySqlConnection { fn batch_execute(&mut self, query: &str) -> StdResult<()> { self.client()?.query_drop(query)?; Ok(()) diff --git a/migra-cli/src/database/clients/postgres.rs b/migra-cli/src/database/clients/postgres.rs index e39f35c..cbba7a8 100644 --- a/migra-cli/src/database/clients/postgres.rs +++ b/migra-cli/src/database/clients/postgres.rs @@ -14,15 +14,16 @@ impl OpenDatabaseConnection for PostgresConnection { } } -impl DatabaseConnection for PostgresConnection { - fn migration_table_stmt(&self) -> String { +impl DatabaseStatements for PostgresConnection { + fn create_migration_table_stmt(&self) -> &'static str { r#"CREATE TABLE IF NOT EXISTS migrations ( id serial PRIMARY KEY, name text NOT NULL UNIQUE )"# - .to_string() } +} +impl DatabaseConnection for PostgresConnection { fn batch_execute(&mut self, query: &str) -> StdResult<()> { self.client.batch_execute(query)?; Ok(()) diff --git a/migra-cli/src/database/connection.rs b/migra-cli/src/database/connection.rs index 53c07c2..0fc5f3b 100644 --- a/migra-cli/src/database/connection.rs +++ b/migra-cli/src/database/connection.rs @@ -9,9 +9,11 @@ pub trait OpenDatabaseConnection: Sized { fn open(connection_string: &str) -> StdResult; } -pub trait DatabaseConnection { - fn migration_table_stmt(&self) -> String; +pub trait DatabaseStatements { + fn create_migration_table_stmt(&self) -> &'static str; +} +pub trait DatabaseConnection: DatabaseStatements { fn batch_execute(&mut self, query: &str) -> StdResult<()>; fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult; diff --git a/migra-cli/src/database/migration.rs b/migra-cli/src/database/migration.rs index 8647310..9855770 100644 --- a/migra-cli/src/database/migration.rs +++ b/migra-cli/src/database/migration.rs @@ -104,7 +104,7 @@ impl ManageMigration for MigrationManager { } fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> { - let stmt = conn.migration_table_stmt(); + let stmt = conn.create_migration_table_stmt(); conn.batch_execute(&stmt) } diff --git a/migra-cli/src/database/mod.rs b/migra-cli/src/database/mod.rs index b4d0edb..7f176f3 100644 --- a/migra-cli/src/database/mod.rs +++ b/migra-cli/src/database/mod.rs @@ -7,7 +7,9 @@ pub(crate) mod transaction; pub mod prelude { pub use super::adapter::{ToSql, ToSqlParams, TryFromSql}; - pub use super::connection::{AnyConnection, DatabaseConnection, OpenDatabaseConnection}; + pub use super::connection::{ + AnyConnection, DatabaseConnection, DatabaseStatements, OpenDatabaseConnection, + }; pub use super::migration::ManageMigration; pub use super::transaction::ManageTransaction; }