# Buggy code
def upgrade():  # noqa: D103
    # We previously had a KnownEvent's table, but we deleted the table without
    # a down migration to remove it (so we didn't delete anyone's data if they
    # were happing to use the feature.
    #
    # But before we can delete the users table we need to drop the FK

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

    if "known_event" in tables:
        for fkey in inspector.get_foreign_keys(
            table_name="known_event", referred_table="users"
        ):
            op.drop_constraint(fkey["name"], "known_event", type_="foreignkey")

    if "chart" in tables:
        op.drop_table(
            "chart",
        )

    if "users" in tables:
        op.drop_table("users")


# Traceback (NotImplementedError)
[2021-01-24 08:38:42,015] {db.py:678} INFO - Creating tables
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade 03afc6b6f902 -> cf5dc11e79ad, drop_user_and_chart
Traceback (most recent call last):
File "/Users/vijayantsoni/.virtualenvs/airflow/bin/airflow", line 11, in <module>
sys.exit(main())
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/__main__.py", line 40, in main
args.func(args)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/cli/cli_parser.py", line 48, in command
return func(*args, **kwargs)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/cli/commands/db_command.py", line 31, in initdb
db.initdb()
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/utils/db.py", line 549, in initdb
upgradedb()
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/utils/db.py", line 688, in upgradedb
command.upgrade(config, 'heads')
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/command.py", line 294, in upgrade
script.run_env()
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/script/base.py", line 481, in run_env
util.load_python_file(self.dir, "env.py")
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 97, in load_python_file
module = load_module_py(module_id, path)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/util/compat.py", line 182, in load_module_py
spec.loader.exec_module(module)
File "<frozen importlib._bootstrap_external>", line 783, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/migrations/env.py", line 108, in <module>
run_migrations_online()
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/migrations/env.py", line 102, in run_migrations_online
context.run_migrations()
File "<string>", line 8, in run_migrations
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/runtime/environment.py", line 813, in run_migrations
self.get_context().run_migrations(**kw)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/runtime/migration.py", line 560, in run_migrations
step.migration_fn(**kw)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/airflow/migrations/versions/cf5dc11e79ad_drop_user_and_chart.py", line 49, in upgrade
op.drop_constraint('known_event_user_id_fkey', 'known_event')
File "<string>", line 8, in drop_constraint
File "<string>", line 3, in drop_constraint
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/operations/ops.py", line 148, in drop_constraint
return operations.invoke(op)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/operations/base.py", line 354, in invoke
return fn(self, operation)
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/operations/toimpl.py", line 160, in drop_constraint
operations.impl.drop_constraint(
File "/Users/vijayantsoni/.virtualenvs/airflow/lib/python3.8/site-packages/alembic/ddl/sqlite.py", line 52, in drop_constraint
raise NotImplementedError(
NotImplementedError: No support for ALTER of constraints in SQLite dialectPlease refer to the batch mode feature which allows for SQLite migrations using a copy-and-move strategy.

# Fixed code
def upgrade():  # noqa: D103
    # We previously had a KnownEvent's table, but we deleted the table without
    # a down migration to remove it (so we didn't delete anyone's data if they
    # were happing to use the feature.
    #
    # But before we can delete the users table we need to drop the FK

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

    if "known_event" in tables:
        for fkey in inspector.get_foreign_keys(
            table_name="known_event", referred_table="users"
        ):
            if fkey["name"]:
                with op.batch_alter_table(table_name="known_event") as bop:
                    bop.drop_constraint(fkey["name"], type_="foreignkey")

    if "chart" in tables:
        op.drop_table(
            "chart",
        )

    if "users" in tables:
        op.drop_table("users")