|
19 | 19 | import copy |
20 | 20 | from collections.abc import Generator |
21 | 21 | from datetime import datetime, timezone |
| 22 | +from typing import Any |
22 | 23 | from unittest.mock import patch |
23 | 24 |
|
24 | 25 | import pytest |
25 | 26 | import yaml |
26 | 27 | from flask_appbuilder.security.sqla.models import Role, User |
27 | 28 | from pytest_mock import MockerFixture |
| 29 | +from sqlalchemy.exc import SQLAlchemyError |
28 | 30 | from sqlalchemy.orm.session import Session |
29 | 31 |
|
30 | 32 | from superset import security_manager |
@@ -506,3 +508,71 @@ def test_import_tag_logic_for_charts(session_with_schema: Session): |
506 | 508 | .all() |
507 | 509 | ) |
508 | 510 | assert len(associated_tags) == 0 |
| 511 | + |
| 512 | + |
| 513 | +def test_import_tag_savepoint_keeps_session_usable( |
| 514 | + mocker: MockerFixture, session_with_schema: Session |
| 515 | +) -> None: |
| 516 | + """ |
| 517 | + When a single tag operation fails with a SQLAlchemyError (e.g. a unique |
| 518 | + constraint violation from a concurrent import), the per-tag SAVEPOINT |
| 519 | + isolates the failure so the session is not left in a pending-rollback |
| 520 | + state, and the remaining tags still import successfully. |
| 521 | + """ |
| 522 | + contents = { |
| 523 | + "tags.yaml": yaml.dump( |
| 524 | + { |
| 525 | + "tags": [ |
| 526 | + {"tag_name": "tag_1", "description": "Description for tag_1"}, |
| 527 | + {"tag_name": "tag_2", "description": "Description for tag_2"}, |
| 528 | + ] |
| 529 | + } |
| 530 | + ) |
| 531 | + } |
| 532 | + |
| 533 | + object_id = 1 |
| 534 | + object_type = "chart" |
| 535 | + |
| 536 | + # Simulate a unique-constraint violation discovered when the first |
| 537 | + # TaggedObject's SAVEPOINT is flushed (e.g. a concurrent import already |
| 538 | + # created the same association) -- not synchronously from Session.add(). |
| 539 | + # `import_tag`'s own pre-insert existence check would normally catch a |
| 540 | + # real duplicate row, so the failure is injected at the point SQLAlchemy |
| 541 | + # actually persists the pending row: `begin_nested()`'s implicit flush |
| 542 | + # on a successful `with` exit, which calls `Session.flush()` directly |
| 543 | + # (see `SessionTransaction._prepare_impl`). |
| 544 | + pending_tagged_objects: list[TaggedObject] = [] |
| 545 | + original_add = session_with_schema.add |
| 546 | + |
| 547 | + def tracking_add(obj: object) -> None: |
| 548 | + if isinstance(obj, TaggedObject): |
| 549 | + pending_tagged_objects.append(obj) |
| 550 | + original_add(obj) |
| 551 | + |
| 552 | + original_flush = session_with_schema.flush |
| 553 | + |
| 554 | + def flaky_flush(*args: Any, **kwargs: Any) -> None: |
| 555 | + # Only the first TaggedObject ever added should fail, and only while |
| 556 | + # it's still pending -- once its SAVEPOINT rolls back, SQLAlchemy |
| 557 | + # expunges it from the session, so this does not also fail tag_2's |
| 558 | + # flush. |
| 559 | + if ( |
| 560 | + pending_tagged_objects |
| 561 | + and pending_tagged_objects[0] is not None |
| 562 | + and pending_tagged_objects[0] in session_with_schema.new |
| 563 | + ): |
| 564 | + raise SQLAlchemyError("UNIQUE constraint failed: tagged_object") |
| 565 | + original_flush(*args, **kwargs) |
| 566 | + |
| 567 | + mocker.patch.object(session_with_schema, "add", side_effect=tracking_add) |
| 568 | + mocker.patch.object(session_with_schema, "flush", side_effect=flaky_flush) |
| 569 | + |
| 570 | + with patch.object(feature_flag_manager, "is_feature_enabled", return_value=True): |
| 571 | + new_tag_ids = import_tag( |
| 572 | + ["tag_1", "tag_2"], contents, object_id, object_type, session_with_schema |
| 573 | + ) |
| 574 | + |
| 575 | + # tag_1 failed on the unique violation, but tag_2 succeeded. |
| 576 | + assert len(new_tag_ids) == 1 |
| 577 | + # The session is still usable — no PendingRollbackError. |
| 578 | + assert session_with_schema.query(TaggedObject).count() == 1 |
0 commit comments