|
| 1 | +# SPDX-FileCopyrightText: 2024-present MTS PJSC |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import TYPE_CHECKING |
| 6 | + |
| 7 | +import pytest |
| 8 | +from sqlalchemy import create_engine, text |
| 9 | +from sqlalchemy.pool import NullPool |
| 10 | + |
| 11 | +from data_rentgen.db.models import Base |
| 12 | +from tests.test_database.fixtures.alembic import do_run_migrations |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from alembic.config import Config as AlembicConfig |
| 16 | + |
| 17 | +pytestmark = [pytest.mark.db] |
| 18 | + |
| 19 | +PREV_REVISION = "4a02d2d5c8b1" |
| 20 | +THIS_REVISION = "96fd9a096682" |
| 21 | + |
| 22 | + |
| 23 | +def test_migration_detach_spark_unknown_jobs(empty_db_url: str, alembic_config: AlembicConfig): |
| 24 | + do_run_migrations(alembic_config, Base.metadata, PREV_REVISION) |
| 25 | + |
| 26 | + engine = create_engine(empty_db_url, poolclass=NullPool) |
| 27 | + try: |
| 28 | + with engine.begin() as conn: |
| 29 | + conn.execute( |
| 30 | + text( |
| 31 | + """ |
| 32 | + INSERT INTO location (id, type, name) VALUES |
| 33 | + (1, 'local', 'somehost'); |
| 34 | + """, |
| 35 | + ), |
| 36 | + ) |
| 37 | + |
| 38 | + conn.execute( |
| 39 | + text( |
| 40 | + """ |
| 41 | + INSERT INTO job (id, location_id, name, type_id, parent_job_id) VALUES |
| 42 | + (1, 1, 'airflow_task1', 3, null), |
| 43 | + (2, 1, 'airflow_task2', 3, null), |
| 44 | + (3, 1, 'unknown', 1, 1), |
| 45 | + (4, 1, 'some_session', 1, 2); |
| 46 | + """, |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + conn.execute( |
| 51 | + text( |
| 52 | + """ |
| 53 | + INSERT INTO tag (id, name) VALUES |
| 54 | + (1, 'some_tag'), |
| 55 | + (2, 'another_tag'); |
| 56 | + """, |
| 57 | + ), |
| 58 | + ) |
| 59 | + |
| 60 | + conn.execute( |
| 61 | + text( |
| 62 | + """ |
| 63 | + INSERT INTO tag_value (id, tag_id, value) VALUES |
| 64 | + (1, 1, 'some_value'), |
| 65 | + (2, 2, 'another_value'); |
| 66 | + """, |
| 67 | + ), |
| 68 | + ) |
| 69 | + |
| 70 | + conn.execute( |
| 71 | + text( |
| 72 | + """ |
| 73 | + INSERT INTO job_tag_value (job_id, tag_value_id) VALUES |
| 74 | + (1, 1), |
| 75 | + (2, 1), |
| 76 | + (3, 2), |
| 77 | + (4, 2); |
| 78 | + """, |
| 79 | + ), |
| 80 | + ) |
| 81 | + |
| 82 | + conn.execute( |
| 83 | + text( |
| 84 | + """ |
| 85 | + INSERT INTO job_dependency (from_job_id, to_job_id) VALUES |
| 86 | + (1, 2), |
| 87 | + (3, 4); |
| 88 | + """, |
| 89 | + ), |
| 90 | + ) |
| 91 | + |
| 92 | + do_run_migrations(alembic_config, Base.metadata, THIS_REVISION) |
| 93 | + |
| 94 | + with engine.connect() as conn: |
| 95 | + assert conn.execute( |
| 96 | + text("SELECT id, parent_job_id FROM job ORDER BY id"), |
| 97 | + ).fetchall() == [ |
| 98 | + (1, None), |
| 99 | + (2, None), |
| 100 | + (3, None), # parent_id is null |
| 101 | + (4, 2), |
| 102 | + ] |
| 103 | + |
| 104 | + assert conn.execute( |
| 105 | + text("SELECT job_id, tag_value_id FROM job_tag_value ORDER BY job_id, tag_value_id"), |
| 106 | + ).fetchall() == [ |
| 107 | + (1, 1), |
| 108 | + (2, 1), |
| 109 | + # no item (3, 2) |
| 110 | + (4, 2), |
| 111 | + ] |
| 112 | + |
| 113 | + assert conn.execute( |
| 114 | + text("SELECT from_job_id, to_job_id FROM job_dependency ORDER BY from_job_id, to_job_id"), |
| 115 | + ).fetchall() == [ |
| 116 | + (1, 2), |
| 117 | + # no item (3, 4) |
| 118 | + ] |
| 119 | + |
| 120 | + finally: |
| 121 | + engine.dispose() |
0 commit comments