This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-python.git


The following commit(s) were added to refs/heads/main by this push:
     new 56ce5d8  Automatically create the tables for the `SqlCatalog` (#186)
56ce5d8 is described below

commit 56ce5d88c16453e3866a7e3117b69942fecbddf6
Author: Luke Kuzmish <[email protected]>
AuthorDate: Thu Dec 7 09:54:44 2023 -0500

    Automatically create the tables for the `SqlCatalog` (#186)
    
    * ensure tables exist on init
    
    * make private
    
    * remove unnecessary comment
    
    * style
---
 mkdocs/docs/api.md       |  6 ------
 pyiceberg/catalog/sql.py | 14 +++++++++++++-
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/mkdocs/docs/api.md b/mkdocs/docs/api.md
index e2f726a..0e79633 100644
--- a/mkdocs/docs/api.md
+++ b/mkdocs/docs/api.md
@@ -54,12 +54,6 @@ catalog = load_catalog(
 )
 ```
 
-If the catalog has not been initialized before, you need to run:
-
-```python
-catalog.create_tables()
-```
-
 Let's create a namespace:
 
 ```python
diff --git a/pyiceberg/catalog/sql.py b/pyiceberg/catalog/sql.py
index b0c01eb..77ece56 100644
--- a/pyiceberg/catalog/sql.py
+++ b/pyiceberg/catalog/sql.py
@@ -31,7 +31,7 @@ from sqlalchemy import (
     union,
     update,
 )
-from sqlalchemy.exc import IntegrityError
+from sqlalchemy.exc import IntegrityError, OperationalError
 from sqlalchemy.orm import (
     DeclarativeBase,
     Mapped,
@@ -98,6 +98,18 @@ class SqlCatalog(Catalog):
             raise NoSuchPropertyException("SQL connection URI is required")
         self.engine = create_engine(uri_prop, echo=True)
 
+        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:
+                    self.create_tables()
+                    return
+
     def create_tables(self) -> None:
         SqlCatalogBaseTable.metadata.create_all(self.engine)
 

Reply via email to