Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add compat shim for SQLAlchemy to avoid warnings #21959

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions airflow/compat/sqlalchemy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from sqlalchemy import Table
from sqlalchemy.engine import Connection

try:
from sqlalchemy import inspect
except AttributeError:
from sqlalchemy.engine.reflection import Inspector

inspect = Inspector.from_engine

__all__ = ["has_table", "inspect"]


def has_table(conn: Connection, table: Table):
try:
return inspect(conn).has_table(table)
except AttributeError:
return table.exists(conn)
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect
from airflow.migrations.db_types import StringID

# revision identifiers, used by Alembic.
Expand All @@ -41,7 +41,7 @@
def upgrade():
"""Apply Increase length of ``Flask-AppBuilder`` ``ab_view_menu.name`` column"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()

if "ab_view_menu" in tables:
Expand Down Expand Up @@ -72,7 +72,7 @@ def upgrade():
def downgrade():
"""Unapply Increase length of ``Flask-AppBuilder`` ``ab_view_menu.name`` column"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()
if "ab_view_menu" in tables:
if conn.dialect.name == "sqlite":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = '1507a7289a2f'
Expand All @@ -44,7 +45,7 @@ def upgrade():
# true for users who are upgrading from a previous version of Airflow
# that predates Alembic integration
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)

# this will only be true if 'connection' already exists in the db,
# but not if alembic created it in a previous migration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"""
import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = '33ae817a1ff4'
Expand All @@ -39,7 +40,7 @@

def upgrade():
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)

if RESOURCE_TABLE not in inspector.get_table_names():
columns_and_constraints = [
Expand All @@ -63,7 +64,7 @@ def upgrade():

def downgrade():
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)

if RESOURCE_TABLE in inspector.get_table_names():
op.drop_table(RESOURCE_TABLE)
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
import sqlalchemy as sa
from alembic import op
from sqlalchemy import Column, Integer, String
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.ext.declarative import declarative_base

from airflow.compat.sqlalchemy import inspect
from airflow.utils.types import DagRunType

# revision identifiers, used by Alembic.
Expand Down Expand Up @@ -58,7 +58,7 @@ def upgrade():
run_type_col_type = sa.String(length=50)

conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
dag_run_columns = [col.get('name') for col in inspector.get_columns("dag_run")]

