Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a query overridable. #358

Merged
merged 2 commits into from
Feb 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion refinery_core/src/runner.rs
Original file line number Diff line number Diff line change
@@ -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 }
}

23 changes: 11 additions & 12 deletions refinery_core/src/traits/async.rs
Original file line number Diff line number Diff line change
@@ -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)?;

@@ -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)?;

@@ -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)?;

28 changes: 16 additions & 12 deletions refinery_core/src/traits/sync.rs
Original file line number Diff line number Diff line change
@@ -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)
}
@@ -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())
@@ -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)