diff --git a/galaxy_ng/app/migrations/0060_remove_stale_pulp_rbac_indexes.py b/galaxy_ng/app/migrations/0060_remove_stale_pulp_rbac_indexes.py new file mode 100644 index 0000000000..d482896269 --- /dev/null +++ b/galaxy_ng/app/migrations/0060_remove_stale_pulp_rbac_indexes.py @@ -0,0 +1,43 @@ +from django.db import migrations + + +STALE_INDEXES = [ + "core_userrole_content_type_id_a2ff8402", + "core_userrole_domain_id_f78b1c11", + "core_userrole_role_id_8272b20d", + "core_userrole_user_id_aca63c51", + "core_userro_content_5c0477_idx", + "core_grouprole_content_type_id_a80c1cfc", + "core_grouprole_domain_id_9644db4b", + "core_grouprole_group_id_09264d71", + "core_grouprole_role_id_3c2c3564", + "core_groupr_content_ea7d37_idx", +] + +drop_sql = "\n".join(f"DROP INDEX IF EXISTS {idx};" for idx in STALE_INDEXES) + +create_sql = """ +CREATE INDEX IF NOT EXISTS core_userrole_content_type_id_a2ff8402 ON core_userrole (content_type_id); +CREATE INDEX IF NOT EXISTS core_userrole_domain_id_f78b1c11 ON core_userrole (domain_id); +CREATE INDEX IF NOT EXISTS core_userrole_role_id_8272b20d ON core_userrole (role_id); +CREATE INDEX IF NOT EXISTS core_userrole_user_id_aca63c51 ON core_userrole (user_id); +CREATE INDEX IF NOT EXISTS core_userro_content_5c0477_idx ON core_userrole (content_type_id, object_id); +CREATE INDEX IF NOT EXISTS core_grouprole_content_type_id_a80c1cfc ON core_grouprole (content_type_id); +CREATE INDEX IF NOT EXISTS core_grouprole_domain_id_9644db4b ON core_grouprole (domain_id); +CREATE INDEX IF NOT EXISTS core_grouprole_group_id_09264d71 ON core_grouprole (group_id); +CREATE INDEX IF NOT EXISTS core_grouprole_role_id_3c2c3564 ON core_grouprole (role_id); +CREATE INDEX IF NOT EXISTS core_groupr_content_ea7d37_idx ON core_grouprole (content_type_id, object_id); +""" + + +class Migration(migrations.Migration): + dependencies = [ + ('galaxy', '0059_delete_system_auditor_role_definition'), + ] + + operations = [ + migrations.RunSQL( + sql=drop_sql, + reverse_sql=create_sql, + ), + ] diff --git a/galaxy_ng/tests/unit/migrations/test_0060_remove_stale_pulp_rbac_indexes.py b/galaxy_ng/tests/unit/migrations/test_0060_remove_stale_pulp_rbac_indexes.py new file mode 100644 index 0000000000..6cdd3d3c51 --- /dev/null +++ b/galaxy_ng/tests/unit/migrations/test_0060_remove_stale_pulp_rbac_indexes.py @@ -0,0 +1,58 @@ +from importlib import import_module + +from django.db import migrations +from django.test import TestCase + + +class TestRemoveStalePulpRbacIndexes(TestCase): + """Test migration 0060_remove_stale_pulp_rbac_indexes.""" + + def setUp(self): + self.migration_module = import_module( + 'galaxy_ng.app.migrations.0060_remove_stale_pulp_rbac_indexes' + ) + + def test_migration_dependencies(self): + Migration = self.migration_module.Migration + assert hasattr(Migration, 'dependencies') + assert ("galaxy", "0059_delete_system_auditor_role_definition") in Migration.dependencies + + def test_migration_operations(self): + Migration = self.migration_module.Migration + assert hasattr(Migration, 'operations') + assert len(Migration.operations) == 1 + operation = Migration.operations[0] + assert isinstance(operation, migrations.RunSQL) + + def test_migration_is_reversible(self): + Migration = self.migration_module.Migration + operation = Migration.operations[0] + assert operation.reverse_sql is not None + assert operation.reverse_sql != migrations.RunSQL.noop + + def test_drop_sql_targets_correct_indexes(self): + expected_indexes = [ + "core_userrole_content_type_id_a2ff8402", + "core_userrole_domain_id_f78b1c11", + "core_userrole_role_id_8272b20d", + "core_userrole_user_id_aca63c51", + "core_userro_content_5c0477_idx", + "core_grouprole_content_type_id_a80c1cfc", + "core_grouprole_domain_id_9644db4b", + "core_grouprole_group_id_09264d71", + "core_grouprole_role_id_3c2c3564", + "core_groupr_content_ea7d37_idx", + ] + assert self.migration_module.STALE_INDEXES == expected_indexes + + def test_drop_sql_uses_if_exists(self): + for idx in self.migration_module.STALE_INDEXES: + assert f"DROP INDEX IF EXISTS {idx};" in self.migration_module.drop_sql + + def test_reverse_sql_uses_if_not_exists(self): + for idx in self.migration_module.STALE_INDEXES: + assert f"CREATE INDEX IF NOT EXISTS {idx}" in self.migration_module.create_sql + + def test_only_targets_userrole_and_grouprole(self): + for idx in self.migration_module.STALE_INDEXES: + assert "userrole" in idx or "grouprole" in idx or "userro" in idx or "groupr" in idx