From d4106c50e66ec5391a5bbaed1558778783674260 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 16 Feb 2021 18:11:39 +0300 Subject: [PATCH] refac: move postgres initialization to separate dir --- migra-cli/src/commands/apply.rs | 2 +- migra-cli/src/commands/downgrade.rs | 2 +- migra-cli/src/commands/list.rs | 3 +- migra-cli/src/commands/upgrade.rs | 2 +- migra-cli/src/database.rs | 74 ---------------------- migra-cli/src/databases/mod.rs | 3 + migra-cli/src/databases/postgres.rs | 95 +++++++++++++++++++++++++++++ migra-cli/src/main.rs | 1 + migra-cli/src/migration.rs | 21 +------ 9 files changed, 106 insertions(+), 97 deletions(-) create mode 100644 migra-cli/src/databases/mod.rs create mode 100644 migra-cli/src/databases/postgres.rs diff --git a/migra-cli/src/commands/apply.rs b/migra-cli/src/commands/apply.rs index f873db3..c024755 100644 --- a/migra-cli/src/commands/apply.rs +++ b/migra-cli/src/commands/apply.rs @@ -1,5 +1,5 @@ use crate::config::Config; -use crate::database::PostgresConnection; +use crate::databases::*; use crate::migration::{DatabaseMigrationManager, MigrationManager}; use crate::opts::ApplyCommandOpt; use crate::path::PathBuilder; diff --git a/migra-cli/src/commands/downgrade.rs b/migra-cli/src/commands/downgrade.rs index fe80de2..b5561f4 100644 --- a/migra-cli/src/commands/downgrade.rs +++ b/migra-cli/src/commands/downgrade.rs @@ -1,6 +1,6 @@ use crate::config::Config; -use crate::database::PostgresConnection; use crate::migration::{DatabaseMigrationManager, MigrationManager, MigrationNames}; +use crate::databases::*; use crate::StdResult; use std::convert::TryFrom; diff --git a/migra-cli/src/commands/list.rs b/migra-cli/src/commands/list.rs index 8965a83..eb89b71 100644 --- a/migra-cli/src/commands/list.rs +++ b/migra-cli/src/commands/list.rs @@ -1,5 +1,6 @@ use crate::config::Config; -use crate::database::{DatabaseConnection, PostgresConnection}; +use crate::database::DatabaseConnection; +use crate::databases::*; use crate::error::{ErrorKind, StdResult}; use crate::migration::{filter_pending_migrations, Migration, MigrationManager, MigrationNames}; diff --git a/migra-cli/src/commands/upgrade.rs b/migra-cli/src/commands/upgrade.rs index a7dd3ed..8681253 100644 --- a/migra-cli/src/commands/upgrade.rs +++ b/migra-cli/src/commands/upgrade.rs @@ -1,4 +1,4 @@ -use crate::database::PostgresConnection; +use crate::databases::*; use crate::migration::{ filter_pending_migrations, DatabaseMigrationManager, Migration, MigrationManager, MigrationNames, diff --git a/migra-cli/src/database.rs b/migra-cli/src/database.rs index 74c9564..4e37bbc 100644 --- a/migra-cli/src/database.rs +++ b/migra-cli/src/database.rs @@ -1,7 +1,4 @@ -use crate::config::Config; use crate::StdResult; -use postgres::{Client, NoTls}; -use std::convert::TryFrom; pub trait ToSql { fn to_sql(&self) -> String; @@ -17,13 +14,6 @@ pub trait TryFromSql: Sized { fn try_from_sql(row: QueryResultRow) -> StdResult; } -impl TryFromSql for String { - fn try_from_sql(row: postgres::Row) -> StdResult { - let res: String = row.get(0); - Ok(res) - } -} - pub trait DatabaseConnection: Sized { type QueryResultRow; type QueryResult; @@ -42,67 +32,3 @@ pub trait DatabaseConnection: Sized { where OutputItem: ?Sized + TryFromSql; } - -pub struct PostgresConnection { - client: Client, -} - -impl TryFrom<&Config> for PostgresConnection { - type Error = Box; - - fn try_from(config: &Config) -> Result { - PostgresConnection::open(&config.database_connection_string()?) - } -} - -impl DatabaseConnection for PostgresConnection { - type QueryResultRow = postgres::Row; - type QueryResult = Vec; - - fn open(connection_string: &str) -> StdResult { - let client = Client::connect(connection_string, NoTls)?; - Ok(PostgresConnection { client }) - } - - fn batch_execute(&mut self, query: &str) -> StdResult<()> { - self.client.batch_execute(query)?; - Ok(()) - } - - fn execute<'b>(&mut self, query: &str, params: &'b [&'b dyn ToSql]) -> StdResult { - let stmt = params - .iter() - .enumerate() - .fold(query.to_string(), |acc, (i, p)| { - str::replace(&acc, &format!("${}", i), &p.to_sql()) - }); - - let res = self.client.execute(stmt.as_str(), &[])?; - Ok(res) - } - - fn query<'b, OutputItem>( - &mut self, - query: &str, - params: &'b [&'b dyn ToSql], - ) -> StdResult> - where - OutputItem: ?Sized + TryFromSql, - { - let stmt = params - .iter() - .enumerate() - .fold(query.to_string(), |acc, (i, p)| { - str::replace(&acc, &format!("${}", i), &p.to_sql()) - }); - - let res: Self::QueryResult = self.client.query(stmt.as_str(), &[])?; - - let res = res - .into_iter() - .map(OutputItem::try_from_sql) - .collect::, _>>()?; - - Ok(res) - } -} diff --git a/migra-cli/src/databases/mod.rs b/migra-cli/src/databases/mod.rs new file mode 100644 index 0000000..0111749 --- /dev/null +++ b/migra-cli/src/databases/mod.rs @@ -0,0 +1,3 @@ +mod postgres; + +pub use self::postgres::*; diff --git a/migra-cli/src/databases/postgres.rs b/migra-cli/src/databases/postgres.rs new file mode 100644 index 0000000..c5297cc --- /dev/null +++ b/migra-cli/src/databases/postgres.rs @@ -0,0 +1,95 @@ + +use crate::migration::{MigrationNames, MigrationManager, is_migrations_table_not_found}; +use postgres::{Client, NoTls}; +use crate::config::Config; +use std::convert::TryFrom; +use crate::error::StdResult; +use crate::database::{TryFromSql, ToSql, DatabaseConnection}; + +pub struct PostgresConnection { + client: Client, +} + +impl TryFrom<&Config> for PostgresConnection { + type Error = Box; + + fn try_from(config: &Config) -> Result { + PostgresConnection::open(&config.database_connection_string()?) + } +} + +impl DatabaseConnection for PostgresConnection { + type QueryResultRow = postgres::Row; + type QueryResult = Vec; + + fn open(connection_string: &str) -> StdResult { + let client = Client::connect(connection_string, NoTls)?; + Ok(PostgresConnection { client }) + } + + fn batch_execute(&mut self, query: &str) -> StdResult<()> { + self.client.batch_execute(query)?; + Ok(()) + } + + fn execute<'b>(&mut self, query: &str, params: &'b [&'b dyn ToSql]) -> StdResult { + let stmt = params + .iter() + .enumerate() + .fold(query.to_string(), |acc, (i, p)| { + str::replace(&acc, &format!("${}", i), &p.to_sql()) + }); + + let res = self.client.execute(stmt.as_str(), &[])?; + Ok(res) + } + + fn query<'b, OutputItem>( + &mut self, + query: &str, + params: &'b [&'b dyn ToSql], + ) -> StdResult> + where + OutputItem: ?Sized + TryFromSql, + { + let stmt = params + .iter() + .enumerate() + .fold(query.to_string(), |acc, (i, p)| { + str::replace(&acc, &format!("${}", i), &p.to_sql()) + }); + + let res: Self::QueryResult = self.client.query(stmt.as_str(), &[])?; + + let res = res + .into_iter() + .map(OutputItem::try_from_sql) + .collect::, _>>()?; + + Ok(res) + } +} + +impl MigrationNames for MigrationManager { + fn applied_migration_names(&mut self) -> StdResult> { + let res = self + .conn + .query(Self::APPLIED_MIGRATIONS_STMT, &[]) + .or_else(|e| { + if is_migrations_table_not_found(&e) { + Ok(Vec::new()) + } else { + Err(e) + } + })?; + + Ok(res.into_iter().collect()) + } +} + +impl TryFromSql for String { + fn try_from_sql(row: postgres::Row) -> StdResult { + let res: String = row.get(0); + Ok(res) + } +} diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index 8a27d86..5d01ea2 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -3,6 +3,7 @@ mod commands; mod config; mod database; +mod databases; mod error; mod migration; mod opts; diff --git a/migra-cli/src/migration.rs b/migra-cli/src/migration.rs index a35d863..a77426a 100644 --- a/migra-cli/src/migration.rs +++ b/migra-cli/src/migration.rs @@ -1,4 +1,4 @@ -use crate::database::{DatabaseConnection, PostgresConnection}; +use crate::database::DatabaseConnection; use crate::path::PathBuilder; use crate::StdResult; use std::fs; @@ -51,7 +51,7 @@ impl Migration { } pub struct MigrationManager { - conn: Conn, + pub(crate) conn: Conn, } impl MigrationManager { @@ -129,23 +129,6 @@ pub trait MigrationNames { fn applied_migration_names(&mut self) -> StdResult>; } -impl MigrationNames for MigrationManager { - fn applied_migration_names(&mut self) -> StdResult> { - let res = self - .conn - .query(Self::APPLIED_MIGRATIONS_STMT, &[]) - .or_else(|e| { - if is_migrations_table_not_found(&e) { - Ok(Vec::new()) - } else { - Err(e) - } - })?; - - Ok(res.into_iter().collect()) - } -} - pub fn filter_pending_migrations( migrations: Vec, applied_migration_names: &[String],