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-08 07:01:31 +03:00
|
|
|
use std::{env, 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-02-08 07:01:31 +03:00
|
|
|
const DEFAULT_DATABASE_CONNECTION_ENV: &str = "$DATABASE_URL";
|
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)]
|
2021-02-08 07:01:31 +03:00
|
|
|
root: PathBuf,
|
|
|
|
|
|
|
|
directory: PathBuf,
|
|
|
|
|
|
|
|
#[serde(default)]
|
|
|
|
database: DatabaseConfig,
|
2021-01-31 13:40:02 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
2021-01-31 13:40:02 +03:00
|
|
|
pub(crate) struct DatabaseConfig {
|
2021-02-08 07:01:31 +03:00
|
|
|
pub connection: Option<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 {
|
2021-02-08 07:01:31 +03:00
|
|
|
connection: Some(String::from(DEFAULT_DATABASE_CONNECTION_ENV)),
|
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);
|
2021-02-08 07:01:31 +03:00
|
|
|
Some(config_path)
|
2021-02-02 23:50:42 +03:00
|
|
|
}
|
2021-02-08 07:01:31 +03:00
|
|
|
Some(config_path) => Some(config_path),
|
|
|
|
None => recursive_find_config_file().ok(),
|
2021-02-02 00:53:33 +03:00
|
|
|
};
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
match config_path {
|
|
|
|
None => Ok(Config::default()),
|
|
|
|
Some(config_path) => {
|
|
|
|
let content = fs::read_to_string(&config_path)?;
|
2021-02-02 00:53:33 +03:00
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
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();
|
2021-02-02 00:53:33 +03:00
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-08 07:28:35 +03:00
|
|
|
pub fn initialize(config_path: Option<PathBuf>) -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
let config_path = config_path
|
|
|
|
.map(|mut config_path| {
|
|
|
|
let ext = config_path.extension();
|
|
|
|
if config_path.is_dir() || ext.is_none() {
|
|
|
|
config_path.push(MIGRA_TOML_FILENAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
config_path
|
|
|
|
})
|
|
|
|
.unwrap_or_else(|| PathBuf::from(MIGRA_TOML_FILENAME));
|
|
|
|
|
|
|
|
if config_path.exists() {
|
|
|
|
println!("{} already exists", config_path.to_str().unwrap());
|
2021-01-31 03:23:43 +03:00
|
|
|
return Ok(());
|
2021-02-08 07:28:35 +03:00
|
|
|
} else if let Some(dirs) = config_path.parent() {
|
|
|
|
fs::create_dir_all(dirs)?;
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let config = Config::default();
|
|
|
|
let content = toml::to_string(&config)?;
|
2021-02-08 07:28:35 +03:00
|
|
|
fs::write(&config_path, content)?;
|
2021-01-31 03:23:43 +03:00
|
|
|
|
2021-02-08 07:28:35 +03:00
|
|
|
println!("Created {}", config_path.to_str().unwrap());
|
2021-01-31 03:34:38 +03:00
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 23:50:42 +03:00
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
impl Config {
|
|
|
|
pub fn directory_path(&self) -> PathBuf {
|
|
|
|
PathBuilder::from(&self.root)
|
|
|
|
.append(&self.directory)
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn database_connection(&self) -> String {
|
|
|
|
let connection = self
|
|
|
|
.database
|
|
|
|
.connection
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| String::from(DEFAULT_DATABASE_CONNECTION_ENV));
|
|
|
|
if let Some(connection_env) = connection.strip_prefix("$") {
|
|
|
|
env::var(connection_env).unwrap_or_else(|_| {
|
|
|
|
panic!(
|
|
|
|
r#"You need to provide "{}" environment variable"#,
|
|
|
|
connection_env
|
|
|
|
)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
connection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn migration_dir_path(&self) -> PathBuf {
|
|
|
|
PathBuilder::from(&self.directory_path())
|
|
|
|
.append("migrations")
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn migrations(&self) -> io::Result<Vec<Migration>> {
|
|
|
|
let mut entries = self
|
|
|
|
.migration_dir_path()
|
|
|
|
.read_dir()?
|
|
|
|
.map(|res| res.map(|e| e.path()))
|
|
|
|
.collect::<Result<Vec<_>, io::Error>>()?;
|
|
|
|
|
|
|
|
entries.sort();
|
|
|
|
|
|
|
|
let migrations = entries
|
|
|
|
.iter()
|
|
|
|
.filter_map(Migration::new)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(migrations)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
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
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
pub fn downgrade(
|
|
|
|
&self,
|
|
|
|
client: &mut Client,
|
|
|
|
) -> Result<(), Box<dyn std::error::Error + 'static>> {
|
2021-02-06 01:37:30 +03:00
|
|
|
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
|
|
|
}
|