if "run_type" not in dag_run_columns:
Expand Down
7 changes: 4 additions & 3 deletions airflow/migrations/versions/92c57b58940d_add_fab_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = '92c57b58940d'
Expand All @@ -39,7 +40,7 @@
def upgrade():
"""Create FAB Tables"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()
if "ab_permission" not in tables:
op.create_table(
Expand Down Expand Up @@ -153,7 +154,7 @@ def upgrade():
def downgrade():
"""Drop FAB Tables"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()
fab_tables = [
"ab_permission",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@

from alembic import op
from sqlalchemy import Column, Integer
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = 'bbf4a7ad0465'
Expand Down Expand Up @@ -97,7 +98,7 @@ def create_constraints(operator, column_name, constraint_dict):
def upgrade():
"""Apply Remove id column from xcom"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)

with op.batch_alter_table('xcom') as bop:
xcom_columns = [col.get('name') for col in inspector.get_columns("xcom")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

import sqlalchemy as sa
from alembic import op
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = 'bef4f3d11e8b'
Expand All @@ -43,7 +44,7 @@
def upgrade():
"""Apply Drop ``KubeResourceVersion`` and ``KubeWorkerId``entifier tables"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()

if WORKER_UUID_TABLE in tables:
Expand All @@ -55,7 +56,7 @@ def upgrade():
def downgrade():
"""Unapply Drop ``KubeResourceVersion`` and ``KubeWorkerId``entifier tables"""
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()

if WORKER_UUID_TABLE not in tables:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import sqlalchemy as sa
from alembic import op
from sqlalchemy import Column, Integer, String
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.ext.declarative import declarative_base

from airflow import settings
from airflow.compat.sqlalchemy import inspect
from airflow.models import DagBag

# revision identifiers, used by Alembic.
Expand Down Expand Up @@ -62,7 +62,7 @@ def upgrade():
# Checking task_instance table exists prevent the error of querying
# non-existing task_instance table.
connection = op.get_bind()
inspector = Inspector.from_engine(connection)
inspector = inspect(connection)
tables = inspector.get_table_names()

if 'task_instance' in tables:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import mysql
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = 'cf5dc11e79ad'
Expand All @@ -43,7 +44,7 @@ def upgrade():
# But before we can delete the users table we need to drop the FK

conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()

if 'known_event' in tables:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import sqlalchemy as sa
from alembic import op
from sqlalchemy import func
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect
from airflow.migrations.db_types import TIMESTAMP, StringID

# revision identifiers, used by Alembic.
Expand All @@ -41,7 +41,7 @@
def upgrade():

conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()
if 'sensor_instance' in tables:
return
Expand Down Expand Up @@ -74,7 +74,7 @@ def upgrade():

def downgrade():
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()
if 'sensor_instance' in tables:
op.drop_table('sensor_instance')
4 changes: 2 additions & 2 deletions airflow/migrations/versions/e3a246e0dc1_current_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import sqlalchemy as sa
from alembic import op
from sqlalchemy import func
from sqlalchemy.engine.reflection import Inspector

from airflow.compat.sqlalchemy import inspect
from airflow.migrations.db_types import StringID

# revision identifiers, used by Alembic.
Expand All @@ -41,7 +41,7 @@

def upgrade():
conn = op.get_bind()
inspector = Inspector.from_engine(conn)
inspector = inspect(conn)
tables = inspector.get_table_names()

if 'connection' not in tables:
Expand Down
3 changes: 2 additions & 1 deletion airflow/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from sqlalchemy.orm.session import Session

from airflow import settings
from airflow.compat.sqlalchemy import has_table
from airflow.configuration import conf
from airflow.exceptions import AirflowException
from airflow.jobs.base_job import BaseJob # noqa: F401
Expand Down Expand Up @@ -1265,7 +1266,7 @@ def drop_airflow_models(connection):

migration_ctx = MigrationContext.configure(connection)
version = migration_ctx._version
if version.exists(connection):
if has_table(connection, version):
version.drop(connection)


Expand Down
4 changes: 2 additions & 2 deletions airflow/www/fab_security/sqla/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
from flask_appbuilder.models.sqla import Base
from flask_appbuilder.models.sqla.interface import SQLAInterface
from sqlalchemy import and_, func, literal
from sqlalchemy.engine.reflection import Inspector
from sqlalchemy.orm.exc import MultipleResultsFound
from werkzeug.security import generate_password_hash

from airflow.compat import sqlalchemy as sqla_compat
from airflow.www.fab_security.manager import BaseSecurityManager
from airflow.www.fab_security.sqla.models import (
Action,
Expand Down Expand Up @@ -99,7 +99,7 @@ def register_views(self):
def create_db(self):
try:
engine = self.get_session.get_bind(mapper=None, clause=None)
inspector = Inspector.from_engine(engine)
inspector = sqla_compat.inspect(engine)
if "ab_user" not in inspector.get_table_names():
log.info(c.LOGMSG_INF_SEC_NO_DB)
Base.metadata.create_all(engine)
Expand Down
4 changes: 2 additions & 2 deletions tests/serialization/test_dag_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ def test_extra_serialized_field_and_operator_links(
assert serialized_dag["dag"]["tasks"][0]["_operator_extra_links"] == serialized_links

# Test all the extra_links are set
assert set(simple_task.extra_links) == set(links.keys()) | {'airflow', 'github', 'google'}
assert set(simple_task.extra_links) == {*links, 'airflow', 'github', 'google'}

dr = dag_maker.create_dagrun(execution_date=test_date)
(ti,) = dr.task_instances
Expand All @@ -897,7 +897,7 @@ def test_extra_serialized_field_and_operator_links(
value=bash_command,
task_id=simple_task.task_id,
dag_id=simple_task.dag_id,
execution_date=test_date,
run_id=dr.run_id,
)

# Test Deserialized inbuilt link
Expand Down