@@ -4,15 +4,16 @@ use std::{
44 borrow:: Cow ,
55 num:: NonZeroU64 ,
66 path:: { Path , PathBuf } ,
7+ str:: FromStr ,
78 sync:: { RwLock , RwLockReadGuard , RwLockWriteGuard } ,
89} ;
910
1011use ignore:: gitignore:: { Gitignore , GitignoreBuilder } ;
1112use pg_configuration:: {
1213 database:: PartialDatabaseConfiguration ,
1314 diagnostics:: InvalidIgnorePattern ,
14- files:: { FilesConfiguration , MigrationPattern } ,
15- migrations:: { MigrationsConfiguration , PartialMigrationsConfiguration } ,
15+ files:: FilesConfiguration ,
16+ migrations:: { self , MigrationsConfiguration , PartialMigrationsConfiguration } ,
1617 ConfigurationDiagnostic , LinterConfiguration , PartialConfiguration ,
1718} ;
1819use pg_fs:: FileSystem ;
@@ -32,7 +33,7 @@ pub struct Settings {
3233 pub linter : LinterSettings ,
3334
3435 /// Migrations settings
35- pub migrations : Option < Migrations > ,
36+ pub migrations : Option < MigrationSettings > ,
3637}
3738
3839#[ derive( Debug ) ]
@@ -105,7 +106,13 @@ impl Settings {
105106 to_linter_settings ( working_directory. clone ( ) , LinterConfiguration :: from ( linter) ) ?;
106107 }
107108
108- // TODO migrations
109+ // Migrations settings
110+ if let Some ( migrations) = configuration. migrations {
111+ self . migrations = to_migration_settings (
112+ working_directory. clone ( ) ,
113+ MigrationsConfiguration :: from ( migrations) ,
114+ ) ;
115+ }
109116
110117 Ok ( ( ) )
111118 }
@@ -313,68 +320,34 @@ pub struct FilesSettings {
313320}
314321
315322/// Migration settings
316- #[ derive( Debug ) ]
317- pub ( crate ) struct Migrations {
318- path : PathBuf ,
319- pattern : MigrationPattern ,
320- }
321-
322- pub ( crate ) struct Migration {
323- timestamp : u64 ,
324- name : String ,
323+ #[ derive( Debug , Default ) ]
324+ pub struct MigrationSettings {
325+ pub path : Option < PathBuf > ,
326+ pub after : Option < u64 > ,
325327}
326328
327- impl Migrations {
328- fn get_migration ( & self , path : & Path ) -> Option < Migration > {
329- // check if path is a child of the migration directory
330- match path. canonicalize ( ) {
331- Ok ( canonical_child) => match self . path . canonicalize ( ) {
332- Ok ( canonical_dir) => canonical_child. starts_with ( & canonical_dir) ,
333- Err ( _) => return None ,
334- } ,
335- Err ( _) => return None ,
336- } ;
337-
338- match self . pattern {
339- // supabase style migrations/0001_create_table.sql
340- MigrationPattern :: Root => {
341- let file_name = path. file_name ( ) ?. to_str ( ) ?;
342- let timestamp = file_name. split ( '_' ) . next ( ) ?;
343- let name = file_name
344- . split ( '_' )
345- . skip ( 1 )
346- . collect :: < Vec < & str > > ( )
347- . join ( "_" ) ;
348- let timestamp = timestamp. parse ( ) . ok ( ) ?;
349- Some ( Migration { timestamp, name } )
350- }
351- // drizzle / prisma style migrations/0001_create_table/migration.sql
352- MigrationPattern :: Subdirectory => {
353- let relative_path = path. strip_prefix ( & self . path ) . ok ( ) ?;
354- let components: Vec < _ > = relative_path. components ( ) . collect ( ) ;
355- if components. len ( ) != 2 {
356- return None ;
357- }
358- let dir_name = components[ 0 ] . as_os_str ( ) . to_str ( ) ?;
359- let parts: Vec < & str > = dir_name. splitn ( 2 , '_' ) . collect ( ) ;
360- if parts. len ( ) != 2 {
361- return None ;
362- }
363- let timestamp = parts[ 0 ] . parse ( ) . ok ( ) ?;
364- let name = parts[ 1 ] . to_string ( ) ;
365- Some ( Migration { timestamp, name } )
366- }
329+ impl From < PartialMigrationsConfiguration > for MigrationSettings {
330+ fn from ( value : PartialMigrationsConfiguration ) -> Self {
331+ Self {
332+ path : value. migrations_dir . map ( PathBuf :: from) ,
333+ after : value. after ,
367334 }
368335 }
369336}
370337
371- impl From < MigrationsConfiguration > for Migrations {
372- fn from ( value : MigrationsConfiguration ) -> Self {
373- Self {
374- path : value. migration_dir ,
375- pattern : value. migration_pattern ,
376- }
377- }
338+ fn to_migration_settings (
339+ working_directory : Option < PathBuf > ,
340+ conf : MigrationsConfiguration ,
341+ ) -> Option < MigrationSettings > {
342+ tracing:: debug!(
343+ "Migrations configuration: {:?}, dir: {:?}" ,
344+ conf,
345+ working_directory
346+ ) ;
347+ working_directory. map ( |working_directory| MigrationSettings {
348+ path : Some ( working_directory. join ( conf. migrations_dir ) ) ,
349+ after : Some ( conf. after ) ,
350+ } )
378351}
379352
380353/// Limit the size of files to 1.0 MiB by default
0 commit comments