From 33f392e18ec79a8fe0e3958b5abcb1db8c90e667 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 15 Feb 2021 13:47:45 +0300 Subject: [PATCH] refac: add additional trait for migration names --- migra-cli/src/commands/downgrade.rs | 2 +- migra-cli/src/commands/list.rs | 1 + migra-cli/src/commands/upgrade.rs | 13 +++---- migra-cli/src/database.rs | 7 ++++ migra-cli/src/migration.rs | 55 +++++++++++++++-------------- 5 files changed, 44 insertions(+), 34 deletions(-) diff --git a/migra-cli/src/commands/downgrade.rs b/migra-cli/src/commands/downgrade.rs index a8267ad..fe80de2 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}; +use crate::migration::{DatabaseMigrationManager, MigrationManager, MigrationNames}; use crate::StdResult; use std::convert::TryFrom; diff --git a/migra-cli/src/commands/list.rs b/migra-cli/src/commands/list.rs index d0fa8ec..0952573 100644 --- a/migra-cli/src/commands/list.rs +++ b/migra-cli/src/commands/list.rs @@ -3,6 +3,7 @@ use crate::database::{DatabaseConnection, PostgresConnection}; use crate::error::{ErrorKind, StdResult}; use crate::migration::{ filter_pending_migrations, DatabaseMigrationManager, Migration, MigrationManager, + MigrationNames, }; const EM_DASH: char = '—'; diff --git a/migra-cli/src/commands/upgrade.rs b/migra-cli/src/commands/upgrade.rs index 16c6ff5..a7dd3ed 100644 --- a/migra-cli/src/commands/upgrade.rs +++ b/migra-cli/src/commands/upgrade.rs @@ -1,6 +1,8 @@ -use crate::database::{DatabaseConnection, PostgresConnection}; -use crate::migration::Migration; -use crate::migration::{filter_pending_migrations, DatabaseMigrationManager, MigrationManager}; +use crate::database::PostgresConnection; +use crate::migration::{ + filter_pending_migrations, DatabaseMigrationManager, Migration, MigrationManager, + MigrationNames, +}; use crate::Config; use crate::StdResult; use std::convert::TryFrom; @@ -25,13 +27,12 @@ fn is_up_to_date_migrations(migrations: &[Migration], applied_migration_names: & migrations.is_empty() || migrations.last().map(|m| m.name()) == applied_migration_names.first() } -fn upgrade_all_pending_migrations( +fn upgrade_all_pending_migrations( mut manager: ManagerT, pending_migrations: &[Migration], ) -> StdResult<()> where - Conn: DatabaseConnection, - ManagerT: Sized + DatabaseMigrationManager, + ManagerT: Sized + DatabaseMigrationManager, { for migration in pending_migrations.iter() { println!("upgrade {}...", migration.name()); diff --git a/migra-cli/src/database.rs b/migra-cli/src/database.rs index e508aeb..e6ccf1e 100644 --- a/migra-cli/src/database.rs +++ b/migra-cli/src/database.rs @@ -17,6 +17,13 @@ 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; diff --git a/migra-cli/src/migration.rs b/migra-cli/src/migration.rs index a509826..dd3410e 100644 --- a/migra-cli/src/migration.rs +++ b/migra-cli/src/migration.rs @@ -1,4 +1,3 @@ -use crate::database::TryFromSql; use crate::database::{DatabaseConnection, PostgresConnection}; use crate::path::PathBuilder; use crate::StdResult; @@ -67,14 +66,7 @@ pub fn is_migrations_table_not_found(error: D) -> bool { .contains(r#"relation "migrations" does not exist"#) } -impl TryFromSql for String { - fn try_from_sql(row: postgres::Row) -> StdResult { - let res: String = row.get(0); - Ok(res) - } -} - -pub trait DatabaseMigrationManager { +pub trait DatabaseMigrationManager { const CREATE_MIGRATIONS_STMT: &'static str = r#" CREATE TABLE IF NOT EXISTS migrations ( id serial PRIMARY KEY, @@ -88,8 +80,6 @@ pub trait DatabaseMigrationManager { fn apply_sql(&mut self, sql_content: &str) -> StdResult<()>; - fn applied_migration_names(&mut self) -> StdResult>; - fn create_migrations_table(&mut self) -> StdResult<()>; fn insert_migration_info(&mut self, name: &str) -> StdResult; @@ -116,26 +106,14 @@ pub trait DatabaseMigrationManager { } } -impl DatabaseMigrationManager for MigrationManager { +impl DatabaseMigrationManager for MigrationManager +where + Conn: DatabaseConnection, +{ fn apply_sql(&mut self, sql_content: &str) -> StdResult<()> { self.conn.batch_execute(sql_content) } - fn applied_migration_names(&mut self) -> StdResult> { - let res = self - .conn - .query("SELECT name FROM migrations ORDER BY id DESC", &[]) - .or_else(|e| { - if is_migrations_table_not_found(&e) { - Ok(Vec::new()) - } else { - Err(e) - } - })?; - - Ok(res.into_iter().collect()) - } - fn create_migrations_table(&mut self) -> StdResult<()> { self.conn.batch_execute(Self::CREATE_MIGRATIONS_STMT) } @@ -149,6 +127,29 @@ impl DatabaseMigrationManager for MigrationManager 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],