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

View file

@ -1,8 +1,8 @@
use crate::opts::MakeCommandOpt;
use crate::path::PathBuilder;
use crate::Config;
use crate::StdResult;
use chrono::Local;
use std::fs;
pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult<()> {
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();
let migration_dir_path = PathBuilder::from(config.migration_dir_path())
.append(format!("{}_{}", now, migration_name))
.build();
let migration_dir_path = config
.migration_dir_path()
.join(format!("{}_{}", now, migration_name));
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)
.append("up.sql")
.build();
let upgrade_migration_path = &migration_dir_path.join("up.sql");
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)
.append("down.sql")
.build();
let downgrade_migration_path = &migration_dir_path.join("down.sql");
if !downgrade_migration_path.exists() {
std::fs::write(
fs::write(
downgrade_migration_path,
"-- This file should undo anything in `up.sql`\n\n",
)?;

View file

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

View file

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

View file

@ -1,7 +1,6 @@
use crate::config::Config;
use crate::database::DatabaseConnection;
use crate::databases::DatabaseConnectionManager;
use crate::path::PathBuilder;
use crate::StdResult;
use std::convert::TryFrom;
use std::fs;
@ -21,8 +20,8 @@ impl Migration {
.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();
let upgrade_sql = directory.join("up.sql");
let downgrade_sql = directory.join("down.sql");
if upgrade_sql.exists() && downgrade_sql.exists() {
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(""));
}
}