kosteev commented on a change in pull request #16311:
URL: https://github.com/apache/airflow/pull/16311#discussion_r660014270



##########
File path: airflow/utils/db.py
##########
@@ -702,9 +727,33 @@ def upgradedb():
         for err in errs:
             log.error("Automatic migration is not available\n%s", err)
         return
+
+    dialect = session.connection().dialect
+    if dialect.name == 'postgresql':
+        log.info('Acquiring lock on database for upgrade')
+        session.connection().execute('select PG_ADVISORY_LOCK(2);')
+
+    if dialect.name == 'mysql' and dialect.server_version_info >= (
+        5,
+        6,
+    ):
+        log.info('Acquiring lock on database for upgrade')
+        session.connection().execute("select GET_LOCK('db_upgrade',1800);")
+
     command.upgrade(config, 'heads')
     add_default_pool_if_not_exists()
 
+    if dialect.name == 'postgresql':
+        log.info('Releasing lock on database for upgrade')
+        session.connection().execute('select PG_ADVISORY_UNLOCK(2);')
+
+    if dialect.name == 'mysql' and dialect.server_version_info >= (
+        5,
+        6,
+    ):
+        log.info('Releasing lock on database for upgrade')
+        session.connection().execute("select RELEASE_LOCK('db_upgrade');")

Review comment:
       Also, would be good to use it as decorator like
   ```
   @lock_db
   @provide_session
   def initdb(session=None):
   ```
   And same for `upgradedb` method.
   
   What do you think guys?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to