|
20 | 20 | from typing import cast |
21 | 21 |
|
22 | 22 | import pytest |
23 | | -from sqlalchemy import Engine, create_engine, inspect |
| 23 | +from sqlalchemy import Engine, create_engine, inspect, text |
24 | 24 | from sqlalchemy.exc import ArgumentError |
25 | 25 |
|
26 | 26 | from pyiceberg.catalog import load_catalog |
@@ -261,3 +261,83 @@ def test_sql_catalog_multiple_close_calls(self, catalog_sqlite: SqlCatalog) -> N |
261 | 261 |
|
262 | 262 | # Second close should not raise an exception |
263 | 263 | catalog_sqlite.close() |
| 264 | + |
| 265 | + |
| 266 | +def _create_pre_migration_schema_tables(engine: Engine) -> None: |
| 267 | + with engine.connect() as conn: |
| 268 | + conn.execute( |
| 269 | + text( |
| 270 | + "CREATE TABLE iceberg_tables (" |
| 271 | + " catalog_name VARCHAR(255) NOT NULL," |
| 272 | + " table_namespace VARCHAR(255) NOT NULL," |
| 273 | + " table_name VARCHAR(255) NOT NULL," |
| 274 | + " metadata_location VARCHAR(1000)," |
| 275 | + " previous_metadata_location VARCHAR(1000)," |
| 276 | + " PRIMARY KEY (catalog_name, table_namespace, table_name)" |
| 277 | + ")" |
| 278 | + ) |
| 279 | + ) |
| 280 | + conn.execute( |
| 281 | + text( |
| 282 | + "CREATE TABLE iceberg_namespace_properties (" |
| 283 | + " catalog_name VARCHAR(255) NOT NULL," |
| 284 | + " namespace VARCHAR(255) NOT NULL," |
| 285 | + " property_key VARCHAR(255) NOT NULL," |
| 286 | + " property_value VARCHAR(1000) NOT NULL," |
| 287 | + " PRIMARY KEY (catalog_name, namespace, property_key)" |
| 288 | + ")" |
| 289 | + ) |
| 290 | + ) |
| 291 | + conn.commit() |
| 292 | + |
| 293 | + |
| 294 | +def get_columns(engine: Engine) -> set[str]: |
| 295 | + return {c["name"] for c in inspect(engine).get_columns("iceberg_tables")} |
| 296 | + |
| 297 | + |
| 298 | +def test_adds_iceberg_type_column_to_old_schema(warehouse: Path) -> None: |
| 299 | + # Create the old schema tables |
| 300 | + uri = f"sqlite:////{warehouse}/test-migration-add-col" |
| 301 | + engine = create_engine(uri) |
| 302 | + with engine.connect() as conn: |
| 303 | + conn.execute( |
| 304 | + text( |
| 305 | + "CREATE TABLE iceberg_tables (" |
| 306 | + " catalog_name VARCHAR(255) NOT NULL," |
| 307 | + " table_namespace VARCHAR(255) NOT NULL," |
| 308 | + " table_name VARCHAR(255) NOT NULL," |
| 309 | + " metadata_location VARCHAR(1000)," |
| 310 | + " previous_metadata_location VARCHAR(1000)," |
| 311 | + " PRIMARY KEY (catalog_name, table_namespace, table_name)" |
| 312 | + ")" |
| 313 | + ) |
| 314 | + ) |
| 315 | + conn.execute( |
| 316 | + text( |
| 317 | + "CREATE TABLE iceberg_namespace_properties (" |
| 318 | + " catalog_name VARCHAR(255) NOT NULL," |
| 319 | + " namespace VARCHAR(255) NOT NULL," |
| 320 | + " property_key VARCHAR(255) NOT NULL," |
| 321 | + " property_value VARCHAR(1000) NOT NULL," |
| 322 | + " PRIMARY KEY (catalog_name, namespace, property_key)" |
| 323 | + ")" |
| 324 | + ) |
| 325 | + ) |
| 326 | + conn.commit() |
| 327 | + |
| 328 | + # Verify the column does not exist in the old schema |
| 329 | + assert "iceberg_type" not in get_columns(engine) |
| 330 | + |
| 331 | + # Load the catalog and verify the column exists |
| 332 | + catalog = SqlCatalog("test", uri=uri, warehouse=f"file://{warehouse}", init_catalog_tables="false") |
| 333 | + assert "iceberg_type" in get_columns(catalog.engine) |
| 334 | + |
| 335 | + |
| 336 | +def test_idempotent_when_column_already_exists(warehouse: Path) -> None: |
| 337 | + # Verify the column was created by the init_tables call |
| 338 | + catalog = SqlCatalog("test", uri="sqlite:///:memory:", warehouse=f"file://{warehouse}") |
| 339 | + assert "iceberg_type" in get_columns(catalog.engine) |
| 340 | + |
| 341 | + # Verify the method is idempotent by calling it again |
| 342 | + catalog._update_tables_if_required() |
| 343 | + assert "iceberg_type" in get_columns(catalog.engine) |
0 commit comments