Skip to content

Commit e996ff6

Browse files
committed
fix: moved dependency to depends_on
* we have created reverse dependency from invenio_accounts, so this one should be as dependency and not down revision as well. * added missing alembic migration * added alembic tests
1 parent f8032a4 commit e996ff6

3 files changed

Lines changed: 107 additions & 3 deletions

File tree

invenio_communities/alembic/02cd82910727_update_role_id_type_upgrade.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
# revision identifiers, used by Alembic.
2121
revision = "02cd82910727"
2222
down_revision = (
23-
"f9843093f686",
23+
# our own 37b21951084c_update_role_id_type_downgrade.py
2424
"37b21951084c",
25-
) # Depends on invenio-access revision id (f9843093f686)
25+
)
2626
branch_labels = ()
27-
depends_on = None
27+
depends_on = [
28+
# invenio_accounts/alembic/f2522cdd5fcd_change_accountsrole_primary_key_to_string.py
29+
"f2522cdd5fcd",
30+
]
2831

2932

3033
def upgrade():
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#
2+
# This file is part of Invenio.
3+
# Copyright (C) 2016-2018 CERN.
4+
# Copyright (C) 2026 CESNET z.s.p.o.
5+
#
6+
# Invenio is free software; you can redistribute it and/or modify it
7+
# under the terms of the MIT License; see LICENSE file for more details.
8+
9+
"""Removed index by file id, added indices by community and community + file key."""
10+
11+
from alembic import op
12+
13+
# revision identifiers, used by Alembic.
14+
revision = "1777209602"
15+
down_revision = "dfbc96c5211f"
16+
branch_labels = ()
17+
depends_on = None
18+
19+
20+
def upgrade():
21+
"""Upgrade database."""
22+
# ### commands auto generated by Alembic - please adjust! ###
23+
op.drop_index(op.f("uidx_communities_files_id_key"), table_name="communities_files")
24+
op.create_index(
25+
op.f("ix_communities_files_record_id"),
26+
"communities_files",
27+
["record_id"],
28+
unique=False,
29+
)
30+
op.create_index(
31+
"uidx_communities_files_record_id_key",
32+
"communities_files",
33+
["record_id", "key"],
34+
unique=True,
35+
)
36+
# ### end Alembic commands ###
37+
38+
39+
def downgrade():
40+
"""Downgrade database."""
41+
# ### commands auto generated by Alembic - please adjust! ###
42+
op.drop_index(
43+
"uidx_communities_files_record_id_key", table_name="communities_files"
44+
)
45+
op.drop_index(
46+
op.f("ix_communities_files_record_id"), table_name="communities_files"
47+
)
48+
op.create_index(
49+
op.f("uidx_communities_files_id_key"),
50+
"communities_files",
51+
["id", "key"],
52+
unique=True,
53+
)
54+
# ### end Alembic commands ###

tests/test_alembic.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# -*- coding: utf-8 -*-
2+
#
3+
# Copyright (C) 2023 CERN.
4+
# Copyright (C) 2024 Graz University of Technology.
5+
# Copyright (C) 2026 CESNET z.s.p.o.
6+
#
7+
# Invenio-communities is free software; you can redistribute it and/or modify
8+
# it under the terms of the MIT License; see LICENSE file for more details.
9+
"""Test invenio-communities alembic."""
10+
11+
import pytest
12+
from invenio_db.utils import alembic_test_context, drop_alembic_version_table
13+
14+
15+
def test_alembic(base_app, database, extra_entry_points):
16+
"""Test alembic recipes."""
17+
db = database
18+
ext = base_app.extensions["invenio-db"]
19+
20+
if db.engine.name == "sqlite":
21+
raise pytest.skip("Upgrades are not supported on SQLite.")
22+
23+
base_app.config["ALEMBIC_CONTEXT"] = alembic_test_context()
24+
25+
# Check that this package's SQLAlchemy models have been properly registered
26+
tables = [x for x in db.metadata.tables]
27+
assert "communities_metadata" in tables
28+
assert "communities_members" in tables
29+
30+
# Check that Alembic agrees that there's no further tables to create.
31+
assert len(ext.alembic.compare_metadata()) == 0
32+
33+
# Drop everything and recreate tables all with Alembic
34+
db.drop_all()
35+
drop_alembic_version_table()
36+
ext.alembic.upgrade()
37+
38+
# expected non-covered migrations (from previous tests that use mock_module):
39+
# ('add_table', Table('mock_metadata'
40+
# ('add_table', Table('mock_community'
41+
for m in ext.alembic.compare_metadata():
42+
if not (
43+
m[0] == "add_table" and m[1].name in ["mock_metadata", "mock_community"]
44+
):
45+
raise RuntimeError(f"Unexpected migration: {m}")
46+
47+
drop_alembic_version_table()

0 commit comments

Comments
 (0)