Skip to content

Commit 9ce0ad8

Browse files
committed
Use MigratorTraitSelf with shim
1 parent 07034f7 commit 9ce0ad8

File tree

6 files changed

+169
-42
lines changed

6 files changed

+169
-42
lines changed

sea-orm-migration/src/cli.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ use tracing_subscriber::{EnvFilter, prelude::*};
88
use sea_orm::{ConnectOptions, Database, DbConn, DbErr};
99
use sea_orm_cli::{MigrateSubcommands, run_migrate_generate, run_migrate_init};
1010

11-
use super::MigratorTrait;
11+
use super::MigratorTraitSelf;
1212

1313
const MIGRATION_DIR: &str = "./";
1414

1515
pub async fn run_cli<M>(migrator: M)
1616
where
17-
M: MigratorTrait,
17+
M: MigratorTraitSelf,
1818
{
1919
run_cli_with_connection(migrator, Database::connect).await;
2020
}
@@ -26,7 +26,7 @@ where
2626
/// extensions.
2727
pub async fn run_cli_with_connection<M, F, Fut>(migrator: M, make_connection: F)
2828
where
29-
M: MigratorTrait,
29+
M: MigratorTraitSelf,
3030
F: FnOnce(ConnectOptions) -> Fut,
3131
Fut: Future<Output = Result<DbConn, DbErr>>,
3232
{
@@ -52,13 +52,13 @@ where
5252
}
5353

