Skip to content

Commit

Permalink
Make a query overridable.
Browse files Browse the repository at this point in the history
  • Loading branch information
masato-hi committed Dec 31, 2024
1 parent 8e120f6 commit c70d82a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion refinery_core/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub struct Report {

impl Report {
/// Instantiate a new Report
pub(crate) fn new(applied_migrations: Vec<Migration>) -> Report {
pub fn new(applied_migrations: Vec<Migration>) -> Report {
Report { applied_migrations }
}

Expand Down
23 changes: 11 additions & 12 deletions refinery_core/src/traits/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,20 @@ where
ASSERT_MIGRATIONS_TABLE_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

fn get_last_applied_migration_query(migration_table_name: &str) -> String {
GET_LAST_APPLIED_MIGRATION_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

fn get_applied_migrations_query(migration_table_name: &str) -> String {
GET_APPLIED_MIGRATIONS_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

async fn get_last_applied_migration(
&mut self,
migration_table_name: &str,
) -> Result<Option<Migration>, Error> {
let mut migrations = self
.query(
&GET_LAST_APPLIED_MIGRATION_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
)
.query(Self::get_last_applied_migration_query(migration_table_name).as_str())
.await
.migration_err("error getting last applied migration", None)?;

Expand All @@ -152,10 +157,7 @@ where
migration_table_name: &str,
) -> Result<Vec<Migration>, Error> {
let migrations = self
.query(
&GET_APPLIED_MIGRATIONS_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
)
.query(Self::get_applied_migrations_query(migration_table_name).as_str())
.await
.migration_err("error getting applied migrations", None)?;

Expand All @@ -178,10 +180,7 @@ where
.migration_err("error asserting migrations table", None)?;

let applied_migrations = self
.query(
&GET_APPLIED_MIGRATIONS_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
)
.get_applied_migrations(migration_table_name)
.await
.migration_err("error getting current schema version", None)?;

Expand Down
28 changes: 16 additions & 12 deletions refinery_core/src/traits/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,24 @@ pub trait Migrate: Query<Vec<Migration>>
where
Self: Sized,
{
// Needed cause some database vendors like Mssql have a non sql standard way of checking the migrations table
fn assert_migrations_table_query(migration_table_name: &str) -> String {
ASSERT_MIGRATIONS_TABLE_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

fn get_last_applied_migration_query(migration_table_name: &str) -> String {
GET_LAST_APPLIED_MIGRATION_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

fn get_applied_migrations_query(migration_table_name: &str) -> String {
GET_APPLIED_MIGRATIONS_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
}

fn assert_migrations_table(&mut self, migration_table_name: &str) -> Result<usize, Error> {
// Needed cause some database vendors like Mssql have a non sql standard way of checking the migrations table,
// thou on this case it's just to be consistent with the async trait `AsyncMigrate`
self.execute(
[ASSERT_MIGRATIONS_TABLE_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
.as_str()]
.into_iter(),
[Self::assert_migrations_table_query(migration_table_name).as_str()].into_iter(),
)
.migration_err("error asserting migrations table", None)
}
Expand All @@ -109,10 +119,7 @@ where
migration_table_name: &str,
) -> Result<Option<Migration>, Error> {
let mut migrations = self
.query(
&GET_LAST_APPLIED_MIGRATION_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
)
.query(Self::get_last_applied_migration_query(migration_table_name).as_str())
.migration_err("error getting last applied migration", None)?;

Ok(migrations.pop())
Expand All @@ -123,10 +130,7 @@ where
migration_table_name: &str,
) -> Result<Vec<Migration>, Error> {
let migrations = self
.query(
&GET_APPLIED_MIGRATIONS_QUERY
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
)
.query(Self::get_applied_migrations_query(migration_table_name).as_str())
.migration_err("error getting applied migrations", None)?;

Ok(migrations)
Expand Down

0 comments on commit c70d82a

Please sign in to comment.