ashb commented on a change in pull request #8219: Add migration waiting script 
and log cleaner
URL: https://github.com/apache/airflow/pull/8219#discussion_r408673753
 
 

 ##########
 File path: airflow/utils/db.py
 ##########
 @@ -561,6 +562,37 @@ def initdb():
     Base.metadata.create_all(settings.engine)  # pylint: disable=no-member
 
 
+def wait_for_migrations(timeout):
+    """
+    Function to wait for all airflow migrations to complete. Used for 
launching airflow in k8s
+    @param timeout:
+    @return:
+    """
+    from alembic.config import Config
+    from alembic.runtime.migration import MigrationContext
+
+    package_dir = os.path.dirname(importlib.util.find_spec('airflow').origin)
+    directory = os.path.join(package_dir, 'migrations')
+    config = Config(os.path.join(package_dir, 'alembic.ini'))
+    config.set_main_option('script_location', directory)
+    config.set_main_option('sqlalchemy.url', settings.SQL_ALCHEMY_CONN)
+    script_ = ScriptDirectory.from_config(config)
+    with settings.engine.connect() as connection:
+        context = MigrationContext.configure(connection)
+        ticker = 0
+        while True:
+            source_heads = set(script_.get_heads())
+            db_heads = set(context.get_current_heads())
+            if source_heads == db_heads:
+                break
+            if ticker >= timeout:
+                raise TimeoutError("There are still unapplied migrations after 
{} "
+                                   "seconds.".format(ticker))
+            ticker += 1
+            time.sleep(1)
+            logging.info('Waiting for migrations... %s second(s)', ticker)
 
 Review comment:
   ```suggestion
               log.info('Waiting for migrations... %s second(s)', ticker)
   ```

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to