refac(cli): move migration struct to another file
This commit is contained in:
parent
f588912175
commit
db631fddd2
3 changed files with 66 additions and 61 deletions
|
@ -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<Migration> {
|
||||
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<dyn std::error::Error + 'static>> {
|
||||
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<dyn std::error::Error + 'static>> {
|
||||
let content = fs::read_to_string(&self.downgrade_sql)?;
|
||||
|
||||
database::apply_sql(client, &content)?;
|
||||
|
||||
database::delete_migration_info(client, self.name())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
mod config;
|
||||
mod database;
|
||||
mod migration;
|
||||
mod opts;
|
||||
mod path;
|
||||
|
||||
|
|
64
migra-cli/src/migration.rs
Normal file
64
migra-cli/src/migration.rs
Normal file
|
@ -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<Migration> {
|
||||
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<dyn std::error::Error + 'static>> {
|
||||
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<dyn std::error::Error + 'static>> {
|
||||
let content = fs::read_to_string(&self.downgrade_sql)?;
|
||||
|
||||
database::apply_sql(client, &content)?;
|
||||
|
||||
database::delete_migration_info(client, self.name())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
Reference in a new issue