From db631fddd2ce244c2f7100c3f0feda014689f10d Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 8 Feb 2021 23:42:13 +0300 Subject: [PATCH] refac(cli): move migration struct to another file --- migra-cli/src/config.rs | 62 +----------------------------------- migra-cli/src/main.rs | 1 + migra-cli/src/migration.rs | 64 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 migra-cli/src/migration.rs diff --git a/migra-cli/src/config.rs b/migra-cli/src/config.rs index a0e09fd..eb3ca16 100644 --- a/migra-cli/src/config.rs +++ b/migra-cli/src/config.rs @@ -1,6 +1,5 @@ -use crate::database; +use crate::migration::Migration; use crate::path::PathBuilder; -use postgres::Client; use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; use std::{env, fs, io}; @@ -160,62 +159,3 @@ impl Config { Ok(migrations) } } - -#[derive(Debug)] -pub struct Migration { - upgrade_sql: PathBuf, - downgrade_sql: PathBuf, - name: String, -} - -impl Migration { - fn new(directory: &PathBuf) -> Option { - if directory.is_dir() { - let name = directory - .file_name() - .and_then(|name| name.to_str()) - .unwrap_or_default(); - let upgrade_sql = PathBuilder::from(directory).append("up.sql").build(); - let downgrade_sql = PathBuilder::from(directory).append("down.sql").build(); - - if upgrade_sql.exists() && downgrade_sql.exists() { - return Some(Migration { - upgrade_sql, - downgrade_sql, - name: String::from(name), - }); - } - } - - None - } - - pub fn name(&self) -> &String { - &self.name - } - - pub fn upgrade(&self, client: &mut Client) -> Result<(), Box> { - let content = fs::read_to_string(&self.upgrade_sql)?; - - database::create_migration_table(client)?; - - database::apply_sql(client, &content)?; - - database::insert_migration_info(client, self.name())?; - - Ok(()) - } - - pub fn downgrade( - &self, - client: &mut Client, - ) -> Result<(), Box> { - let content = fs::read_to_string(&self.downgrade_sql)?; - - database::apply_sql(client, &content)?; - - database::delete_migration_info(client, self.name())?; - - Ok(()) - } -} diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index fbb4ab2..5fe5237 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -2,6 +2,7 @@ mod config; mod database; +mod migration; mod opts; mod path; diff --git a/migra-cli/src/migration.rs b/migra-cli/src/migration.rs new file mode 100644 index 0000000..6a5770d --- /dev/null +++ b/migra-cli/src/migration.rs @@ -0,0 +1,64 @@ +use crate::database; +use crate::path::PathBuilder; +use postgres::Client; +use std::fs; +use std::path::PathBuf; + +#[derive(Debug)] +pub struct Migration { + upgrade_sql: PathBuf, + downgrade_sql: PathBuf, + name: String, +} + +impl Migration { + pub(crate) fn new(directory: &PathBuf) -> Option { + if directory.is_dir() { + let name = directory + .file_name() + .and_then(|name| name.to_str()) + .unwrap_or_default(); + let upgrade_sql = PathBuilder::from(directory).append("up.sql").build(); + let downgrade_sql = PathBuilder::from(directory).append("down.sql").build(); + + if upgrade_sql.exists() && downgrade_sql.exists() { + return Some(Migration { + upgrade_sql, + downgrade_sql, + name: String::from(name), + }); + } + } + + None + } + + pub fn name(&self) -> &String { + &self.name + } + + pub fn upgrade(&self, client: &mut Client) -> Result<(), Box> { + let content = fs::read_to_string(&self.upgrade_sql)?; + + database::create_migrations_table(client)?; + + database::apply_sql(client, &content)?; + + database::insert_migration_info(client, self.name())?; + + Ok(()) + } + + pub fn downgrade( + &self, + client: &mut Client, + ) -> Result<(), Box> { + let content = fs::read_to_string(&self.downgrade_sql)?; + + database::apply_sql(client, &content)?; + + database::delete_migration_info(client, self.name())?; + + Ok(()) + } +}