|
20 | 20 | from typing import cast |
21 | 21 |
|
22 | 22 | import pytest |
23 | | -from sqlalchemy import Engine, create_engine, inspect, text |
| 23 | +from sqlalchemy import Engine, create_engine, event, inspect, text |
24 | 24 | from sqlalchemy.exc import ArgumentError |
25 | 25 |
|
26 | 26 | from pyiceberg.catalog import load_catalog |
|
32 | 32 | SqlCatalogBaseTable, |
33 | 33 | ) |
34 | 34 | from pyiceberg.exceptions import ( |
| 35 | + NamespaceAlreadyExistsError, |
35 | 36 | NoSuchPropertyException, |
36 | 37 | NoSuchTableError, |
37 | 38 | TableAlreadyExistsError, |
@@ -559,3 +560,33 @@ def test_create_and_load_table_on_v0_schema(warehouse: Path) -> None: |
559 | 560 |
|
560 | 561 | # iceberg_type column must still be absent from the DB (no migration occurred) |
561 | 562 | assert "iceberg_type" not in get_columns(catalog.engine) |
| 563 | + |
| 564 | + |
| 565 | +def test_create_namespace_concurrent_integrity_error(warehouse: Path) -> None: |
| 566 | + """Concurrent create_namespace requests should raise NamespaceAlreadyExistsError.""" |
| 567 | + uri = f"sqlite:///{warehouse.as_posix()}/test-concurrent-create-namespace" |
| 568 | + warehouse_uri = f"file://{warehouse}" |
| 569 | + catalog_1 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri) |
| 570 | + catalog_2 = SqlCatalog("test", uri=uri, warehouse=warehouse_uri) |
| 571 | + try: |
| 572 | + injected = False |
| 573 | + |
| 574 | + @event.listens_for(catalog_1.engine, "before_cursor_execute") |
| 575 | + def inject_concurrent_insert( |
| 576 | + _conn: object, |
| 577 | + _cursor: object, |
| 578 | + statement: str, |
| 579 | + _parameters: object, |
| 580 | + _context: object, |
| 581 | + _executemany: bool, |
| 582 | + ) -> None: |
| 583 | + nonlocal injected |
| 584 | + if not injected and "INSERT INTO iceberg_namespace_properties" in statement: |
| 585 | + injected = True |
| 586 | + catalog_2.create_namespace("concurrent_ns") |
| 587 | + |
| 588 | + with pytest.raises(NamespaceAlreadyExistsError): |
| 589 | + catalog_1.create_namespace("concurrent_ns") |
| 590 | + finally: |
| 591 | + catalog_1.close() |
| 592 | + catalog_2.close() |
0 commit comments