refac: restructure database
This commit is contained in:
parent
d1d09ac912
commit
eb0775e35d
14 changed files with 62 additions and 51 deletions
|
@ -1,5 +1,6 @@
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::migration::{DatabaseMigrationManager, MigrationManager};
|
use crate::database::prelude::*;
|
||||||
|
use crate::database::MigrationManager;
|
||||||
use crate::opts::ApplyCommandOpt;
|
use crate::opts::ApplyCommandOpt;
|
||||||
use crate::StdResult;
|
use crate::StdResult;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::migration::{DatabaseMigrationManager, MigrationManager};
|
use crate::database::prelude::*;
|
||||||
|
use crate::database::MigrationManager;
|
||||||
use crate::StdResult;
|
use crate::StdResult;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use crate::config::Config;
|
use crate::config::{Config, MIGRA_TOML_FILENAME};
|
||||||
use crate::config::MIGRA_TOML_FILENAME;
|
|
||||||
use crate::StdResult;
|
use crate::StdResult;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::databases::*;
|
use crate::database::migration::filter_pending_migrations;
|
||||||
|
use crate::database::prelude::*;
|
||||||
|
use crate::database::{DatabaseConnectionManager, Migration, MigrationManager};
|
||||||
use crate::error::{Error, StdResult};
|
use crate::error::{Error, StdResult};
|
||||||
use crate::migration::{
|
|
||||||
filter_pending_migrations, DatabaseMigrationManager, Migration, MigrationManager,
|
|
||||||
};
|
|
||||||
|
|
||||||
const EM_DASH: char = '—';
|
const EM_DASH: char = '—';
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use crate::migration::{
|
use crate::database::migration::*;
|
||||||
filter_pending_migrations, DatabaseMigrationManager, Migration, MigrationManager,
|
|
||||||
};
|
|
||||||
use crate::Config;
|
use crate::Config;
|
||||||
use crate::StdResult;
|
use crate::StdResult;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
|
use crate::database::migration::Migration;
|
||||||
use crate::error::{Error, MigraResult};
|
use crate::error::{Error, MigraResult};
|
||||||
use crate::migration::Migration;
|
|
||||||
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};
|
||||||
|
|
17
migra-cli/src/database/adapter.rs
Normal file
17
migra-cli/src/database/adapter.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use crate::error::StdResult;
|
||||||
|
|
||||||
|
pub trait ToSql {
|
||||||
|
fn to_sql(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type ToSqlParams<'a> = &'a [&'a dyn ToSql];
|
||||||
|
|
||||||
|
impl ToSql for &str {
|
||||||
|
fn to_sql(&self) -> String {
|
||||||
|
format!("'{}'", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TryFromSql<QueryResultRow>: Sized {
|
||||||
|
fn try_from_sql(row: QueryResultRow) -> StdResult<Self>;
|
||||||
|
}
|
|
@ -1,32 +1,4 @@
|
||||||
use crate::StdResult;
|
use super::prelude::*;
|
||||||
|
|
||||||
pub trait ToSql {
|
|
||||||
fn to_sql(&self) -> String;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub type ToSqlParams<'a> = &'a [&'a dyn ToSql];
|
|
||||||
|
|
||||||
impl ToSql for &str {
|
|
||||||
fn to_sql(&self) -> String {
|
|
||||||
format!("'{}'", self)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait TryFromSql<QueryResultRow>: Sized {
|
|
||||||
fn try_from_sql(row: QueryResultRow) -> StdResult<Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait OpenDatabaseConnection: Sized {
|
|
||||||
fn open(connection_string: &str) -> StdResult<Self>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait DatabaseConnection {
|
|
||||||
fn batch_execute(&mut self, query: &str) -> StdResult<()>;
|
|
||||||
|
|
||||||
fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<u64>;
|
|
||||||
|
|
||||||
fn query<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<Vec<Vec<String>>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn merge_query_with_params(query: &str, params: ToSqlParams) -> String {
|
pub(crate) fn merge_query_with_params(query: &str, params: ToSqlParams) -> String {
|
||||||
params
|
params
|
3
migra-cli/src/database/clients/mod.rs
Normal file
3
migra-cli/src/database/clients/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
mod postgres;
|
||||||
|
|
||||||
|
pub use self::postgres::*;
|
|
@ -1,4 +1,5 @@
|
||||||
use crate::database::*;
|
use crate::database::builder::merge_query_with_params;
|
||||||
|
use crate::database::prelude::*;
|
||||||
use crate::error::StdResult;
|
use crate::error::StdResult;
|
||||||
use postgres::{Client, NoTls};
|
use postgres::{Client, NoTls};
|
||||||
|
|
|
@ -1,11 +1,20 @@
|
||||||
mod postgres;
|
use super::adapter::ToSqlParams;
|
||||||
|
use super::clients::*;
|
||||||
pub use self::postgres::*;
|
|
||||||
|
|
||||||
use crate::config::{DatabaseConfig, SupportedDatabaseClient};
|
use crate::config::{DatabaseConfig, SupportedDatabaseClient};
|
||||||
use crate::database::{DatabaseConnection, OpenDatabaseConnection};
|
|
||||||
use crate::error::StdResult;
|
use crate::error::StdResult;
|
||||||
|
|
||||||
|
pub trait OpenDatabaseConnection: Sized {
|
||||||
|
fn open(connection_string: &str) -> StdResult<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait DatabaseConnection {
|
||||||
|
fn batch_execute(&mut self, query: &str) -> StdResult<()>;
|
||||||
|
|
||||||
|
fn execute<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<u64>;
|
||||||
|
|
||||||
|
fn query<'b>(&mut self, query: &str, params: ToSqlParams<'b>) -> StdResult<Vec<Vec<String>>>;
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) struct DatabaseConnectionManager {
|
pub(crate) struct DatabaseConnectionManager {
|
||||||
config: DatabaseConfig,
|
config: DatabaseConfig,
|
||||||
}
|
}
|
|
@ -1,6 +1,5 @@
|
||||||
|
use super::connection::{DatabaseConnection, DatabaseConnectionManager};
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::database::DatabaseConnection;
|
|
||||||
use crate::databases::DatabaseConnectionManager;
|
|
||||||
use crate::StdResult;
|
use crate::StdResult;
|
||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
use std::fs;
|
use std::fs;
|
14
migra-cli/src/database/mod.rs
Normal file
14
migra-cli/src/database/mod.rs
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
pub(crate) mod adapter;
|
||||||
|
pub(crate) mod builder;
|
||||||
|
pub(crate) mod clients;
|
||||||
|
pub(crate) mod connection;
|
||||||
|
pub(crate) mod migration;
|
||||||
|
|
||||||
|
pub mod prelude {
|
||||||
|
pub use super::adapter::{ToSql, ToSqlParams, TryFromSql};
|
||||||
|
pub use super::connection::{DatabaseConnection, OpenDatabaseConnection};
|
||||||
|
pub use super::migration::DatabaseMigrationManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) use connection::DatabaseConnectionManager;
|
||||||
|
pub(crate) use migration::{Migration, MigrationManager};
|
|
@ -4,9 +4,7 @@
|
||||||
mod commands;
|
mod commands;
|
||||||
mod config;
|
mod config;
|
||||||
mod database;
|
mod database;
|
||||||
mod databases;
|
|
||||||
mod error;
|
mod error;
|
||||||
mod migration;
|
|
||||||
mod opts;
|
mod opts;
|
||||||
|
|
||||||
use crate::error::StdResult;
|
use crate::error::StdResult;
|
||||||
|
|
Reference in a new issue