Skip to content

Commit

Permalink
refactor: replace qualified path sqlx::Database with use
Browse files Browse the repository at this point in the history
Signed-off-by: Saurav Sharma <[email protected]>
  • Loading branch information
iamsauravsharma committed Oct 15, 2024
1 parent 8ed53c2 commit 7fcdc12
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 141 deletions.
29 changes: 15 additions & 14 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use std::io::Write;

use clap::{Parser, Subcommand};
use sqlx::Database;

use crate::error::Error;
use crate::migrator::{Migrate, Plan};
Expand All @@ -41,11 +42,11 @@ impl MigrationCommand {
/// # Errors
/// If migration command fails to complete and raise some issue
pub async fn parse_and_run<DB>(
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
let migration_command = Self::parse();
migration_command.run(connection, migrator).await
Expand All @@ -57,11 +58,11 @@ impl MigrationCommand {
/// If migration command fails to complete and raise some issue
pub async fn run<DB>(
&self,
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
self.sub_command
.handle_subcommand(migrator, connection)
Expand Down Expand Up @@ -92,10 +93,10 @@ impl SubCommand {
async fn handle_subcommand<DB>(
&self,
migrator: Box<dyn Migrate<DB>>,
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
match self {
SubCommand::Apply(apply) => apply.run(connection, migrator).await?,
Expand All @@ -108,11 +109,11 @@ impl SubCommand {
}

async fn drop_migrations<DB>(
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
migrator.ensure_migration_table_exists(connection).await?;
if !migrator
Expand All @@ -128,11 +129,11 @@ where
}

async fn list_migrations<DB>(
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
let migration_plan = migrator.generate_migration_plan(connection, None).await?;

Expand Down Expand Up @@ -221,11 +222,11 @@ struct Apply {
impl Apply {
async fn run<DB>(
&self,
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
let plan;
if let Some(count) = self.count {
Expand Down Expand Up @@ -323,11 +324,11 @@ struct Revert {
impl Revert {
async fn run<DB>(
&self,
connection: &mut <DB as sqlx::Database>::Connection,
connection: &mut <DB as Database>::Connection,
migrator: Box<dyn Migrate<DB>>,
) -> Result<(), Error>
where
DB: sqlx::Database,
DB: Database,
{
let plan;
if let Some(count) = self.count {
Expand Down
6 changes: 3 additions & 3 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ macro_rules! migration {

/// Macro for implementing the [`migration`] macro for the `Any`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// This macro calls [`migration`] macro with db value already set as
/// `sqlx::Any`
#[macro_export]
#[cfg(all(
Expand All @@ -66,7 +66,7 @@ macro_rules! any_migration {

/// Macro for implementing the [`migration`] macro for the `MySql`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// This macro calls [`migration`] macro with db value already set as
/// `sqlx::MySql`
#[macro_export]
#[cfg(feature = "mysql")]
Expand All @@ -85,7 +85,7 @@ macro_rules! mysql_migration {

/// Macro for implementing the [`migration`] macro for the `Postgres`.
///
/// This macro calls [`migration`] macro with db value already set asg
/// This macro calls [`migration`] macro with db value already set as
/// `sqlx::Postgres`
#[macro_export]
#[cfg(feature = "postgres")]
Expand Down
78 changes: 33 additions & 45 deletions src/migrator/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sqlx::MySql;
use sqlx::Postgres;
#[cfg(feature = "sqlite")]
use sqlx::Sqlite;
use sqlx::{Any, Arguments};
use sqlx::{Any, Arguments, Database};

#[cfg(feature = "mysql")]
use super::mysql;
Expand All @@ -19,16 +19,16 @@ use crate::migration::{AppliedMigrationSqlRow, Migration};

/// get database name
async fn get_database_name(
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
) -> Result<Option<String>, Error> {
let backend_name = connection.backend_name();
let database_name_query = match backend_name {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => Some(postgres::current_database_query()),
<Postgres as Database>::NAME => Some(postgres::current_database_query()),
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => None,
<Sqlite as Database>::NAME => None,
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => Some(mysql::current_database_query()),
<MySql as Database>::NAME => Some(mysql::current_database_query()),
_ => return Err(Error::UnsupportedDatabase),
};
if let Some(sql) = database_name_query {
Expand All @@ -44,21 +44,17 @@ async fn get_database_name(
impl DatabaseOperation<Any> for Migrator<Any> {
async fn ensure_migration_table_exists(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
) -> Result<(), Error> {
let sql_query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => {
<Postgres as Database>::NAME => {
postgres::create_migrator_table_query(self.table_name())
}
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => {
sqlite::create_migrator_table_query(self.table_name())
}
<Sqlite as Database>::NAME => sqlite::create_migrator_table_query(self.table_name()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => {
mysql::create_migrator_table_query(self.table_name())
}
<MySql as Database>::NAME => mysql::create_migrator_table_query(self.table_name()),
_ => return Err(Error::UnsupportedDatabase),
};
sqlx::query(&sql_query).execute(connection).await?;
Expand All @@ -67,15 +63,15 @@ impl DatabaseOperation<Any> for Migrator<Any> {

async fn drop_migration_table_if_exists(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
) -> Result<(), Error> {
let sql_query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => postgres::drop_table_query(self.table_name()),
<Postgres as Database>::NAME => postgres::drop_table_query(self.table_name()),
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => sqlite::drop_table_query(self.table_name()),
<Sqlite as Database>::NAME => sqlite::drop_table_query(self.table_name()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => mysql::drop_table_query(self.table_name()),
<MySql as Database>::NAME => mysql::drop_table_query(self.table_name()),
_ => return Err(Error::UnsupportedDatabase),
};
sqlx::query(&sql_query).execute(connection).await?;
Expand All @@ -84,16 +80,16 @@ impl DatabaseOperation<Any> for Migrator<Any> {

async fn add_migration_to_db_table(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
migration: &Box<dyn Migration<Any>>,
) -> Result<(), Error> {
let sql_query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => postgres::add_migration_query(self.table_name()),
<Postgres as Database>::NAME => postgres::add_migration_query(self.table_name()),
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => sqlite::add_migration_query(self.table_name()),
<Sqlite as Database>::NAME => sqlite::add_migration_query(self.table_name()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => mysql::add_migration_query(self.table_name()),
<MySql as Database>::NAME => mysql::add_migration_query(self.table_name()),
_ => return Err(Error::UnsupportedDatabase),
};
sqlx::query(&sql_query)
Expand All @@ -106,18 +102,16 @@ impl DatabaseOperation<Any> for Migrator<Any> {

async fn delete_migration_from_db_table(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
migration: &Box<dyn Migration<Any>>,
) -> Result<(), Error> {
let sql_query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => {
postgres::delete_migration_query(self.table_name())
}
<Postgres as Database>::NAME => postgres::delete_migration_query(self.table_name()),
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => sqlite::delete_migration_query(self.table_name()),
<Sqlite as Database>::NAME => sqlite::delete_migration_query(self.table_name()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => mysql::delete_migration_query(self.table_name()),
<MySql as Database>::NAME => mysql::delete_migration_query(self.table_name()),
_ => return Err(Error::UnsupportedDatabase),
};
sqlx::query(&sql_query)
Expand All @@ -130,40 +124,37 @@ impl DatabaseOperation<Any> for Migrator<Any> {

async fn fetch_applied_migration_from_db(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
connection: &mut <Any as Database>::Connection,
) -> Result<Vec<AppliedMigrationSqlRow>, Error> {
let backend_name = connection.backend_name();
let query = match backend_name {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => postgres::fetch_rows_query(self.table_name()),
<Postgres as Database>::NAME => postgres::fetch_rows_query(self.table_name()),
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => sqlite::fetch_rows_query(self.table_name()),
<Sqlite as Database>::NAME => sqlite::fetch_rows_query(self.table_name()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => mysql::fetch_rows_query(self.table_name()),
<MySql as Database>::NAME => mysql::fetch_rows_query(self.table_name()),
_ => return Err(Error::UnsupportedDatabase),
};
Ok(sqlx::query_as::<_, AppliedMigrationSqlRow>(&query)
.fetch_all(connection)
.await?)
}

async fn lock(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
) -> Result<(), Error> {
async fn lock(&self, connection: &mut <Any as Database>::Connection) -> Result<(), Error> {
let database_name = get_database_name(connection).await?;
if let Some(name) = database_name {
let mut arguments = AnyArguments::default();
let query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => {
<Postgres as Database>::NAME => {
arguments.add(postgres::get_lock_id(&name, self.table_name()))?;
postgres::lock_database_query()
}
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => return Ok(()),
<Sqlite as Database>::NAME => return Ok(()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => {
<MySql as Database>::NAME => {
arguments.add(mysql::get_lock_id(&name, self.table_name()))?;
mysql::lock_database_query()
}
Expand All @@ -176,23 +167,20 @@ impl DatabaseOperation<Any> for Migrator<Any> {
Ok(())
}

async fn unlock(
&self,
connection: &mut <Any as sqlx::Database>::Connection,
) -> Result<(), Error> {
async fn unlock(&self, connection: &mut <Any as Database>::Connection) -> Result<(), Error> {
let database_name = get_database_name(connection).await?;
if let Some(name) = database_name {
let mut arguments = AnyArguments::default();
let query = match connection.backend_name() {
#[cfg(feature = "postgres")]
<Postgres as sqlx::Database>::NAME => {
<Postgres as Database>::NAME => {
arguments.add(postgres::get_lock_id(&name, self.table_name()))?;
postgres::unlock_database_query()
}
#[cfg(feature = "sqlite")]
<Sqlite as sqlx::Database>::NAME => return Ok(()),
<Sqlite as Database>::NAME => return Ok(()),
#[cfg(feature = "mysql")]
<MySql as sqlx::Database>::NAME => {
<MySql as Database>::NAME => {
arguments.add(mysql::get_lock_id(&name, self.table_name()))?;
mysql::unlock_database_query()
}
Expand Down
Loading

0 comments on commit 7fcdc12

Please sign in to comment.