env.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. from __future__ import annotations
  18. import contextlib
  19. from logging import getLogger
  20. from logging.config import fileConfig
  21. from alembic import context
  22. from airflow import settings
  23. from airflow.providers.fab.auth_manager.models.db import FABDBManager
  24. # this is the Alembic Config object, which provides
  25. # access to the values within the .ini file in use.
  26. config = context.config
  27. version_table = FABDBManager.version_table_name
  28. # Interpret the config file for Python logging.
  29. # This line sets up loggers basically.
  30. if not getLogger().handlers and config.config_file_name:
  31. fileConfig(config.config_file_name, disable_existing_loggers=False)
  32. # add your model's MetaData object here
  33. # for 'autogenerate' support
  34. # from myapp import mymodel
  35. # target_metadata = mymodel.Base.metadata
  36. target_metadata = FABDBManager.metadata
  37. # other values from the config, defined by the needs of env.py,
  38. # can be acquired:
  39. # my_important_option = config.get_main_option("my_important_option")
  40. # ... etc.
  41. def include_object(_, name, type_, *args):
  42. if type_ == "table" and name not in target_metadata.tables:
  43. return False
  44. return True
  45. def run_migrations_offline():
  46. """
  47. Run migrations in 'offline' mode.
  48. This configures the context with just a URL
  49. and not an Engine, though an Engine is acceptable
  50. here as well. By skipping the Engine creation
  51. we don't even need a DBAPI to be available.
  52. Calls to context.execute() here emit the given string to the
  53. script output.
  54. """
  55. context.configure(
  56. url=settings.SQL_ALCHEMY_CONN,
  57. target_metadata=target_metadata,
  58. literal_binds=True,
  59. compare_type=True,
  60. compare_server_default=True,
  61. render_as_batch=True,
  62. version_table=version_table,
  63. include_object=include_object,
  64. )
  65. with context.begin_transaction():
  66. context.run_migrations()
  67. def run_migrations_online():
  68. """
  69. Run migrations in 'online' mode.
  70. In this scenario we need to create an Engine
  71. and associate a connection with the context.
  72. """
  73. def process_revision_directives(context, revision, directives):
  74. if getattr(config.cmd_opts, "autogenerate", False):
  75. script = directives[0]
  76. if script.upgrade_ops and script.upgrade_ops.is_empty():
  77. directives[:] = []
  78. print("No change detected in ORM schema, skipping revision.")
  79. with contextlib.ExitStack() as stack:
  80. connection = config.attributes.get("connection", None)
  81. if not connection:
  82. connection = stack.push(settings.engine.connect())
  83. context.configure(
  84. connection=connection,
  85. transaction_per_migration=True,
  86. target_metadata=target_metadata,
  87. compare_type=True,
  88. compare_server_default=True,
  89. include_object=include_object,
  90. render_as_batch=True,
  91. process_revision_directives=process_revision_directives,
  92. version_table=version_table,
  93. )
  94. with context.begin_transaction():
  95. context.run_migrations()
  96. if context.is_offline_mode():
  97. run_migrations_offline()
  98. else:
  99. run_migrations_online()