2021-01-31 02:38:35 +03:00
|
|
|
#![deny(clippy::all)]
|
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
mod config;
|
2021-01-31 02:54:23 +03:00
|
|
|
mod opts;
|
2021-01-31 02:38:35 +03:00
|
|
|
|
2021-02-03 01:03:56 +03:00
|
|
|
use chrono::Local;
|
2021-01-31 03:23:43 +03:00
|
|
|
use config::Config;
|
2021-02-02 23:50:42 +03:00
|
|
|
use migra_core::path::PathBuilder;
|
2021-02-03 01:03:56 +03:00
|
|
|
use opts::{AppOpt, ApplyCommandOpt, Command, MakeCommandOpt, StructOpt};
|
2021-01-31 14:54:50 +03:00
|
|
|
use std::fs;
|
2021-01-31 02:38:35 +03:00
|
|
|
|
2021-01-31 03:23:43 +03:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2021-01-31 02:38:35 +03:00
|
|
|
let opt = AppOpt::from_args();
|
2021-01-31 03:23:43 +03:00
|
|
|
|
2021-02-02 00:53:33 +03:00
|
|
|
match opt.command {
|
|
|
|
Command::Init => {
|
2021-01-31 03:23:43 +03:00
|
|
|
Config::initialize()?;
|
2021-02-01 23:51:23 +03:00
|
|
|
}
|
2021-02-02 00:53:33 +03:00
|
|
|
Command::Apply(ApplyCommandOpt { file_name }) => {
|
|
|
|
let config = Config::read(opt.config)?;
|
2021-01-31 13:41:12 +03:00
|
|
|
|
|
|
|
let mut client = migra_core::database::connect(&config.database.connection)?;
|
|
|
|
|
2021-02-02 23:50:42 +03:00
|
|
|
let file_path = PathBuilder::from(config.directory_path())
|
2021-01-31 14:54:50 +03:00
|
|
|
.append(file_name)
|
|
|
|
.default_extension("sql")
|
|
|
|
.build();
|
2021-01-31 13:41:12 +03:00
|
|
|
|
2021-01-31 14:54:50 +03:00
|
|
|
let content = fs::read_to_string(file_path)?;
|
2021-01-31 13:41:12 +03:00
|
|
|
|
|
|
|
match migra_core::database::apply_sql(&mut client, &content) {
|
|
|
|
Ok(_) => {
|
|
|
|
println!("File was applied successfully")
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
println!("{}", err)
|
|
|
|
}
|
|
|
|
}
|
2021-01-31 02:38:35 +03:00
|
|
|
}
|
2021-02-03 01:03:56 +03:00
|
|
|
Command::Make(MakeCommandOpt { migration_name }) => {
|
|
|
|
let config = Config::read(opt.config)?;
|
|
|
|
|
|
|
|
let now = Local::now().format("%y%m%d%H%M%S");
|
|
|
|
|
|
|
|
let migration_name: String = migration_name
|
|
|
|
.to_lowercase()
|
|
|
|
.chars()
|
|
|
|
.map(|c| match c {
|
|
|
|
'0'..='9' | 'a'..='z' => c,
|
|
|
|
_ => '_',
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2021-02-03 10:18:02 +03:00
|
|
|
let migration_dir_path = PathBuilder::from(config.directory_path())
|
2021-02-03 01:03:56 +03:00
|
|
|
.append(format!("{}_{}", now, migration_name))
|
|
|
|
.build();
|
2021-02-03 10:18:02 +03:00
|
|
|
if !migration_dir_path.exists() {
|
|
|
|
fs::create_dir(&migration_dir_path)?;
|
2021-02-03 01:03:56 +03:00
|
|
|
}
|
|
|
|
|
2021-02-03 10:18:02 +03:00
|
|
|
let upgrade_migration_path = PathBuilder::from(&migration_dir_path)
|
|
|
|
.append("up.sql")
|
|
|
|
.build();
|
2021-02-03 01:03:56 +03:00
|
|
|
if !upgrade_migration_path.exists() {
|
|
|
|
fs::write(upgrade_migration_path, "-- Your SQL goes here\n\n")?;
|
|
|
|
}
|
|
|
|
|
2021-02-03 10:18:02 +03:00
|
|
|
let downgrade_migration_path = PathBuilder::from(&migration_dir_path)
|
|
|
|
.append("down.sql")
|
|
|
|
.build();
|
|
|
|
if !downgrade_migration_path.exists() {
|
2021-02-03 01:03:56 +03:00
|
|
|
fs::write(
|
2021-02-03 10:18:02 +03:00
|
|
|
downgrade_migration_path,
|
2021-02-03 01:03:56 +03:00
|
|
|
"-- This file should undo anything in `up.sql`\n\n",
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
}
|
2021-02-02 23:50:42 +03:00
|
|
|
Command::List => {
|
|
|
|
let config = Config::read(opt.config)?;
|
|
|
|
|
|
|
|
let migration_dirs = config.migration_dirs()?;
|
|
|
|
if migration_dirs.is_empty() {
|
|
|
|
println!(
|
|
|
|
"You haven't migrations in {}",
|
|
|
|
config.directory_path().to_str().unwrap()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
migration_dirs.iter().for_each(|dir| {
|
|
|
|
let file_name = dir.file_name().and_then(|name| name.to_str()).unwrap();
|
|
|
|
println!("{}", file_name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Command::Upgrade | Command::Downgrade => {
|
2021-02-02 00:53:33 +03:00
|
|
|
unimplemented!();
|
|
|
|
}
|
2021-01-31 02:38:35 +03:00
|
|
|
}
|
2021-01-31 03:23:43 +03:00
|
|
|
|
|
|
|
Ok(())
|
2021-01-31 02:38:35 +03:00
|
|
|
}
|