@@ -2,7 +2,7 @@ use std::cmp::Ordering;
22use std:: path:: Path ;
33
44use anyhow:: { anyhow, Context } ;
5- use rorm_declaration:: config:: DatabaseDriver ;
5+ use rorm_declaration:: config:: { DatabaseConfig , DatabaseDriver } ;
66use rorm_declaration:: imr:: { Annotation , DbType } ;
77use rorm_declaration:: migration:: Migration ;
88use rorm_sql:: create_table:: CreateTable ;
@@ -96,41 +96,24 @@ pub async fn apply_migration(
9696 Ok ( ( ) )
9797}
9898
99- /**
100- Applies migrations on the given database
101- */
102- pub async fn run_migrate ( options : MigrateOptions ) -> anyhow:: Result < ( ) > {
103- let db_conf_path = Path :: new ( options. database_config . as_str ( ) ) ;
104-
105- if !& db_conf_path. exists ( ) {
106- println ! (
107- "Couldn't find the database configuration file, created {} and exiting" ,
108- options. database_config. as_str( )
109- ) ;
110- create_db_config ( db_conf_path) ?;
111- return Ok ( ( ) ) ;
112- }
113-
114- let db_conf = deserialize_db_conf ( db_conf_path) ?;
115-
116- if let DatabaseDriver :: SQLite { filename } = & db_conf. driver {
117- if filename. is_empty ( ) {
118- println ! ( "Invalid configuration: Filename for sqlite is empty" ) ;
119- return Ok ( ( ) ) ;
120- }
121- }
122-
123- let p = Path :: new ( options. migration_dir . as_str ( ) ) ;
99+ /// Applies migrations on the given database with a given driver
100+ pub async fn run_migrate_custom (
101+ db_conf : DatabaseConfig ,
102+ migration_dir : String ,
103+ log_sql : bool ,
104+ apply_until : Option < u16 > ,
105+ ) -> anyhow:: Result < ( ) > {
106+ let p = Path :: new ( migration_dir. as_str ( ) ) ;
124107 if !p. exists ( ) || p. is_file ( ) {
125108 println ! (
126109 "Couldn't find the migration directory in {} \n \n \
127110 You can specify an alternative path with --migration-dir <PATH>",
128- options . migration_dir. as_str( )
111+ migration_dir. as_str( )
129112 ) ;
130113 return Ok ( ( ) ) ;
131114 }
132115
133- let existing_migrations = get_existing_migrations ( options . migration_dir . as_str ( ) )
116+ let existing_migrations = get_existing_migrations ( migration_dir. as_str ( ) )
134117 . with_context ( || "Couldn't retrieve existing migrations" ) ?;
135118
136119 if existing_migrations. is_empty ( ) {
@@ -214,7 +197,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
214197 . with_context ( || "Could not create transaction" ) ?;
215198
216199 for ( query_string, bind_params) in statements {
217- if options . log_queries {
200+ if log_sql {
218201 println ! ( "{}" , query_string. as_str( ) ) ;
219202 }
220203
@@ -237,7 +220,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
237220 "SELECT migration_id FROM {} ORDER BY id DESC LIMIT 1;" ,
238221 & last_migration_table_name
239222 ) ,
240- options . log_queries
223+ log_sql
241224 )
242225 . as_str ( ) ,
243226 )
@@ -257,11 +240,11 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
257240 migration,
258241 & pool,
259242 last_migration_table_name,
260- options . log_queries ,
243+ log_sql ,
261244 )
262245 . await ?;
263246
264- if let Some ( apply_until) = options . apply_until {
247+ if let Some ( apply_until) = apply_until {
265248 if migration. id == apply_until {
266249 println ! (
267250 "Applied all migrations until (inclusive) migration {apply_until:04}"
@@ -283,7 +266,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
283266 migration,
284267 & pool,
285268 last_migration_table_name,
286- options . log_queries ,
269+ log_sql ,
287270 )
288271 . await ?;
289272 continue ;
@@ -297,7 +280,7 @@ pub async fn run_migrate(options: MigrateOptions) -> anyhow::Result<()> {
297280 }
298281 }
299282
300- if let Some ( apply_until) = options . apply_until {
283+ if let Some ( apply_until) = apply_until {
301284 match migration. id . cmp ( & apply_until) {
302285 Ordering :: Equal => {
303286 if apply {
@@ -331,3 +314,34 @@ To correct, empty the {last_migration_table_name} table or reset the whole datab
331314
332315 Ok ( ( ) )
333316}
317+
318+ /// Applies migrations on the given database
319+ pub async fn run_migrate ( options : MigrateOptions ) -> anyhow:: Result < ( ) > {
320+ let db_conf_path = Path :: new ( options. database_config . as_str ( ) ) ;
321+
322+ if !& db_conf_path. exists ( ) {
323+ println ! (
324+ "Couldn't find the database configuration file, created {} and exiting" ,
325+ options. database_config. as_str( )
326+ ) ;
327+ create_db_config ( db_conf_path) ?;
328+ return Ok ( ( ) ) ;
329+ }
330+
331+ let db_conf = deserialize_db_conf ( db_conf_path) ?;
332+
333+ if let DatabaseDriver :: SQLite { filename } = & db_conf. driver {
334+ if filename. is_empty ( ) {
335+ println ! ( "Invalid configuration: Filename for sqlite is empty" ) ;
336+ return Ok ( ( ) ) ;
337+ }
338+ }
339+
340+ run_migrate_custom (
341+ db_conf,
342+ options. migration_dir ,
343+ options. log_queries ,
344+ options. apply_until ,
345+ )
346+ . await
347+ }
0 commit comments