Skip to content

Commit cbdece5

Browse files
masato-hijxs
andauthored
Make a query overridable. (#358)
Co-authored-by: João Oliveira <[email protected]>
1 parent 154ffc8 commit cbdece5

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

refinery_core/src/runner.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ pub struct Report {
204204

205205
impl Report {
206206
/// Instantiate a new Report
207-
pub(crate) fn new(applied_migrations: Vec<Migration>) -> Report {
207+
pub fn new(applied_migrations: Vec<Migration>) -> Report {
208208
Report { applied_migrations }
209209
}
210210

refinery_core/src/traits/async.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -132,15 +132,20 @@ where
132132
ASSERT_MIGRATIONS_TABLE_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
133133
}
134134

135+
fn get_last_applied_migration_query(migration_table_name: &str) -> String {
136+
GET_LAST_APPLIED_MIGRATION_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
137+
}
138+
139+
fn get_applied_migrations_query(migration_table_name: &str) -> String {
140+
GET_APPLIED_MIGRATIONS_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
141+
}
142+
135143
async fn get_last_applied_migration(
136144
&mut self,
137145
migration_table_name: &str,
138146
) -> Result<Option<Migration>, Error> {
139147
let mut migrations = self
140-
.query(
141-
&GET_LAST_APPLIED_MIGRATION_QUERY
142-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
143-
)
148+
.query(Self::get_last_applied_migration_query(migration_table_name).as_str())
144149
.await
145150
.migration_err("error getting last applied migration", None)?;
146151

@@ -152,10 +157,7 @@ where
152157
migration_table_name: &str,
153158
) -> Result<Vec<Migration>, Error> {
154159
let migrations = self
155-
.query(
156-
&GET_APPLIED_MIGRATIONS_QUERY
157-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
158-
)
160+
.query(Self::get_applied_migrations_query(migration_table_name).as_str())
159161
.await
160162
.migration_err("error getting applied migrations", None)?;
161163

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

180182
let applied_migrations = self
181-
.query(
182-
&GET_APPLIED_MIGRATIONS_QUERY
183-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
184-
)
183+
.get_applied_migrations(migration_table_name)
185184
.await
186185
.migration_err("error getting current schema version", None)?;
187186

refinery_core/src/traits/sync.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,24 @@ pub trait Migrate: Query<Vec<Migration>>
9292
where
9393
Self: Sized,
9494
{
95+
// Needed cause some database vendors like Mssql have a non sql standard way of checking the migrations table
96+
fn assert_migrations_table_query(migration_table_name: &str) -> String {
97+
ASSERT_MIGRATIONS_TABLE_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
98+
}
99+
100+
fn get_last_applied_migration_query(migration_table_name: &str) -> String {
101+
GET_LAST_APPLIED_MIGRATION_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
102+
}
103+
104+
fn get_applied_migrations_query(migration_table_name: &str) -> String {
105+
GET_APPLIED_MIGRATIONS_QUERY.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
106+
}
107+
95108
fn assert_migrations_table(&mut self, migration_table_name: &str) -> Result<usize, Error> {
96109
// Needed cause some database vendors like Mssql have a non sql standard way of checking the migrations table,
97110
// thou on this case it's just to be consistent with the async trait `AsyncMigrate`
98111
self.execute(
99-
[ASSERT_MIGRATIONS_TABLE_QUERY
100-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name)
101-
.as_str()]
102-
.into_iter(),
112+
[Self::assert_migrations_table_query(migration_table_name).as_str()].into_iter(),
103113
)
104114
.migration_err("error asserting migrations table", None)
105115
}
@@ -109,10 +119,7 @@ where
109119
migration_table_name: &str,
110120
) -> Result<Option<Migration>, Error> {
111121
let mut migrations = self
112-
.query(
113-
&GET_LAST_APPLIED_MIGRATION_QUERY
114-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
115-
)
122+
.query(Self::get_last_applied_migration_query(migration_table_name).as_str())
116123
.migration_err("error getting last applied migration", None)?;
117124

118125
Ok(migrations.pop())
@@ -123,10 +130,7 @@ where
123130
migration_table_name: &str,
124131
) -> Result<Vec<Migration>, Error> {
125132
let migrations = self
126-
.query(
127-
&GET_APPLIED_MIGRATIONS_QUERY
128-
.replace("%MIGRATION_TABLE_NAME%", migration_table_name),
129-
)
133+
.query(Self::get_applied_migrations_query(migration_table_name).as_str())
130134
.migration_err("error getting applied migrations", None)?;
131135

132136
Ok(migrations)

0 commit comments

Comments
 (0)