Repository: alephdata/aleph
Issue: Migrations broken by SQLAlchemy version bump

When resetting my dev environment database and index today resulted in the following error:
```
{"logger": "alembic.runtime.migration", "timestamp": "2023-06-27 11:03:06.010008", "message": "Running upgrade 1519391870a0 -> b3959bf8cc66, Migrate entity IDs and simplify schema.", "severity": "INFO"}
Traceback (most recent call last):
  File "/usr/bin/aleph", line 33, in <module>
    sys.exit(load_entry_point('aleph', 'console_scripts', 'aleph')())
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1130, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1055, in main
    rv = self.invoke(ctx)
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1657, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/flask/cli.py", line 357, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/click/core.py", line 760, in invoke
    return __callback(*args, **kwargs)
  File "/aleph/aleph/manage.py", line 490, in upgrade
    upgrade_system()
  File "/aleph/aleph/migration.py", line 12, in upgrade_system
    flask_migrate.upgrade()
  File "/usr/local/lib/python3.8/dist-packages/flask_migrate/__init__.py", line 98, in wrapped
    f(*args, **kwargs)
  File "/usr/local/lib/python3.8/dist-packages/flask_migrate/__init__.py", line 185, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/usr/local/lib/python3.8/dist-packages/alembic/command.py", line 322, in upgrade
    script.run_env()
  File "/usr/local/lib/python3.8/dist-packages/alembic/script/base.py", line 569, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.8/dist-packages/alembic/util/pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python3.8/dist-packages/alembic/util/pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/aleph/aleph/migrate/env.py", line 49, in <module>
    run_migrations_online()
  File "/aleph/aleph/migrate/env.py", line 41, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.8/dist-packages/alembic/runtime/environment.py", line 853, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.8/dist-packages/alembic/runtime/migration.py", line 623, in run_migrations
    step.migration_fn(**kw)
  File "/aleph/aleph/migrate/versions/b3959bf8cc66_entity_ids.py", line 22, in upgrade
    meta.reflect()
TypeError: reflect() missing 1 required positional argument: 'bind'
make: *** [Makefile:51: upgrade] Error 1
```

This seems to be caused by the SQLAlchemy version bump

Changed files:
aleph/migrate/versions/4c9e198c5b31_entitysets.py
aleph/migrate/versions/aa486b9e627e_hard_deletes.py
aleph/migrate/versions/b3959bf8cc66_entity_ids.py
aleph/migrate/versions/9dcef7592cea_added_fields_to_entitysetitem_to_support_profiles.py

diff --git a/aleph/migrate/versions/4c9e198c5b31_entitysets.py b/aleph/migrate/versions/4c9e198c5b31_entitysets.py
index f06701482..8e88b6d3f 100644
--- a/aleph/migrate/versions/4c9e198c5b31_entitysets.py
+++ b/aleph/migrate/versions/4c9e198c5b31_entitysets.py
@@ -77,12 +77,12 @@ def upgrade():
     bind = op.get_bind()
     meta = sa.MetaData()
     meta.bind = bind
-    meta.reflect()
+    meta.reflect(bind=bind)
     diagram_table = meta.tables["diagram"]
     entityset_table = meta.tables["entityset"]
     item_table = meta.tables["entityset_item"]
 
-    q = sa.select([diagram_table])
+    q = sa.select(diagram_table)
     rp = bind.execute(q)
     while True:
         diagram = rp.fetchone()
diff --git a/aleph/migrate/versions/9dcef7592cea_added_fields_to_entitysetitem_to_support_profiles.py b/aleph/migrate/versions/9dcef7592cea_added_fields_to_entitysetitem_to_support_profiles.py
index 8e921e4c6..d7cab583f 100644
--- a/aleph/migrate/versions/9dcef7592cea_added_fields_to_entitysetitem_to_support_profiles.py
+++ b/aleph/migrate/versions/9dcef7592cea_added_fields_to_entitysetitem_to_support_profiles.py
@@ -34,7 +34,7 @@ def upgrade():
     bind = op.get_bind()
     meta = sa.MetaData()
     meta.bind = bind
-    meta.reflect()
+    meta.reflect(bind)
     linkage_table = meta.tables["linkage"]
     entityset_table = meta.tables["entityset"]
     item_table = meta.tables["entityset_item"]
@@ -43,7 +43,7 @@ def upgrade():
     q = q.values({"judgement": "POSITIVE"})
     bind.execute(q)
 
-    q = sa.select([linkage_table]).order_by("profile_id")
+    q = sa.select(linkage_table).order_by("profile_id")
     rp = bind.execute(q)
 
     profiles = groupby(
diff --git a/aleph/migrate/versions/aa486b9e627e_hard_deletes.py b/aleph/migrate/versions/aa486b9e627e_hard_deletes.py
index bb8557524..1253bd801 100644
--- a/aleph/migrate/versions/aa486b9e627e_hard_deletes.py
+++ b/aleph/migrate/versions/aa486b9e627e_hard_deletes.py
@@ -15,7 +15,7 @@ down_revision = "9dcef7592cea"
 def upgrade():
     meta = sa.MetaData()
     meta.bind = op.get_bind()
-    meta.reflect()
+    meta.reflect(bind=meta.bind)
     for table_name in ("alert", "entity", "mapping", "permission"):
         table = meta.tables[table_name]
         q = sa.delete(table).where(table.c.deleted_at != None)  # noqa
diff --git a/aleph/migrate/versions/b3959bf8cc66_entity_ids.py b/aleph/migrate/versions/b3959bf8cc66_entity_ids.py
index 157f90f7b..d07927125 100644
--- a/aleph/migrate/versions/b3959bf8cc66_entity_ids.py
+++ b/aleph/migrate/versions/b3959bf8cc66_entity_ids.py
@@ -19,14 +19,14 @@ def upgrade():
     bind = op.get_bind()
     meta = sa.MetaData()
     meta.bind = bind
-    meta.reflect()
+    meta.reflect(bind=bind)
     entity_table = meta.tables["entity"]
     collection_table = meta.tables["collection"]
-    q = sa.select([collection_table])
+    q = sa.select(collection_table)
     crp = bind.execute(q)
     for collection in crp.fetchall():
         ns = Namespace(collection.foreign_id)
-        q = sa.select([entity_table])
+        q = sa.select(entity_table)
         q = q.where(entity_table.c.collection_id == collection.id)
         erp = bind.execute(q)
         while True: