Archived
1
0
Fork 0

refac: use join instead path builder

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-22 23:13:50 +03:00
parent dbc7ddadb7
commit f4ad03cee0
6 changed files with 20 additions and 99 deletions

View file

@ -1,17 +1,19 @@
use crate::config::Config; use crate::config::Config;
use crate::migration::{DatabaseMigrationManager, MigrationManager}; use crate::migration::{DatabaseMigrationManager, MigrationManager};
use crate::opts::ApplyCommandOpt; use crate::opts::ApplyCommandOpt;
use crate::path::PathBuilder;
use crate::StdResult; use crate::StdResult;
use std::convert::TryFrom; use std::convert::TryFrom;
pub(crate) fn apply_sql(config: Config, opts: ApplyCommandOpt) -> StdResult<()> { pub(crate) fn apply_sql(config: Config, opts: ApplyCommandOpt) -> StdResult<()> {
let mut manager = MigrationManager::try_from(&config)?; let mut manager = MigrationManager::try_from(&config)?;
let file_path = PathBuilder::from(config.directory_path()) let file_path = {
.append(opts.file_name) let mut file_path = config.directory_path().join(opts.file_name);
.default_extension("sql") if file_path.extension().is_none() {
.build(); file_path.set_extension("sql");
}
file_path
};
let content = std::fs::read_to_string(file_path)?; let content = std::fs::read_to_string(file_path)?;

View file

@ -1,8 +1,8 @@
use crate::opts::MakeCommandOpt; use crate::opts::MakeCommandOpt;
use crate::path::PathBuilder;
use crate::Config; use crate::Config;
use crate::StdResult; use crate::StdResult;
use chrono::Local; use chrono::Local;
use std::fs;
pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult<()> { pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult<()> {
let now = Local::now().format("%y%m%d%H%M%S"); let now = Local::now().format("%y%m%d%H%M%S");
@ -17,25 +17,21 @@ pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult<
}) })
.collect(); .collect();
let migration_dir_path = PathBuilder::from(config.migration_dir_path()) let migration_dir_path = config
.append(format!("{}_{}", now, migration_name)) .migration_dir_path()
.build(); .join(format!("{}_{}", now, migration_name));
if !migration_dir_path.exists() { if !migration_dir_path.exists() {
std::fs::create_dir_all(&migration_dir_path)?; fs::create_dir_all(&migration_dir_path)?;
} }
let upgrade_migration_path = PathBuilder::from(&migration_dir_path) let upgrade_migration_path = &migration_dir_path.join("up.sql");
.append("up.sql")
.build();
if !upgrade_migration_path.exists() { if !upgrade_migration_path.exists() {
std::fs::write(upgrade_migration_path, "-- Your SQL goes here\n\n")?; fs::write(upgrade_migration_path, "-- Your SQL goes here\n\n")?;
} }
let downgrade_migration_path = PathBuilder::from(&migration_dir_path) let downgrade_migration_path = &migration_dir_path.join("down.sql");
.append("down.sql")
.build();
if !downgrade_migration_path.exists() { if !downgrade_migration_path.exists() {
std::fs::write( fs::write(
downgrade_migration_path, downgrade_migration_path,
"-- This file should undo anything in `up.sql`\n\n", "-- This file should undo anything in `up.sql`\n\n",
)?; )?;

View file

@ -1,6 +1,5 @@
use crate::error::{Error, MigraResult}; use crate::error::{Error, MigraResult};
use crate::migration::Migration; use crate::migration::Migration;
use crate::path::PathBuilder;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::{env, fs, io}; use std::{env, fs, io};
@ -108,15 +107,11 @@ impl Config {
impl Config { impl Config {
pub fn directory_path(&self) -> PathBuf { pub fn directory_path(&self) -> PathBuf {
PathBuilder::from(&self.manifest_root) self.manifest_root.join(&self.root)
.append(&self.root)
.build()
} }
pub fn migration_dir_path(&self) -> PathBuf { pub fn migration_dir_path(&self) -> PathBuf {
PathBuilder::from(&self.directory_path()) self.directory_path().join("migrations")
.append("migrations")
.build()
} }
pub fn migrations(&self) -> MigraResult<Vec<Migration>> { pub fn migrations(&self) -> MigraResult<Vec<Migration>> {

View file

@ -8,7 +8,6 @@ mod databases;
mod error; mod error;
mod migration; mod migration;
mod opts; mod opts;
mod path;
use crate::error::StdResult; use crate::error::StdResult;
use config::Config; use config::Config;

View file

@ -1,7 +1,6 @@
use crate::config::Config; use crate::config::Config;
use crate::database::DatabaseConnection; use crate::database::DatabaseConnection;
use crate::databases::DatabaseConnectionManager; use crate::databases::DatabaseConnectionManager;
use crate::path::PathBuilder;
use crate::StdResult; use crate::StdResult;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs; use std::fs;
@ -21,8 +20,8 @@ impl Migration {
.file_name() .file_name()
.and_then(|name| name.to_str()) .and_then(|name| name.to_str())
.unwrap_or_default(); .unwrap_or_default();
let upgrade_sql = PathBuilder::from(directory).append("up.sql").build(); let upgrade_sql = directory.join("up.sql");
let downgrade_sql = PathBuilder::from(directory).append("down.sql").build(); let downgrade_sql = directory.join("down.sql");
if upgrade_sql.exists() && downgrade_sql.exists() { if upgrade_sql.exists() && downgrade_sql.exists() {
return Some(Migration { return Some(Migration {

View file

@ -1,70 +0,0 @@
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
#[derive(Debug, Default)]
pub struct PathBuilder {
buf: PathBuf,
}
impl<P: AsRef<Path>> From<P> for PathBuilder {
fn from(path: P) -> Self {
PathBuilder {
buf: path.as_ref().to_path_buf(),
}
}
}
impl PathBuilder {
pub fn append<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.buf.push(path);
self
}
pub fn default_extension<S: AsRef<OsStr>>(&mut self, extension: S) -> &mut Self {
if self.buf.as_path().extension().is_none() {
self.buf.set_extension(extension);
}
self
}
pub fn build(&self) -> PathBuf {
self.buf.clone()
}
}
#[cfg(test)]
mod tests {
use super::Path;
use super::PathBuilder;
#[test]
fn create_path_builder() {
let path = PathBuilder::from("test").build();
assert_eq!(path, Path::new("test"))
}
#[test]
fn append_path_to_builder() {
let path = PathBuilder::from("directory").append("schema.sql").build();
assert_eq!(path, Path::new("directory/schema.sql"))
}
#[test]
fn add_default_extension_for_path() {
let path = PathBuilder::from("directory")
.append("schema")
.default_extension("sql")
.build();
assert_eq!(path, Path::new("directory/schema.sql"));
}
#[test]
fn build_default_path() {
let path = PathBuilder::default().build();
assert_eq!(path, Path::new(""));
}
}