|
| 1 | +"""Tests for removing legacy org-scoped builtin registry repositories.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | +import subprocess |
| 7 | +import uuid |
| 8 | +from collections.abc import Iterator |
| 9 | + |
| 10 | +import pytest |
| 11 | +from sqlalchemy import create_engine, text |
| 12 | +from sqlalchemy.pool import NullPool |
| 13 | + |
| 14 | +from tests.database import TEST_DB_CONFIG |
| 15 | + |
| 16 | +MIGRATION_REVISION = "0a1e3100a432" |
| 17 | +PREVIOUS_REVISION = "6171727be56a" |
| 18 | + |
| 19 | + |
| 20 | +def _run_alembic(db_url: str, *args: str) -> None: |
| 21 | + env = os.environ.copy() |
| 22 | + env["TRACECAT__DB_URI"] = db_url |
| 23 | + result = subprocess.run( |
| 24 | + ["uv", "run", "alembic", *args], |
| 25 | + env=env, |
| 26 | + capture_output=True, |
| 27 | + text=True, |
| 28 | + check=False, |
| 29 | + ) |
| 30 | + if result.returncode != 0: |
| 31 | + raise RuntimeError( |
| 32 | + "Alembic command failed:\n" |
| 33 | + f"stdout:\n{result.stdout}\n" |
| 34 | + f"stderr:\n{result.stderr}" |
| 35 | + ) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="function") |
| 39 | +def migration_db_url() -> Iterator[str]: |
| 40 | + default_engine = create_engine( |
| 41 | + TEST_DB_CONFIG.sys_url_sync, |
| 42 | + isolation_level="AUTOCOMMIT", |
| 43 | + poolclass=NullPool, |
| 44 | + ) |
| 45 | + db_name = f"test_registry_cleanup_{uuid.uuid4().hex[:8]}" |
| 46 | + termination_query = text( |
| 47 | + f""" |
| 48 | + SELECT pg_terminate_backend(pg_stat_activity.pid) |
| 49 | + FROM pg_stat_activity |
| 50 | + WHERE pg_stat_activity.datname = '{db_name}' |
| 51 | + AND pid <> pg_backend_pid(); |
| 52 | + """ |
| 53 | + ) |
| 54 | + |
| 55 | + try: |
| 56 | + with default_engine.connect() as conn: |
| 57 | + conn.execute(termination_query) |
| 58 | + conn.execute(text(f'CREATE DATABASE "{db_name}"')) |
| 59 | + |
| 60 | + db_url = TEST_DB_CONFIG.test_url_sync.replace( |
| 61 | + TEST_DB_CONFIG.test_db_name, db_name |
| 62 | + ) |
| 63 | + _run_alembic(db_url, "upgrade", PREVIOUS_REVISION) |
| 64 | + yield db_url |
| 65 | + finally: |
| 66 | + with default_engine.connect() as conn: |
| 67 | + conn.execute(termination_query) |
| 68 | + conn.execute(text(f'DROP DATABASE IF EXISTS "{db_name}"')) |
| 69 | + default_engine.dispose() |
| 70 | + |
| 71 | + |
| 72 | +def _seed_legacy_registry_rows(db_url: str) -> tuple[uuid.UUID, uuid.UUID]: |
| 73 | + engine = create_engine(db_url, poolclass=NullPool) |
| 74 | + organization_id = uuid.uuid4() |
| 75 | + legacy_repo_id = uuid.uuid4() |
| 76 | + legacy_version_id = uuid.uuid4() |
| 77 | + platform_repo_id = uuid.uuid4() |
| 78 | + platform_version_id = uuid.uuid4() |
| 79 | + |
| 80 | + try: |
| 81 | + with engine.begin() as conn: |
| 82 | + conn.execute( |
| 83 | + text( |
| 84 | + """ |
| 85 | + INSERT INTO organization (id, name, slug, is_active) |
| 86 | + VALUES (:id, 'Test org', :slug, true) |
| 87 | + """ |
| 88 | + ), |
| 89 | + {"id": organization_id, "slug": f"test-org-{organization_id.hex[:8]}"}, |
| 90 | + ) |
| 91 | + conn.execute( |
| 92 | + text( |
| 93 | + """ |
| 94 | + INSERT INTO registry_repository (id, organization_id, origin, current_version_id) |
| 95 | + VALUES (:id, :organization_id, 'tracecat_registry', NULL) |
| 96 | + """ |
| 97 | + ), |
| 98 | + { |
| 99 | + "id": legacy_repo_id, |
| 100 | + "organization_id": organization_id, |
| 101 | + }, |
| 102 | + ) |
| 103 | + conn.execute( |
| 104 | + text( |
| 105 | + """ |
| 106 | + INSERT INTO registry_version ( |
| 107 | + id, |
| 108 | + organization_id, |
| 109 | + repository_id, |
| 110 | + version, |
| 111 | + manifest, |
| 112 | + tarball_uri |
| 113 | + ) |
| 114 | + VALUES ( |
| 115 | + :id, |
| 116 | + :organization_id, |
| 117 | + :repository_id, |
| 118 | + '1.0.0', |
| 119 | + CAST('{}' AS jsonb), |
| 120 | + 's3://test-bucket/legacy.tar.gz' |
| 121 | + ) |
| 122 | + """ |
| 123 | + ), |
| 124 | + { |
| 125 | + "id": legacy_version_id, |
| 126 | + "organization_id": organization_id, |
| 127 | + "repository_id": legacy_repo_id, |
| 128 | + }, |
| 129 | + ) |
| 130 | + conn.execute( |
| 131 | + text( |
| 132 | + """ |
| 133 | + UPDATE registry_repository |
| 134 | + SET current_version_id = :version_id |
| 135 | + WHERE id = :repository_id |
| 136 | + """ |
| 137 | + ), |
| 138 | + { |
| 139 | + "version_id": legacy_version_id, |
| 140 | + "repository_id": legacy_repo_id, |
| 141 | + }, |
| 142 | + ) |
| 143 | + conn.execute( |
| 144 | + text( |
| 145 | + """ |
| 146 | + INSERT INTO platform_registry_repository (id, origin, current_version_id) |
| 147 | + VALUES (:id, 'tracecat_registry', NULL) |
| 148 | + """ |
| 149 | + ), |
| 150 | + {"id": platform_repo_id}, |
| 151 | + ) |
| 152 | + conn.execute( |
| 153 | + text( |
| 154 | + """ |
| 155 | + INSERT INTO platform_registry_version ( |
| 156 | + id, |
| 157 | + repository_id, |
| 158 | + version, |
| 159 | + manifest, |
| 160 | + tarball_uri |
| 161 | + ) |
| 162 | + VALUES ( |
| 163 | + :id, |
| 164 | + :repository_id, |
| 165 | + '1.0.0', |
| 166 | + CAST('{}' AS jsonb), |
| 167 | + 's3://test-bucket/platform.tar.gz' |
| 168 | + ) |
| 169 | + """ |
| 170 | + ), |
| 171 | + { |
| 172 | + "id": platform_version_id, |
| 173 | + "repository_id": platform_repo_id, |
| 174 | + }, |
| 175 | + ) |
| 176 | + conn.execute( |
| 177 | + text( |
| 178 | + """ |
| 179 | + UPDATE platform_registry_repository |
| 180 | + SET current_version_id = :version_id |
| 181 | + WHERE id = :repository_id |
| 182 | + """ |
| 183 | + ), |
| 184 | + { |
| 185 | + "version_id": platform_version_id, |
| 186 | + "repository_id": platform_repo_id, |
| 187 | + }, |
| 188 | + ) |
| 189 | + finally: |
| 190 | + engine.dispose() |
| 191 | + |
| 192 | + return legacy_repo_id, platform_repo_id |
| 193 | + |
| 194 | + |
| 195 | +def test_upgrade_removes_legacy_org_scoped_builtin_registry_repository( |
| 196 | + migration_db_url: str, |
| 197 | +) -> None: |
| 198 | + db_url = migration_db_url |
| 199 | + legacy_repo_id, platform_repo_id = _seed_legacy_registry_rows(db_url) |
| 200 | + _run_alembic(db_url, "upgrade", MIGRATION_REVISION) |
| 201 | + |
| 202 | + engine = create_engine(db_url, poolclass=NullPool) |
| 203 | + try: |
| 204 | + with engine.begin() as conn: |
| 205 | + legacy_repo_count = conn.execute( |
| 206 | + text( |
| 207 | + """ |
| 208 | + SELECT COUNT(*) |
| 209 | + FROM registry_repository |
| 210 | + WHERE id = :repository_id |
| 211 | + """ |
| 212 | + ), |
| 213 | + {"repository_id": legacy_repo_id}, |
| 214 | + ).scalar_one() |
| 215 | + assert legacy_repo_count == 0 |
| 216 | + |
| 217 | + legacy_version_count = conn.execute( |
| 218 | + text( |
| 219 | + """ |
| 220 | + SELECT COUNT(*) |
| 221 | + FROM registry_version |
| 222 | + WHERE repository_id = :repository_id |
| 223 | + """ |
| 224 | + ), |
| 225 | + {"repository_id": legacy_repo_id}, |
| 226 | + ).scalar_one() |
| 227 | + assert legacy_version_count == 0 |
| 228 | + |
| 229 | + platform_repo_count = conn.execute( |
| 230 | + text( |
| 231 | + """ |
| 232 | + SELECT COUNT(*) |
| 233 | + FROM platform_registry_repository |
| 234 | + WHERE id = :repository_id |
| 235 | + """ |
| 236 | + ), |
| 237 | + {"repository_id": platform_repo_id}, |
| 238 | + ).scalar_one() |
| 239 | + assert platform_repo_count == 1 |
| 240 | + finally: |
| 241 | + engine.dispose() |
0 commit comments