From 481760ee6e960012e683123b9fcba6cc04ab25dd Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 14 Feb 2021 00:03:37 +0300 Subject: [PATCH] refac: add trait for upgrade and downgrade --- migra-cli/src/commands/downgrade.rs | 1 + migra-cli/src/commands/upgrade.rs | 2 +- migra-cli/src/migration.rs | 25 +++++++++++++++++-------- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/migra-cli/src/commands/downgrade.rs b/migra-cli/src/commands/downgrade.rs index 7d51887..c891252 100644 --- a/migra-cli/src/commands/downgrade.rs +++ b/migra-cli/src/commands/downgrade.rs @@ -1,5 +1,6 @@ use crate::config::Config; use crate::database::DatabaseConnection; +use crate::migration::Downgrade; use crate::StdResult; use std::convert::TryFrom; diff --git a/migra-cli/src/commands/upgrade.rs b/migra-cli/src/commands/upgrade.rs index af27881..4b9c49d 100644 --- a/migra-cli/src/commands/upgrade.rs +++ b/migra-cli/src/commands/upgrade.rs @@ -1,5 +1,5 @@ use crate::database::DatabaseConnection; -use crate::migration::Migration; +use crate::migration::{Migration, Upgrade}; use crate::Config; use crate::StdResult; use std::convert::TryFrom; diff --git a/migra-cli/src/migration.rs b/migra-cli/src/migration.rs index 6dba04b..4da540b 100644 --- a/migra-cli/src/migration.rs +++ b/migra-cli/src/migration.rs @@ -1,8 +1,17 @@ use crate::database::DatabaseConnection; use crate::path::PathBuilder; +use crate::StdResult; use std::fs; use std::path::PathBuf; +pub trait Upgrade { + fn upgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()>; +} + +pub trait Downgrade { + fn downgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()>; +} + #[derive(Debug)] pub struct Migration { upgrade_sql: PathBuf, @@ -31,15 +40,16 @@ impl Migration { None } +} +impl Migration { pub fn name(&self) -> &String { &self.name } +} - pub fn upgrade( - &self, - connection: &mut DatabaseConnection, - ) -> Result<(), Box> { +impl Upgrade for Migration { + fn upgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()> { let content = fs::read_to_string(&self.upgrade_sql)?; connection.create_migrations_table()?; @@ -48,11 +58,10 @@ impl Migration { Ok(()) } +} - pub fn downgrade( - &self, - connection: &mut DatabaseConnection, - ) -> Result<(), Box> { +impl Downgrade for Migration { + fn downgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()> { let content = fs::read_to_string(&self.downgrade_sql)?; connection.apply_sql(&content)?;