From 1d8993d1f007ce219882be60ea9c488f7ef66057 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 13 Feb 2021 23:46:19 +0300 Subject: [PATCH] refac: rename method ... to open database connection --- migra-cli/src/commands/list.rs | 2 +- migra-cli/src/commands/upgrade.rs | 11 +++++------ migra-cli/src/database.rs | 4 ++-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/migra-cli/src/commands/list.rs b/migra-cli/src/commands/list.rs index 3118e2d..a5f6473 100644 --- a/migra-cli/src/commands/list.rs +++ b/migra-cli/src/commands/list.rs @@ -7,7 +7,7 @@ const EM_DASH: char = '—'; pub(crate) fn print_migration_lists(config: Config) -> StdResult<()> { let applied_migrations = match config.database_connection_string() { Ok(ref database_connection_string) => { - let mut connection = DatabaseConnection::connect(database_connection_string)?; + let mut connection = DatabaseConnection::open(database_connection_string)?; let applied_migrations = connection.applied_migration_names()?; println!("Applied migrations:"); diff --git a/migra-cli/src/commands/upgrade.rs b/migra-cli/src/commands/upgrade.rs index 17d69f3..af27881 100644 --- a/migra-cli/src/commands/upgrade.rs +++ b/migra-cli/src/commands/upgrade.rs @@ -1,14 +1,13 @@ -use crate::database; +use crate::database::DatabaseConnection; use crate::migration::Migration; use crate::Config; use crate::StdResult; +use std::convert::TryFrom; pub(crate) fn upgrade_pending_migrations(config: Config) -> StdResult<()> { - let database_connection_string = &config.database_connection_string()?; - let mut client = database::connect(database_connection_string)?; - - let applied_migration_names = database::applied_migration_names(&mut client)?; + let mut connection = DatabaseConnection::try_from(&config)?; + let applied_migration_names = connection.applied_migration_names()?; let migrations = config.migrations()?; if is_up_to_date_migrations(&migrations, &applied_migration_names) { @@ -18,7 +17,7 @@ pub(crate) fn upgrade_pending_migrations(config: Config) -> StdResult<()> { for migration in pending_migrations.iter() { println!("upgrade {}...", migration.name()); - migration.upgrade(&mut client)?; + migration.upgrade(&mut connection)?; } } diff --git a/migra-cli/src/database.rs b/migra-cli/src/database.rs index 69feaa6..7c98e16 100644 --- a/migra-cli/src/database.rs +++ b/migra-cli/src/database.rs @@ -11,12 +11,12 @@ impl TryFrom<&Config> for DatabaseConnection { type Error = Box; fn try_from(config: &Config) -> Result { - DatabaseConnection::connect(&config.database_connection_string()?) + DatabaseConnection::open(&config.database_connection_string()?) } } impl DatabaseConnection { - pub fn connect(connection_string: &str) -> StdResult { + pub fn open(connection_string: &str) -> StdResult { let client = Client::connect(connection_string, NoTls)?; Ok(DatabaseConnection { client }) }