@@ -14,6 +14,7 @@ import { ResetMigrationsOnTenant, RunMigrationsOnTenants } from '@storage/events
14
14
import { ERRORS } from '@internal/errors'
15
15
import { DBMigration } from './types'
16
16
import { getSslSettings } from '../util'
17
+ import { MigrationTransformer , DisableConcurrentIndexTransformer } from './transformers'
17
18
18
19
const {
19
20
multitenantDatabaseUrl,
@@ -224,7 +225,7 @@ export async function obtainLockOnMultitenantDB<T>(fn: () => Promise<T>) {
224
225
} finally {
225
226
try {
226
227
await multitenantKnex . raw ( `SELECT pg_advisory_unlock(?);` , [ '-8575985245963000605' ] )
227
- } catch ( e ) { }
228
+ } catch { }
228
229
}
229
230
}
230
231
@@ -550,12 +551,15 @@ export async function migrate({
550
551
shouldCreateStorageSchema,
551
552
upToMigration,
552
553
} : MigrateOptions ) : Promise < Array < Migration > > {
554
+ const accessMethod = await getDefaultAccessMethod ( client )
553
555
return withAdvisoryLock (
554
556
waitForLock ,
555
557
runMigrations ( {
556
558
migrationsDirectory,
557
559
shouldCreateStorageSchema,
558
560
upToMigration,
561
+ // Remove concurrent index creation if we're using oriole db as it does not support it currently
562
+ transformers : accessMethod === 'orioledb' ? [ new DisableConcurrentIndexTransformer ( ) ] : [ ] ,
559
563
} )
560
564
) ( client )
561
565
}
@@ -564,6 +568,7 @@ interface RunMigrationOptions {
564
568
migrationsDirectory : string
565
569
shouldCreateStorageSchema ?: boolean
566
570
upToMigration ?: keyof typeof DBMigration
571
+ transformers ?: MigrationTransformer [ ]
567
572
}
568
573
569
574
/**
@@ -576,6 +581,7 @@ function runMigrations({
576
581
migrationsDirectory,
577
582
shouldCreateStorageSchema,
578
583
upToMigration,
584
+ transformers = [ ] ,
579
585
} : RunMigrationOptions ) {
580
586
return async ( client : BasicPgClient ) => {
581
587
let intendedMigrations = await loadMigrationFilesCached ( migrationsDirectory )
@@ -648,14 +654,17 @@ function runMigrations({
648
654
}
649
655
650
656
for ( const migration of migrationsToRun ) {
651
- const result = await runMigration ( migrationTableName , client ) ( migration )
657
+ const result = await runMigration (
658
+ migrationTableName ,
659
+ client
660
+ ) ( runMigrationTransformers ( migration , transformers ) )
652
661
completedMigrations . push ( result )
653
662
}
654
663
655
664
return completedMigrations
656
- } catch ( e : any ) {
657
- const error : MigrationError = new Error ( `Migration failed. Reason: ${ e . message } ` )
658
- error . cause = e
665
+ } catch ( e ) {
666
+ const error : MigrationError = new Error ( `Migration failed. Reason: ${ ( e as Error ) . message } ` )
667
+ error . cause = e + ''
659
668
throw error
660
669
}
661
670
}
@@ -675,6 +684,30 @@ function filterMigrations(
675
684
return migrations . filter ( notAppliedMigration )
676
685
}
677
686
687
+ /**
688
+ * Transforms provided migration by running all transformers
689
+ * @param migration
690
+ * @param transformers
691
+ */
692
+ function runMigrationTransformers (
693
+ migration : Migration ,
694
+ transformers : MigrationTransformer [ ]
695
+ ) : Migration {
696
+ for ( const transformer of transformers ) {
697
+ migration = transformer . transform ( migration )
698
+ }
699
+ return migration
700
+ }
701
+
702
+ /**
703
+ * Get the current default access method for this database
704
+ * @param client
705
+ */
706
+ async function getDefaultAccessMethod ( client : BasicPgClient ) : Promise < string > {
707
+ const result = await client . query ( `SHOW default_table_access_method` )
708
+ return result . rows ?. [ 0 ] ?. default_table_access_method || ''
709
+ }
710
+
678
711
/**
679
712
* Checks if a table exists
680
713
* @param client
@@ -755,7 +788,7 @@ function withAdvisoryLock<T>(
755
788
} finally {
756
789
try {
757
790
await client . query ( 'SELECT pg_advisory_unlock(-8525285245963000605);' )
758
- } catch ( e ) { }
791
+ } catch { }
759
792
}
760
793
}
761
794
}
@@ -828,11 +861,11 @@ async function refreshMigrationPosition(
828
861
migrations . push ( intendedMigrations [ migration . index ] )
829
862
830
863
// add the other run migrations by updating their id and hash
831
- const afterMigration = newMigrations . slice ( migration . index ) . map ( ( m ) => {
832
- ; ( m as any ) . id = m . id + 1
833
- ; ( m as any ) . hash = intendedMigrations [ m . id ] . hash
834
- return m
835
- } )
864
+ const afterMigration = newMigrations . slice ( migration . index ) . map ( ( m ) => ( {
865
+ ... m ,
866
+ id : m . id + 1 ,
867
+ hash : intendedMigrations [ m . id ] . hash ,
868
+ } ) )
836
869
837
870
migrations . push ( ...afterMigration )
838
871
newMigrations = migrations
@@ -868,18 +901,18 @@ async function refreshMigrationPosition(
868
901
* Memoizes a promise
869
902
* @param func
870
903
*/
871
- function memoizePromise < T , Args extends any [ ] > (
904
+ function memoizePromise < T , Args extends unknown [ ] > (
872
905
func : ( ...args : Args ) => Promise < T >
873
906
) : ( ...args : Args ) => Promise < T > {
874
907
const cache = new Map < string , Promise < T > > ( )
875
908
876
909
function generateKey ( args : Args ) : string {
877
910
return args
878
911
. map ( ( arg ) => {
879
- if ( typeof arg === 'object' ) {
912
+ if ( typeof arg === 'object' && arg !== null ) {
880
913
return Object . entries ( arg ) . sort ( ) . toString ( )
881
914
}
882
- return arg . toString ( )
915
+ return String ( arg )
883
916
} )
884
917
. join ( '|' )
885
918
}
0 commit comments