Skip to content

Commit

Permalink
add _ensure_tables_exists back until postgres integration tests compl…
Browse files Browse the repository at this point in the history
…eted
  • Loading branch information
isc-patrick committed Sep 16, 2024
1 parent 58f9e53 commit c045175
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pyiceberg/catalog/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
union,
update,
)
from sqlalchemy.exc import IntegrityError, NoResultFound
from sqlalchemy.exc import IntegrityError, NoResultFound, OperationalError, ProgrammingError
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
Expand Down Expand Up @@ -129,8 +129,20 @@ def __init__(self, name: str, **properties: str):
self.engine = create_engine(uri_prop, echo=echo, pool_pre_ping=pool_pre_ping)

if init_catalog_tables:
# Only creates tables that do not exist
self.create_tables()
self._ensure_tables_exist()

def _ensure_tables_exist(self) -> None:
with Session(self.engine) as session:
for table in [IcebergTables, IcebergNamespaceProperties]:
stmt = select(1).select_from(table)
try:
session.scalar(stmt)
except (
OperationalError,
ProgrammingError,
): # sqlalchemy returns OperationalError in case of sqlite and ProgrammingError with postgres.
self.create_tables()
return

def create_tables(self) -> None:
SqlCatalogBaseTable.metadata.create_all(self.engine)
Expand Down

0 comments on commit c045175

Please sign in to comment.