@@ -13,20 +13,20 @@ import { error } from '../../../lib/log/logger.js';
1313import { getConnection , pool } from '../../../lib/postgres/connection.js' ;
1414import { createMigrationTable } from '../../install/createMigrationTable.js' ;
1515
16- async function getCurrentInstalledVersion ( module ) {
16+ async function getCurrentInstalledVersion ( module , connection = null ) {
1717 /** Check for current installed version */
1818 const check = await select ( )
1919 . from ( 'migration' )
2020 . where ( 'module' , '=' , module )
21- . load ( pool ) ;
21+ . load ( connection || pool ) ;
2222 if ( ! check ) {
2323 return '0.0.1' ;
2424 } else {
2525 return check . version ;
2626 }
2727}
2828
29- async function migrateModule ( module ) {
29+ async function migrateModule ( module , connection = null ) {
3030 /** Check if the module has migration folder, if not ignore it */
3131 if ( ! existsSync ( path . resolve ( module . path , 'migration' ) ) ) {
3232 return ;
@@ -42,15 +42,20 @@ async function migrateModule(module) {
4242 . map ( ( dirent ) => dirent . name . replace ( 'Version-' , '' ) . replace ( '.js' , '' ) )
4343 . sort ( ( first , second ) => semver . lt ( first , second ) ) ;
4444
45- const currentInstalledVersion = await getCurrentInstalledVersion ( module . name ) ;
45+ const currentInstalledVersion = await getCurrentInstalledVersion (
46+ module . name ,
47+ connection
48+ ) ;
4649
4750 for ( const version of migrations ) {
4851 /** If the version is lower or equal the installed version, ignore it */
4952 if ( semver . lte ( version , currentInstalledVersion ) ) {
5053 continue ;
5154 }
52- const connection = await getConnection ( ) ;
53- await startTransaction ( connection ) ;
55+ const migrationConnection = connection || ( await getConnection ( ) ) ;
56+ if ( ! connection ) {
57+ await startTransaction ( migrationConnection ) ;
58+ }
5459
5560 /** We expect the migration script to provide a function as a default export */
5661 try {
@@ -59,32 +64,36 @@ async function migrateModule(module) {
5964 path . resolve ( module . path , 'migration' , `Version-${ version } .js` )
6065 )
6166 ) ;
62- await versionModule . default ( connection ) ;
67+ await versionModule . default ( migrationConnection ) ;
6368
6469 await insertOnUpdate ( 'migration' , [ 'module' ] )
6570 . given ( {
6671 module : module . name ,
6772 version
6873 } )
69- . execute ( connection , false ) ;
70- await commit ( connection ) ;
74+ . execute ( migrationConnection , false ) ;
75+ if ( ! connection ) {
76+ await commit ( migrationConnection ) ;
77+ }
7178 } catch ( e ) {
72- await rollback ( connection ) ;
79+ if ( ! connection ) {
80+ await rollback ( migrationConnection ) ;
81+ }
7382 throw new Error (
7483 `Migration failed for module ${ module . name } , version ${ version } \n${ e } `
7584 ) ;
7685 }
7786 }
7887}
7988
80- export async function migrate ( modules ) {
89+ export async function migrate ( modules , connection = null ) {
8190 try {
82- const connection = await getConnection ( ) ;
91+ const psqlConnection = connection || ( await getConnection ( ) ) ;
8392 // Create a migration table if not exists. This is for the first time installation
84- await createMigrationTable ( connection ) ;
93+ await createMigrationTable ( psqlConnection ) ;
8594
8695 for ( const module of modules ) {
87- await migrateModule ( module ) ;
96+ await migrateModule ( module , connection ) ;
8897 }
8998 } catch ( e ) {
9099 error ( e ) ;
0 commit comments