chore: return manual migrations table name
This commit is contained in:
parent
14cee3eaea
commit
30482fbb79
14 changed files with 72 additions and 51 deletions
|
@ -25,7 +25,10 @@ pub trait ManageTransaction: BatchExecute {
|
|||
|
||||
pub trait ManageMigrations: BatchExecute {
|
||||
fn apply_sql(&mut self, sql: &str) -> MigraResult<()> {
|
||||
self.batch_execute(sql).map_err(|_| Error::FailedApplySql)
|
||||
self.batch_execute(sql).map_err(|err| {
|
||||
dbg!(err);
|
||||
Error::FailedApplySql
|
||||
})
|
||||
}
|
||||
|
||||
fn create_migrations_table(&mut self) -> MigraResult<()>;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::commands;
|
||||
use crate::error::{MigraResult, StdResult};
|
||||
use crate::error::MigraResult;
|
||||
use crate::opts::Command;
|
||||
use crate::AppOpt;
|
||||
use crate::Config;
|
||||
|
@ -24,7 +24,7 @@ impl App {
|
|||
Config::read(self.config_path())
|
||||
}
|
||||
|
||||
pub fn run_command(&self) -> StdResult<()> {
|
||||
pub fn run_command(&self) -> migra::StdResult<()> {
|
||||
match self.app_opt.command.clone() {
|
||||
Command::Init => {
|
||||
commands::initialize_migra_manifest(self)?;
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use crate::config::SupportedDatabaseClient;
|
||||
use crate::Config;
|
||||
#[cfg(feature = "mysql")]
|
||||
use migra::clients::MysqlClient;
|
||||
#[cfg(feature = "postgres")]
|
||||
|
@ -10,19 +11,38 @@ use migra::clients::{AnyClient, OpenDatabaseConnection};
|
|||
pub fn create(
|
||||
client_kind: &SupportedDatabaseClient,
|
||||
connection_string: &str,
|
||||
migrations_table_name: &str,
|
||||
) -> migra::Result<AnyClient> {
|
||||
let client: AnyClient = match client_kind {
|
||||
#[cfg(feature = "postgres")]
|
||||
SupportedDatabaseClient::Postgres => Box::new(PostgresClient::new(&connection_string)?),
|
||||
SupportedDatabaseClient::Postgres => Box::new(PostgresClient::manual(
|
||||
connection_string,
|
||||
migrations_table_name,
|
||||
)?),
|
||||
#[cfg(feature = "mysql")]
|
||||
SupportedDatabaseClient::Mysql => Box::new(MysqlClient::new(&connection_string)?),
|
||||
SupportedDatabaseClient::Mysql => Box::new(MysqlClient::manual(
|
||||
connection_string,
|
||||
migrations_table_name,
|
||||
)?),
|
||||
#[cfg(feature = "sqlite")]
|
||||
SupportedDatabaseClient::Sqlite => Box::new(SqliteClient::new(&connection_string)?),
|
||||
SupportedDatabaseClient::Sqlite => Box::new(SqliteClient::manual(
|
||||
connection_string,
|
||||
migrations_table_name,
|
||||
)?),
|
||||
};
|
||||
|
||||
Ok(client)
|
||||
}
|
||||
|
||||
pub fn create_from_config(config: &Config) -> migra::StdResult<AnyClient> {
|
||||
create(
|
||||
&config.database.client(),
|
||||
&config.database.connection_string()?,
|
||||
&config.migrations.table_name(),
|
||||
)
|
||||
.map_err(From::from)
|
||||
}
|
||||
|
||||
pub fn with_transaction<TrxFnMut, Res>(
|
||||
client: &mut AnyClient,
|
||||
trx_fn: &mut TrxFnMut,
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
use crate::app::App;
|
||||
use crate::client::maybe_with_transaction;
|
||||
use crate::opts::ApplyCommandOpt;
|
||||
use crate::StdResult;
|
||||
|
||||
pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> StdResult<()> {
|
||||
pub(crate) fn apply_sql(app: &App, cmd_opts: &ApplyCommandOpt) -> migra::StdResult<()> {
|
||||
let config = app.config()?;
|
||||
let mut client = crate::client::create(
|
||||
&config.database.client(),
|
||||
&config.database.connection_string()?,
|
||||
&config.migrations.table_name(),
|
||||
)?;
|
||||
|
||||
let file_contents = cmd_opts
|
||||
|
|
|
@ -2,15 +2,14 @@ use crate::app::App;
|
|||
use crate::client;
|
||||
use crate::client::maybe_with_transaction;
|
||||
use crate::opts::DowngradeCommandOpt;
|
||||
use crate::StdResult;
|
||||
use std::cmp;
|
||||
|
||||
pub(crate) fn rollback_applied_migrations(app: &App, opts: &DowngradeCommandOpt) -> StdResult<()> {
|
||||
pub(crate) fn rollback_applied_migrations(
|
||||
app: &App,
|
||||
opts: &DowngradeCommandOpt,
|
||||
) -> migra::StdResult<()> {
|
||||
let config = app.config()?;
|
||||
let mut client = client::create(
|
||||
&config.database.client(),
|
||||
&config.database.connection_string()?,
|
||||
)?;
|
||||
let mut client = client::create_from_config(&config)?;
|
||||
|
||||
client.create_migrations_table()?;
|
||||
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
use crate::app::App;
|
||||
use crate::config::{Config, MIGRA_TOML_FILENAME};
|
||||
use crate::StdResult;
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub(crate) fn initialize_migra_manifest(app: &App) -> StdResult<()> {
|
||||
pub(crate) fn initialize_migra_manifest(app: &App) -> migra::StdResult<()> {
|
||||
let config_path = app.config_path().cloned().map_or_else(
|
||||
|| PathBuf::from(MIGRA_TOML_FILENAME),
|
||||
|mut config_path| {
|
||||
|
|
|
@ -1,15 +1,19 @@
|
|||
use crate::app::App;
|
||||
use crate::client;
|
||||
use crate::error::{Error, StdResult};
|
||||
use crate::error::Error;
|
||||
use migra::migration;
|
||||
|
||||
const EM_DASH: char = '—';
|
||||
|
||||
pub(crate) fn print_migration_lists(app: &App) -> StdResult<()> {
|
||||
pub(crate) fn print_migration_lists(app: &App) -> migra::StdResult<()> {
|
||||
let config = app.config()?;
|
||||
let applied_migrations = match config.database.connection_string() {
|
||||
Ok(ref database_connection_string) => {
|
||||
let mut client = client::create(&config.database.client(), database_connection_string)?;
|
||||
let mut client = client::create(
|
||||
&config.database.client(),
|
||||
database_connection_string,
|
||||
&config.migrations.table_name(),
|
||||
)?;
|
||||
let applied_migrations = client
|
||||
.get_applied_migrations()
|
||||
.unwrap_or_else(|_| migration::List::new());
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use crate::app::App;
|
||||
use crate::opts::MakeCommandOpt;
|
||||
use crate::StdResult;
|
||||
use chrono::Local;
|
||||
use std::fs;
|
||||
|
||||
pub(crate) fn make_migration(app: &App, opts: &MakeCommandOpt) -> StdResult<()> {
|
||||
pub(crate) fn make_migration(app: &App, opts: &MakeCommandOpt) -> migra::StdResult<()> {
|
||||
let config = app.config()?;
|
||||
let date_format = config.migrations.date_format();
|
||||
let formatted_current_timestamp = Local::now().format(&date_format);
|
||||
|
|
|
@ -2,15 +2,14 @@ use crate::app::App;
|
|||
use crate::client;
|
||||
use crate::client::maybe_with_transaction;
|
||||
use crate::opts::UpgradeCommandOpt;
|
||||
use crate::StdResult;
|
||||
use migra::migration;
|
||||
|
||||
pub(crate) fn upgrade_pending_migrations(app: &App, opts: &UpgradeCommandOpt) -> StdResult<()> {
|
||||
pub(crate) fn upgrade_pending_migrations(
|
||||
app: &App,
|
||||
opts: &UpgradeCommandOpt,
|
||||
) -> migra::StdResult<()> {
|
||||
let config = app.config()?;
|
||||
let mut client = client::create(
|
||||
&config.database.client(),
|
||||
&config.database.connection_string()?,
|
||||
)?;
|
||||
let mut client = client::create_from_config(&config)?;
|
||||
|
||||
client.create_migrations_table()?;
|
||||
|
||||
|
|
|
@ -228,7 +228,7 @@ impl MigrationsConfig {
|
|||
pub(crate) const MIGRA_TOML_FILENAME: &str = "Migra.toml";
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub(crate) struct Config {
|
||||
pub struct Config {
|
||||
#[serde(skip)]
|
||||
manifest_root: PathBuf,
|
||||
|
||||
|
|
|
@ -39,12 +39,12 @@ impl Migration {
|
|||
&self.name
|
||||
}
|
||||
|
||||
fn upgrade_sql_content(&self) -> StdResult<String> {
|
||||
fn upgrade_sql_content(&self) -> migra::StdResult<String> {
|
||||
let content = fs::read_to_string(&self.upgrade_sql_file_path)?;
|
||||
Ok(content)
|
||||
}
|
||||
|
||||
fn downgrade_sql_content(&self) -> StdResult<String> {
|
||||
fn downgrade_sql_content(&self) -> migra::StdResult<String> {
|
||||
let content = fs::read_to_string(&self.downgrade_sql_file_path)?;
|
||||
Ok(content)
|
||||
}
|
||||
|
@ -90,17 +90,17 @@ pub fn is_migrations_table_not_found<D: std::fmt::Display>(error: D) -> bool {
|
|||
}
|
||||
|
||||
pub trait ManageMigration {
|
||||
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()>;
|
||||
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> migra::StdResult<()>;
|
||||
|
||||
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()>;
|
||||
fn create_migrations_table(&self, conn: &mut AnyConnection) -> migra::StdResult<()>;
|
||||
|
||||
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64>;
|
||||
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult<u64>;
|
||||
|
||||
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64>;
|
||||
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult<u64>;
|
||||
|
||||
fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>>;
|
||||
fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult<Vec<String>>;
|
||||
|
||||
fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> {
|
||||
fn upgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> {
|
||||
let content = migration.upgrade_sql_content()?;
|
||||
|
||||
self.create_migrations_table(conn)?;
|
||||
|
@ -110,7 +110,7 @@ pub trait ManageMigration {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> StdResult<()> {
|
||||
fn downgrade(&self, conn: &mut AnyConnection, migration: &Migration) -> migra::StdResult<()> {
|
||||
let content = migration.downgrade_sql_content()?;
|
||||
|
||||
self.apply_sql(conn, &content)?;
|
||||
|
@ -121,16 +121,16 @@ pub trait ManageMigration {
|
|||
}
|
||||
|
||||
impl ManageMigration for MigrationManager {
|
||||
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> StdResult<()> {
|
||||
fn apply_sql(&self, conn: &mut AnyConnection, sql_content: &str) -> migra::StdResult<()> {
|
||||
conn.batch_execute(sql_content)
|
||||
}
|
||||
|
||||
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
||||
fn create_migrations_table(&self, conn: &mut AnyConnection) -> migra::StdResult<()> {
|
||||
let stmt = conn.create_migration_table_stmt(&self.migrations_table_name);
|
||||
conn.batch_execute(&stmt)
|
||||
}
|
||||
|
||||
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> {
|
||||
fn insert_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult<u64> {
|
||||
conn.execute(
|
||||
&format!(
|
||||
"INSERT INTO {} (name) VALUES ($1)",
|
||||
|
@ -140,7 +140,7 @@ impl ManageMigration for MigrationManager {
|
|||
)
|
||||
}
|
||||
|
||||
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> {
|
||||
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> migra::StdResult<u64> {
|
||||
conn.execute(
|
||||
&format!(
|
||||
"DELETE FROM {} WHERE name = $1",
|
||||
|
@ -150,7 +150,7 @@ impl ManageMigration for MigrationManager {
|
|||
)
|
||||
}
|
||||
|
||||
fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>> {
|
||||
fn applied_migration_names(&self, conn: &mut AnyConnection) -> migra::StdResult<Vec<String>> {
|
||||
let res = conn
|
||||
.query(
|
||||
&format!(
|
||||
|
|
|
@ -2,11 +2,11 @@ use super::client_rusqlite::Connection::AnyConnection;
|
|||
use crate::error::StdResult;
|
||||
|
||||
pub trait ManageTransaction {
|
||||
fn begin_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>;
|
||||
fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>;
|
||||
|
||||
fn rollback_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>;
|
||||
fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>;
|
||||
|
||||
fn commit_transaction(&self, conn: &mut AnyConnection) -> StdResult<()>;
|
||||
fn commit_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()>;
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -19,15 +19,15 @@ impl TransactionManager {
|
|||
}
|
||||
|
||||
impl ManageTransaction for TransactionManager {
|
||||
fn begin_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
||||
fn begin_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> {
|
||||
conn.batch_execute("BEGIN")
|
||||
}
|
||||
|
||||
fn rollback_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
||||
fn rollback_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> {
|
||||
conn.batch_execute("ROLLBACK")
|
||||
}
|
||||
|
||||
fn commit_transaction(&self, conn: &mut AnyConnection) -> StdResult<()> {
|
||||
fn commit_transaction(&self, conn: &mut AnyConnection) -> migra::StdResult<()> {
|
||||
conn.batch_execute("COMMIT")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,7 +4,6 @@ use std::io;
|
|||
use std::mem;
|
||||
use std::result;
|
||||
|
||||
pub type StdResult<T> = result::Result<T, Box<dyn std::error::Error>>;
|
||||
pub type MigraResult<T> = result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -16,12 +16,11 @@ pub use error::Error;
|
|||
|
||||
mod opts;
|
||||
|
||||
use crate::error::StdResult;
|
||||
use app::App;
|
||||
use config::Config;
|
||||
use opts::{AppOpt, StructOpt};
|
||||
|
||||
fn main() -> StdResult<()> {
|
||||
fn main() -> migra::StdResult<()> {
|
||||
#[cfg(feature = "dotenv")]
|
||||
dotenv::dotenv().ok();
|
||||
|
||||
|
|
Reference in a new issue