Archived
1
0
Fork 0

refac: remove stmt constants from manager trait

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-15 23:22:13 +03:00
parent 33f392e18e
commit 92c07f4181
3 changed files with 10 additions and 16 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "migra-cli" name = "migra-cli"
version = "0.1.1" version = "0.1.2"
authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"] authors = ["Dmitriy Pleshevskiy <dmitriy@ideascup.me>"]
edition = "2018" edition = "2018"
description = "Simple SQL migration manager for your project" description = "Simple SQL migration manager for your project"

View file

@ -9,7 +9,7 @@ pub trait ToSql {
impl ToSql for &str { impl ToSql for &str {
fn to_sql(&self) -> String { fn to_sql(&self) -> String {
format!(r#""{}""#, self) format!("'{}'", self)
} }
} }

View file

@ -67,17 +67,6 @@ pub fn is_migrations_table_not_found<D: std::fmt::Display>(error: D) -> bool {
} }
pub trait DatabaseMigrationManager { pub trait DatabaseMigrationManager {
const CREATE_MIGRATIONS_STMT: &'static str = r#"
CREATE TABLE IF NOT EXISTS migrations (
id serial PRIMARY KEY,
name text NOT NULL UNIQUE
)
"#;
const INSERT_MIGRATION_STMT: &'static str = "INSERT INTO migrations (name) VALUES ($1)";
const DELETE_MIGRATION_STMT: &'static str = "DELETE FROM migrations WHERE name = $1";
fn apply_sql(&mut self, sql_content: &str) -> StdResult<()>; fn apply_sql(&mut self, sql_content: &str) -> StdResult<()>;
fn create_migrations_table(&mut self) -> StdResult<()>; fn create_migrations_table(&mut self) -> StdResult<()>;
@ -115,15 +104,20 @@ where
} }
fn create_migrations_table(&mut self) -> StdResult<()> { fn create_migrations_table(&mut self) -> StdResult<()> {
self.conn.batch_execute(Self::CREATE_MIGRATIONS_STMT) self.conn.batch_execute(
r#"CREATE TABLE IF NOT EXISTS migrations (
id serial PRIMARY KEY,
name text NOT NULL UNIQUE
)"#
)
} }
fn insert_migration_info(&mut self, name: &str) -> StdResult<u64> { fn insert_migration_info(&mut self, name: &str) -> StdResult<u64> {
self.conn.execute(Self::INSERT_MIGRATION_STMT, &[&name]) self.conn.execute("INSERT INTO migrations (name) VALUES ($1)", &[&name])
} }
fn delete_migration_info(&mut self, name: &str) -> StdResult<u64> { fn delete_migration_info(&mut self, name: &str) -> StdResult<u64> {
self.conn.execute(Self::DELETE_MIGRATION_STMT, &[&name]) self.conn.execute("DELETE FROM migrations WHERE name = $1", &[&name])
} }
} }