base.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from __future__ import annotations
  19. from typing import TYPE_CHECKING, Any
  20. from sqlalchemy import Column, Integer, MetaData, String, text
  21. from sqlalchemy.orm import registry
  22. from airflow.configuration import conf
  23. SQL_ALCHEMY_SCHEMA = conf.get("database", "SQL_ALCHEMY_SCHEMA")
  24. # For more information about what the tokens in the naming convention
  25. # below mean, see:
  26. # https://docs.sqlalchemy.org/en/14/core/metadata.html#sqlalchemy.schema.MetaData.params.naming_convention
  27. naming_convention = {
  28. "ix": "idx_%(column_0_N_label)s",
  29. "uq": "%(table_name)s_%(column_0_N_name)s_uq",
  30. "ck": "ck_%(table_name)s_%(constraint_name)s",
  31. "fk": "%(table_name)s_%(column_0_name)s_fkey",
  32. "pk": "%(table_name)s_pkey",
  33. }
  34. def _get_schema():
  35. if not SQL_ALCHEMY_SCHEMA or SQL_ALCHEMY_SCHEMA.isspace():
  36. return None
  37. return SQL_ALCHEMY_SCHEMA
  38. metadata = MetaData(schema=_get_schema(), naming_convention=naming_convention)
  39. mapper_registry = registry(metadata=metadata)
  40. _sentinel = object()
  41. if TYPE_CHECKING:
  42. Base = Any
  43. else:
  44. Base = mapper_registry.generate_base()
  45. ID_LEN = 250
  46. def get_id_collation_args():
  47. """Get SQLAlchemy args to use for COLLATION."""
  48. collation = conf.get("database", "sql_engine_collation_for_ids", fallback=None)
  49. if collation:
  50. return {"collation": collation}
  51. else:
  52. # Automatically use utf8mb3_bin collation for mysql
  53. # This is backwards-compatible. All our IDS are ASCII anyway so even if
  54. # we migrate from previously installed database with different collation and we end up mixture of
  55. # COLLATIONS, it's not a problem whatsoever (and we keep it small enough so that our indexes
  56. # for MYSQL will not exceed the maximum index size.
  57. #
  58. # See https://github.com/apache/airflow/pull/17603#issuecomment-901121618.
  59. #
  60. # We cannot use session/dialect as at this point we are trying to determine the right connection
  61. # parameters, so we use the connection
  62. conn = conf.get("database", "sql_alchemy_conn", fallback="")
  63. if conn.startswith(("mysql", "mariadb")):
  64. return {"collation": "utf8mb3_bin"}
  65. return {}
  66. COLLATION_ARGS: dict[str, Any] = get_id_collation_args()
  67. def StringID(*, length=ID_LEN, **kwargs) -> String:
  68. return String(length=length, **kwargs, **COLLATION_ARGS)
  69. class TaskInstanceDependencies(Base):
  70. """Base class for depending models linked to TaskInstance."""
  71. __abstract__ = True
  72. task_id = Column(StringID(), nullable=False)
  73. dag_id = Column(StringID(), nullable=False)
  74. run_id = Column(StringID(), nullable=False)
  75. map_index = Column(Integer, nullable=False, server_default=text("-1"))