@@ -123,27 +123,34 @@ abstract class TormBase<D extends Driver> {
123123 this . migrations_manager . initialize_database ( )
124124 }
125125
126- if ( auto_migrate ) {
127- while ( this . migrations_manager . is_database_outdated ( ) ) {
128- this . status = 'outdated'
129- const current_version = this . schemas . version ( )
130- const migration_operation : MigrationOperation = {
131- start_version : current_version ,
132- backup : false ,
133- next_version : - 1 ,
134- }
135- if ( backup_before_migrate ) {
136- if ( ! options ?. backups ?. folder ) {
137- throw new Error ( `backups_folder must be defined in order to use automatic backups` )
126+ if ( auto_migrate && this . migrations_manager . is_database_outdated ( ) ) {
127+ try {
128+ driver . exec ( `PRAGMA locking_mode = EXCLUSIVE` )
129+
130+ while ( this . migrations_manager . is_database_outdated ( ) ) {
131+ this . status = 'outdated'
132+ const current_version = this . schemas . version ( )
133+ const migration_operation : MigrationOperation = {
134+ start_version : current_version ,
135+ backup : false ,
136+ next_version : - 1 ,
137+ }
138+ if ( backup_before_migrate ) {
139+ if ( ! options ?. backups ?. folder ) {
140+ throw new Error ( `backups_folder must be defined in order to use automatic backups` )
141+ }
142+ migration_operation . backup = true
143+ this . backup ( options . backups . folder , `migration_backup_v${ current_version } ` )
138144 }
139- migration_operation . backup = true
140- this . backup ( options . backups . folder , `migration_backup_v${ current_version } ` )
145+ const next_version = this . migrations_manager . upgrade_database ( )
146+ migration_operation . next_version = next_version
147+ migration_operations . push ( migration_operation )
141148 }
142- const next_version = this . migrations_manager . upgrade_database ( )
143- migration_operation . next_version = next_version
144- migration_operations . push ( migration_operation )
149+ this . initialize_models ( )
150+
151+ } finally {
152+ driver . exec ( `PRAGMA locking_mode = normal` )
145153 }
146- this . initialize_models ( )
147154 }
148155 if ( this . migrations_manager . is_database_outdated ( ) ) {
149156 this . status = 'outdated'
@@ -164,6 +171,8 @@ abstract class TormBase<D extends Driver> {
164171
165172 public abstract backup ( folder : string , name : string ) : void
166173
174+ public abstract transaction < T > ( fn : ( ) => T ) : ( ) => T
175+
167176 private initialize_models = ( ) => {
168177 for ( const model of this . model_registry ) {
169178 model . prepare_queries ( )
0 commit comments