5454
pub async fn run_migrate<M>(
55-
_: M,
55+
migrator: M,
5656
db: &DbConn,
5757
command: Option<MigrateSubcommands>,
5858
verbose: bool,
5959
) -> Result<(), Box<dyn Error>>
6060
where
61-
M: MigratorTrait,
61+
M: MigratorTraitSelf,
6262
{
6363
let filter = match verbose {
6464
true => "debug",
@@ -85,19 +85,19 @@ where
8585
};
8686

8787
match command {
88-
Some(MigrateSubcommands::Fresh) => M::fresh(db).await?,
89-
Some(MigrateSubcommands::Refresh) => M::refresh(db).await?,
90-
Some(MigrateSubcommands::Reset) => M::reset(db).await?,
91-
Some(MigrateSubcommands::Status) => M::status(db).await?,
92-
Some(MigrateSubcommands::Up { num }) => M::up(db, num).await?,
93-
Some(MigrateSubcommands::Down { num }) => M::down(db, Some(num)).await?,
88+
Some(MigrateSubcommands::Fresh) => migrator.fresh(db).await?,
89+
Some(MigrateSubcommands::Refresh) => migrator.refresh(db).await?,
90+
Some(MigrateSubcommands::Reset) => migrator.reset(db).await?,
91+
Some(MigrateSubcommands::Status) => migrator.status(db).await?,
92+
Some(MigrateSubcommands::Up { num }) => migrator.up(db, num).await?,
93+
Some(MigrateSubcommands::Down { num }) => migrator.down(db, Some(num)).await?,
9494
Some(MigrateSubcommands::Init) => run_migrate_init(MIGRATION_DIR)?,
9595
Some(MigrateSubcommands::Generate {
9696
migration_name,
9797
universal_time: _,
9898
local_time,
9999
}) => run_migrate_generate(MIGRATION_DIR, &migration_name, !local_time)?,
100-
_ => M::up(db, None).await?,
100+
_ => migrator.up(db, None).await?,
101101
};
102102

103103
Ok(())

sea-orm-migration/src/migrator/exec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub fn get_migration_with_status(
6969
macro_rules! exec_with_connection {
7070
($db:ident, $fn:expr) => {{
7171
async {
72-
let db = $db.into_schema_manager_connection();
72+
let db = $db.into_database_executor();
7373

7474
match db.get_database_backend() {
7575
DbBackend::Postgres => {

sea-orm-migration/src/migrator/with_self.rs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,109 @@ pub trait MigratorTraitSelf: Sized + Send + Sync {
167167
}
168168
}
169169

170+
#[async_trait::async_trait]
171+
impl<M> MigratorTraitSelf for M
172+
where
173+
M: super::MigratorTrait + Sized + Send + Sync,
174+
{
175+
fn migrations(&self) -> Vec<Box<dyn MigrationTrait>> {
176+
M::migrations()
177+
}
178+
179+
fn migration_table_name(&self) -> DynIden {
180+
M::migration_table_name()
181+
}
182+
183+
fn get_migration_files(&self) -> Vec<Migration> {
184+
M::get_migration_files()
185+
}
186+
187+
async fn get_migration_models<C>(&self, db: &C) -> Result<Vec<seaql_migrations::Model>, DbErr>
188+
where
189+
C: ConnectionTrait,
190+
{
191+
M::get_migration_models(db).await
192+
}
193+
194+
async fn get_migration_with_status<C>(&self, db: &C) -> Result<Vec<Migration>, DbErr>
195+
where
196+
C: ConnectionTrait,
197+
{
198+
M::get_migration_with_status(db).await
199+
}
200+
201+
async fn get_pending_migrations<C>(&self, db: &C) -> Result<Vec<Migration>, DbErr>
202+
where
203+
C: ConnectionTrait,
204+
{
205+
M::get_pending_migrations(db).await
206+
}
207+
208+
async fn get_applied_migrations<C>(&self, db: &C) -> Result<Vec<Migration>, DbErr>
209+
where
210+
C: ConnectionTrait,
211+
{
212+
M::get_applied_migrations(db).await
213+
}
214+
215+
async fn install<C>(&self, db: &C) -> Result<(), DbErr>
216+
where
217+
C: ConnectionTrait,
218+
{
219+
M::install(db).await
220+
}
221+
222+
/// Check the status of all migrations
223+
async fn status<C>(&self, db: &C) -> Result<(), DbErr>
224+
where
225+
C: ConnectionTrait,
226+
{
227+
M::status(db).await
228+
}
229+
230+
async fn fresh<'c, C>(&self, db: C) -> Result<(), DbErr>
231+
where
232+
C: IntoSchemaManagerConnection<'c>,
233+
{
234+
M::fresh(db).await
235+
}
236+
237+
async fn refresh<'c, C>(&self, db: C) -> Result<(), DbErr>
238+
where
239+
C: IntoSchemaManagerConnection<'c>,
240+
{
241+
M::refresh(db).await
242+
}
243+
244+
async fn reset<'c, C>(&self, db: C) -> Result<(), DbErr>
245+
where
246+
C: IntoSchemaManagerConnection<'c>,
247+
{
248+
M::reset(db).await
249+
}
250+
251+
async fn uninstall<'c, C>(&self, db: C) -> Result<(), DbErr>
252+
where
253+
C: IntoSchemaManagerConnection<'c>,
254+
{
255+
M::uninstall(db).await
256+
}
257+
258+
async fn up<'c, C>(&self, db: C, steps: Option<u32>) -> Result<(), DbErr>
259+
where
260+
C: IntoSchemaManagerConnection<'c>,
261+
{
262+
M::up(db, steps).await
263+
}
264+
265+
async fn down<'c, C>(&self, db: C, steps: Option<u32>) -> Result<(), DbErr>
266+
where
267+
C: IntoSchemaManagerConnection<'c>,
268+
{
269+
M::down(db, steps).await
270+
}
271+
}
272+
170273
async fn exec_fresh<M>(migrator: &M, manager: &SchemaManager<'_>) -> Result<(), DbErr>
171274
where
172275
M: MigratorTraitSelf,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod default;
22
pub mod override_migration_table_name;
3+
pub mod with_self;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use crate::common::migration::*;
2+
use sea_orm_migration::{MigratorTraitSelf, prelude::*};
3+
4+
pub struct Migrator {
5+
pub i: i32,
6+
}
7+
8+
#[async_trait::async_trait]
9+
impl MigratorTraitSelf for Migrator {
10+
fn migrations(&self) -> Vec<Box<dyn MigrationTrait>> {
11+
vec![
12+
Box::new(m20220118_000001_create_cake_table::Migration),
13+
Box::new(m20220118_000002_create_fruit_table::Migration),
14+
Box::new(m20220118_000003_seed_cake_table::Migration),
15+
Box::new(m20220118_000004_create_tea_enum::Migration),
16+
Box::new(m20220923_000001_seed_cake_table::Migration),
17+
Box::new(m20230109_000001_seed_cake_table::Migration),
18+
]
19+
}
20+
}

0 commit comments

Comments
 (0)