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(()) + } +}