1- use anyhow:: bail;
21use clap:: Parser ;
3- use migration:: { Migrator , Options , SchemaDataManager } ;
4- use sea_orm :: { ConnectOptions , Database } ;
5- use sea_orm_migration :: { IntoSchemaManagerConnection , SchemaManager } ;
6- use std :: collections :: HashMap ;
2+ use migration:: {
3+ Migrator ,
4+ data :: { Direction , MigratorWithData , Options , Runner } ,
5+ } ;
76use trustify_module_storage:: config:: StorageConfig ;
87
98#[ derive( clap:: Parser , Debug , Clone ) ]
@@ -13,6 +12,7 @@ struct Cli {
1312}
1413
1514#[ derive( clap:: Subcommand , Debug , Clone ) ]
15+ #[ allow( clippy:: large_enum_variant) ]
1616enum Command {
1717 /// List all data migrations
1818 List ,
@@ -67,13 +67,6 @@ struct Run {
6767 storage : StorageConfig ,
6868}
6969
70- #[ derive( Debug , Clone , Copy , PartialEq , Eq , Default , clap:: ValueEnum ) ]
71- pub enum Direction {
72- #[ default]
73- Up ,
74- Down ,
75- }
76-
7770impl Run {
7871 fn direction ( & self ) -> Direction {
7972 if self . down {
@@ -83,47 +76,23 @@ impl Run {
8376 }
8477 }
8578
79+ #[ allow( clippy:: expect_used) ]
8680 pub async fn run ( self ) -> anyhow:: Result < ( ) > {
8781 let direction = self . direction ( ) ;
88-
89- let migrations = Migrator :: data_migrations ( )
90- . into_iter ( )
91- . map ( |migration| ( migration. name ( ) . to_string ( ) , migration) )
92- . collect :: < HashMap < _ , _ > > ( ) ;
93-
94- let mut running = vec ! [ ] ;
95-
96- for migration in self . migrations {
97- let Some ( migration) = migrations. get ( & migration) else {
98- bail ! ( "Migration {migration} not found" ) ;
99- } ;
100- running. push ( migration) ;
101- }
102-
10382 let storage = self . storage . into_storage ( false ) . await ?;
10483
105- let url = self
106- . database_url
107- . expect ( "Environment variable 'DATABASE_URL' not set" ) ;
108- let schema = self . database_schema . unwrap_or_else ( || "public" . to_owned ( ) ) ;
109-
110- let connect_options = ConnectOptions :: new ( url)
111- . set_schema_search_path ( schema)
112- . to_owned ( ) ;
113-
114- let db = Database :: connect ( connect_options) . await ?;
115-
116- let manager = SchemaManager :: new ( db. into_schema_manager_connection ( ) ) ;
117- let manager = SchemaDataManager :: new ( & manager, & storage, & self . options ) ;
118-
119- for run in running {
120- tracing:: info!( "Running data migration: {}" , run. name( ) ) ;
121-
122- match direction {
123- Direction :: Up => run. up ( & manager) . await ?,
124- Direction :: Down => run. down ( & manager) . await ?,
125- }
84+ Runner {
85+ direction,
86+ storage,
87+ migrations : self . migrations ,
88+ database_url : self
89+ . database_url
90+ . expect ( "Environment variable 'DATABASE_URL' not set" ) ,
91+ database_schema : self . database_schema ,
92+ options : self . options ,
12693 }
94+ . run :: < Migrator > ( )
95+ . await ?;
12796
12897 Ok ( ( ) )
12998 }
@@ -143,6 +112,7 @@ impl Command {
143112 }
144113}
145114
115+ #[ allow( clippy:: unwrap_used) ]
146116#[ tokio:: main]
147117async fn main ( ) {
148118 let cli = Cli :: parse ( ) ;
0 commit comments