2021-02-22 23:06:08 +03:00
|
|
|
use crate::error::{Error, MigraResult};
|
2021-02-08 23:42:13 +03:00
|
|
|
use crate::migration::Migration;
|
2021-02-05 01:37:25 +03:00
|
|
|
use crate::path::PathBuilder;
|
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-02-13 00:39:39 +03:00
|
|
|
pub(crate) const MIGRA_TOML_FILENAME: &str = "Migra.toml";
|
|
|
|
pub(crate) 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-13 00:39:39 +03:00
|
|
|
manifest_root: PathBuf,
|
2021-02-08 07:01:31 +03:00
|
|
|
|
2021-02-13 00:39:39 +03:00
|
|
|
root: PathBuf,
|
2021-02-08 07:01:31 +03:00
|
|
|
|
|
|
|
#[serde(default)]
|
2021-02-18 12:29:09 +03:00
|
|
|
pub database: DatabaseConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub(crate) enum SupportedDatabaseClient {
|
|
|
|
Postgres,
|
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-18 12:29:09 +03:00
|
|
|
pub client: Option<SupportedDatabaseClient>,
|
2021-02-08 07:01:31 +03:00
|
|
|
pub connection: Option<String>,
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-18 12:29:09 +03:00
|
|
|
impl DatabaseConfig {
|
2021-02-22 23:06:08 +03:00
|
|
|
pub fn client(&self) -> MigraResult<SupportedDatabaseClient> {
|
2021-02-18 12:29:09 +03:00
|
|
|
Ok(SupportedDatabaseClient::Postgres)
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:06:08 +03:00
|
|
|
pub fn connection_string(&self) -> MigraResult<String> {
|
2021-02-18 12:29:09 +03:00
|
|
|
let connection = self
|
|
|
|
.connection
|
|
|
|
.clone()
|
|
|
|
.unwrap_or_else(|| String::from(DEFAULT_DATABASE_CONNECTION_ENV));
|
|
|
|
if let Some(connection_env) = connection.strip_prefix("$") {
|
2021-02-22 23:06:08 +03:00
|
|
|
env::var(connection_env).map_err(|_| Error::MissedEnvVar(connection_env.to_string()))
|
2021-02-18 12:29:09 +03:00
|
|
|
} else {
|
|
|
|
Ok(connection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Config {
|
|
|
|
Config {
|
2021-02-18 12:29:09 +03:00
|
|
|
manifest_root: PathBuf::default(),
|
2021-02-13 00:39:39 +03:00
|
|
|
root: 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-18 12:29:09 +03:00
|
|
|
..Default::default()
|
2021-02-01 23:51:23 +03:00
|
|
|
},
|
2021-01-31 03:23:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:06:08 +03:00
|
|
|
fn search_for_directory_containing_file(path: &Path, file_name: &str) -> MigraResult<PathBuf> {
|
|
|
|
let file_path = path.join(file_name);
|
|
|
|
if file_path.is_file() {
|
|
|
|
Ok(path.to_owned())
|
|
|
|
} else {
|
|
|
|
path.parent()
|
|
|
|
.ok_or(Error::RootNotFound)
|
|
|
|
.and_then(|p| search_for_directory_containing_file(p, file_name))
|
|
|
|
}
|
|
|
|
}
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-22 23:06:08 +03:00
|
|
|
fn recursive_find_project_root() -> MigraResult<PathBuf> {
|
|
|
|
let current_dir = std::env::current_dir()?;
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-22 23:06:08 +03:00
|
|
|
search_for_directory_containing_file(¤t_dir, MIGRA_TOML_FILENAME)
|
2021-02-02 00:53:33 +03:00
|
|
|
}
|
2021-02-01 23:51:23 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
impl Config {
|
2021-02-22 23:06:08 +03:00
|
|
|
pub fn read(config_path: Option<PathBuf>) -> MigraResult<Config> {
|
2021-02-02 00:53:33 +03:00
|
|
|
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),
|
2021-02-22 23:06:08 +03:00
|
|
|
None => recursive_find_project_root().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");
|
2021-02-13 00:39:39 +03:00
|
|
|
config.manifest_root = config_path
|
2021-02-08 07:01:31 +03:00
|
|
|
.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-02 23:50:42 +03:00
|
|
|
|
2021-02-08 07:01:31 +03:00
|
|
|
impl Config {
|
|
|
|
pub fn directory_path(&self) -> PathBuf {
|
2021-02-13 00:39:39 +03:00
|
|
|
PathBuilder::from(&self.manifest_root)
|
|
|
|
.append(&self.root)
|
2021-02-08 07:01:31 +03:00
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn migration_dir_path(&self) -> PathBuf {
|
|
|
|
PathBuilder::from(&self.directory_path())
|
|
|
|
.append("migrations")
|
|
|
|
.build()
|
|
|
|
}
|
|
|
|
|
2021-02-22 23:06:08 +03:00
|
|
|
pub fn migrations(&self) -> MigraResult<Vec<Migration>> {
|
2021-02-10 01:13:27 +03:00
|
|
|
let mut entries = match self.migration_dir_path().read_dir() {
|
|
|
|
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(Vec::new()),
|
|
|
|
entries => entries?
|
|
|
|
.map(|res| res.map(|e| e.path()))
|
|
|
|
.collect::<Result<Vec<_>, io::Error>>()?,
|
|
|
|
};
|
|
|
|
|
|
|
|
if entries.is_empty() {
|
|
|
|
return Ok(vec![]);
|
|
|
|
}
|
2021-02-08 07:01:31 +03:00
|
|
|
|
|
|
|
entries.sort();
|
|
|
|
|
|
|
|
let migrations = entries
|
|
|
|
.iter()
|
2021-02-21 17:53:43 +03:00
|
|
|
.filter_map(|path| Migration::new(&path))
|
2021-02-08 07:01:31 +03:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
Ok(migrations)
|
|
|
|
}
|
|
|
|
}
|