2021-02-06 01:22:00 +03:00
|
|
|
use crate::database;
|
2021-02-05 01:37:25 +03:00
|
|
|
use crate::path::PathBuilder;
|
2021-02-06 01:22:00 +03:00
|
|
|
use postgres::Client;
|
2021-01-31 03:23:43 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-02-01 23:51:23 +03:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-02-02 23:50:42 +03:00
|
|
|
use std::{fs, io};
|
2021-01-31 03:23:43 +03:00
|
|
|
|
2021-01-31 03:34:38 +03:00
|
|
|
const MIGRA_TOML_FILENAME: &str = "Migra.toml";
|
2021-01-31 03:23:43 +03:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub(crate) struct Config {
|
2021-02-01 23:51:23 +03:00
|
|
|
#[serde(skip)]
|
|
|
|
pub root: PathBuf,
|
|
|
|
pub directory: PathBuf,
|
2021-01-31 13:40:02 +03:00
|
|
|
pub database: DatabaseConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
pub(crate) struct DatabaseConfig {
|
2021-02-01 23:51:23 +03:00
|
|
|
pub connection: String,
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2021-02-01 23:51:23 +03:00
|
|
|
root: PathBuf::new(),
|
|
|
|
directory: PathBuf::from("database"),
|
2021-01-31 13:40:02 +03:00
|
|
|
database: DatabaseConfig {
|
|
|
|
connection: String::new(),
|
2021-02-01 23:51:23 +03:00
|
|
|
},
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
fn recursive_find_config_file() -> io::Result<PathBuf> {
|
|
|
|
let current_dir = std::env::current_dir()?;
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
let mut read_dir = Some(current_dir.as_path());
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
loop {
|
|
|
|
if let Some(dir) = read_dir {
|
|
|
|
let migra_file_path = PathBuilder::from(dir).append(MIGRA_TOML_FILENAME).build();
|
|
|
|
if !migra_file_path.exists() {
|
|
|
|
read_dir = dir.parent();
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
return Ok(migra_file_path);
|
|
|
|
} else {
|
|
|
|
return Err(io::Error::from(io::ErrorKind::NotFound));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
impl Config {
|
|
|
|
pub fn read(config_path: Option<PathBuf>) -> io::Result<Config> {
|
|
|
|
let config_path = match config_path {
|
|
|
|
Some(mut config_path) if config_path.is_dir() => {
|
|
|
|
config_path.push(MIGRA_TOML_FILENAME);
|
|
|
|
config_path
|
2021-02-02 23:50:42 +03:00
|
|
|
}
|
2021-02-02 00:53:33 +03:00
|
|
|
Some(config_path) => config_path,
|
|
|
|
None => recursive_find_config_file()?,
|
|
|
|
};
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
let content = fs::read_to_string(&config_path)?;
|
|
|
|
|
|
|
|
let mut config: Config = toml::from_str(&content).expect("Cannot parse Migra.toml");
|
|
|
|
config.root = config_path
|
|
|
|
.parent()
|
|
|
|
.unwrap_or_else(|| Path::new(""))
|
|
|
|
.to_path_buf();
|
|
|
|
|
|
|
|
Ok(config)
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn initialize() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
if Path::new(MIGRA_TOML_FILENAME).exists() {
|
2021-01-31 03:34:38 +03:00
|
|
|
println!("{} already exists", MIGRA_TOML_FILENAME);
|
2021-01-31 03:23:43 +03:00
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
let config = Config::default();
|
|
|
|
let content = toml::to_string(&config)?;
|
|
|
|
fs::write(MIGRA_TOML_FILENAME, content)?;
|
|
|
|
|
2021-01-31 03:34:38 +03:00
|
|
|
println!("Created {}", MIGRA_TOML_FILENAME);
|
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 23:50:42 +03:00
|
|
|
|
2021-02-06 01:22:00 +03:00
|
|
|
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(())
|
|
|
|
}
|
2021-02-06 01:37:30 +03:00
|
|
|
|
|
|
|
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(())
|
|
|
|
}
|
2021-02-06 01:22:00 +03:00
|
|
|
}
|
|
|
|
|
2021-02-02 23:50:42 +03:00
|
|
|
impl Config {
|
|
|
|
pub fn directory_path(&self) -> PathBuf {
|
|
|
|
PathBuilder::from(&self.root)
|
|
|
|
.append(&self.directory)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:46:18 +03:00
|
|
|
pub fn migration_dir_path(&self) -> PathBuf {
|
|
|
|
PathBuilder::from(&self.directory_path())
|
|
|
|
.append("migrations")
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2021-02-06 01:22:00 +03:00
|
|
|
pub fn migrations(&self) -> io::Result<Vec<Migration>> {
|
2021-02-02 23:50:42 +03:00
|
|
|
let mut entries = self
|
2021-02-05 01:46:18 +03:00
|
|
|
.migration_dir_path()
|
2021-02-02 23:50:42 +03:00
|
|
|
.read_dir()?
|
|
|
|
.map(|res| res.map(|e| e.path()))
|
|
|
|
.collect::<Result<Vec<_>, io::Error>>()?;
|
|
|
|
|
|
|
|
entries.sort();
|
|
|
|
|
2021-02-06 01:22:00 +03:00
|
|
|
let migrations = entries
|
|
|
|
.iter()
|
|
|
|
.filter_map(Migration::new)
|
2021-02-02 23:50:42 +03:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2021-02-06 01:22:00 +03:00
|
|
|
Ok(migrations)
|
2021-02-02 23:50:42 +03:00
|
|
|
}
|
|
|
|
}
|