Archived
1
0
Fork 0

refac: rename database connection to postgres

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-14 12:10:12 +03:00
parent 481760ee6e
commit 1b16aff6e5
6 changed files with 20 additions and 20 deletions

View file

@ -1,12 +1,12 @@
use crate::config::Config;
use crate::database::DatabaseConnection;
use crate::database::PostgresConnection;
use crate::opts::ApplyCommandOpt;
use crate::path::PathBuilder;
use crate::StdResult;
use std::convert::TryFrom;
pub(crate) fn apply_sql(config: Config, opts: ApplyCommandOpt) -> StdResult<()> {
let mut connection = DatabaseConnection::try_from(&config)?;
let mut connection = PostgresConnection::try_from(&config)?;
let file_path = PathBuilder::from(config.directory_path())
.append(opts.file_name)

View file

@ -1,11 +1,11 @@
use crate::config::Config;
use crate::database::DatabaseConnection;
use crate::database::PostgresConnection;
use crate::migration::Downgrade;
use crate::StdResult;
use std::convert::TryFrom;
pub(crate) fn downgrade_applied_migrations(config: Config) -> StdResult<()> {
let mut connection = DatabaseConnection::try_from(&config)?;
let mut connection = PostgresConnection::try_from(&config)?;
let applied_migrations = connection.applied_migration_names()?;
let migrations = config.migrations()?;

View file

@ -1,5 +1,5 @@
use crate::config::Config;
use crate::database::DatabaseConnection;
use crate::database::PostgresConnection;
use crate::error::{ErrorKind, StdResult};
const EM_DASH: char = '—';
@ -7,7 +7,7 @@ const EM_DASH: char = '—';
pub(crate) fn print_migration_lists(config: Config) -> StdResult<()> {
let applied_migrations = match config.database_connection_string() {
Ok(ref database_connection_string) => {
let mut connection = DatabaseConnection::open(database_connection_string)?;
let mut connection = PostgresConnection::open(database_connection_string)?;
let applied_migrations = connection.applied_migration_names()?;
println!("Applied migrations:");

View file

@ -1,11 +1,11 @@
use crate::database::DatabaseConnection;
use crate::database::PostgresConnection;
use crate::migration::{Migration, Upgrade};
use crate::Config;
use crate::StdResult;
use std::convert::TryFrom;
pub(crate) fn upgrade_pending_migrations(config: Config) -> StdResult<()> {
let mut connection = DatabaseConnection::try_from(&config)?;
let mut connection = PostgresConnection::try_from(&config)?;
let applied_migration_names = connection.applied_migration_names()?;
let migrations = config.migrations()?;

View file

@ -3,22 +3,22 @@ use crate::StdResult;
use postgres::{Client, Error, NoTls};
use std::convert::TryFrom;
pub struct DatabaseConnection {
pub struct PostgresConnection {
client: Client,
}
impl TryFrom<&Config> for DatabaseConnection {
impl TryFrom<&Config> for PostgresConnection {
type Error = Box<dyn std::error::Error>;
fn try_from(config: &Config) -> Result<Self, Self::Error> {
DatabaseConnection::open(&config.database_connection_string()?)
PostgresConnection::open(&config.database_connection_string()?)
}
}
impl DatabaseConnection {
pub fn open(connection_string: &str) -> StdResult<DatabaseConnection> {
impl PostgresConnection {
pub fn open(connection_string: &str) -> StdResult<PostgresConnection> {
let client = Client::connect(connection_string, NoTls)?;
Ok(DatabaseConnection { client })
Ok(PostgresConnection { client })
}
}
@ -27,7 +27,7 @@ pub fn is_migrations_table_not_found(e: &Error) -> bool {
.contains(r#"relation "migrations" does not exist"#)
}
impl DatabaseConnection {
impl PostgresConnection {
pub fn apply_sql(&mut self, sql_content: &str) -> Result<(), Error> {
self.client.batch_execute(sql_content)
}

View file

@ -1,15 +1,15 @@
use crate::database::DatabaseConnection;
use crate::database::PostgresConnection;
use crate::path::PathBuilder;
use crate::StdResult;
use std::fs;
use std::path::PathBuf;
pub trait Upgrade {
fn upgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()>;
fn upgrade(&self, connection: &mut PostgresConnection) -> StdResult<()>;
}
pub trait Downgrade {
fn downgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()>;
fn downgrade(&self, connection: &mut PostgresConnection) -> StdResult<()>;
}
#[derive(Debug)]
@ -49,7 +49,7 @@ impl Migration {
}
impl Upgrade for Migration {
fn upgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()> {
fn upgrade(&self, connection: &mut PostgresConnection) -> StdResult<()> {
let content = fs::read_to_string(&self.upgrade_sql)?;
connection.create_migrations_table()?;
@ -61,7 +61,7 @@ impl Upgrade for Migration {
}
impl Downgrade for Migration {
fn downgrade(&self, connection: &mut DatabaseConnection) -> StdResult<()> {
fn downgrade(&self, connection: &mut PostgresConnection) -> StdResult<()> {
let content = fs::read_to_string(&self.downgrade_sql)?;
connection.apply_sql(&content)?;