refac: add migrations table name to manifest

This commit is contained in:
Dmitriy Pleshevskiy 2021-04-26 12:18:12 +03:00
parent eef7980222
commit 83a4155d76
4 changed files with 45 additions and 17 deletions

View File

@ -17,11 +17,14 @@ impl OpenDatabaseConnection for MySqlConnection {
} }
impl DatabaseStatements for MySqlConnection { impl DatabaseStatements for MySqlConnection {
fn create_migration_table_stmt(&self) -> &'static str { fn create_migration_table_stmt(&self, migrations_table_name: &str) -> String {
r#"CREATE TABLE IF NOT EXISTS migrations ( format!(
id int AUTO_INCREMENT PRIMARY KEY, r#"CREATE TABLE IF NOT EXISTS {} (
name varchar(256) NOT NULL UNIQUE id int AUTO_INCREMENT PRIMARY KEY,
)"# name varchar(256) NOT NULL UNIQUE
)"#,
migrations_table_name
)
} }
} }

View File

@ -15,11 +15,14 @@ impl OpenDatabaseConnection for PostgresConnection {
} }
impl DatabaseStatements for PostgresConnection { impl DatabaseStatements for PostgresConnection {
fn create_migration_table_stmt(&self) -> &'static str { fn create_migration_table_stmt(&self, migrations_table_name: &str) -> String {
r#"CREATE TABLE IF NOT EXISTS migrations ( format!(
id serial PRIMARY KEY, r#"CREATE TABLE IF NOT EXISTS {} (
name text NOT NULL UNIQUE id serial PRIMARY KEY,
)"# name text NOT NULL UNIQUE
)"#,
migrations_table_name
)
} }
} }

View File

@ -10,7 +10,7 @@ pub trait OpenDatabaseConnection: Sized {
} }
pub trait DatabaseStatements { pub trait DatabaseStatements {
fn create_migration_table_stmt(&self) -> &'static str; fn create_migration_table_stmt(&self, migrations_table_name: &str) -> String;
} }
pub trait SupportsTransactionalDdl { pub trait SupportsTransactionalDdl {

View File

@ -50,11 +50,15 @@ impl Migration {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct MigrationManager; pub struct MigrationManager {
migrations_table_name: String,
}
impl MigrationManager { impl MigrationManager {
pub fn new() -> Self { pub fn new() -> Self {
MigrationManager MigrationManager {
migrations_table_name: String::from("migrations"),
}
} }
} }
@ -109,21 +113,39 @@ impl ManageMigration for MigrationManager {
} }
fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> { fn create_migrations_table(&self, conn: &mut AnyConnection) -> StdResult<()> {
let stmt = conn.create_migration_table_stmt(); let stmt = conn.create_migration_table_stmt(&self.migrations_table_name);
conn.batch_execute(&stmt) 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) -> StdResult<u64> {
conn.execute("INSERT INTO migrations (name) VALUES ($1)", &[&name]) conn.execute(
&format!(
"INSERT INTO {} (name) VALUES ($1)",
&self.migrations_table_name
),
&[&name],
)
} }
fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> { fn delete_migration_info(&self, conn: &mut AnyConnection, name: &str) -> StdResult<u64> {
conn.execute("DELETE FROM migrations WHERE name = $1", &[&name]) conn.execute(
&format!(
"DELETE FROM {} WHERE name = $1",
&self.migrations_table_name
),
&[&name],
)
} }
fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>> { fn applied_migration_names(&self, conn: &mut AnyConnection) -> StdResult<Vec<String>> {
let res = conn let res = conn
.query("SELECT name FROM migrations ORDER BY id DESC", &[]) .query(
&format!(
"SELECT name FROM {} ORDER BY id DESC",
&self.migrations_table_name
),
&[],
)
.or_else(|e| { .or_else(|e| {
if is_migrations_table_not_found(&e) { if is_migrations_table_not_found(&e) {
Ok(Vec::new()) Ok(Vec::new())