Skip to content

Commit

Permalink
Skip schema creation if schema already exists (#520)
Browse files Browse the repository at this point in the history
  • Loading branch information
JKoetsier authored Oct 19, 2023
1 parent f5ba739 commit 7bf0b8b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2023-10-18 (5.17.15)

* Bugfix: Don't try to create schema if schema already exists. Fails on 'create schema'
permissions.

# 2023-10-18 (5.17.14)

* Bugfix: Fixed issue where duplicate indexes were created
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = amsterdam-schema-tools
version = 5.17.14
version = 5.17.15
url = https://github.com/amsterdam/schema-tools
license = Mozilla Public 2.0
author = Team Data Diensten, van het Dataplatform onder de Directie Digitale Voorzieningen (Gemeente Amsterdam)
Expand Down
12 changes: 10 additions & 2 deletions src/schematools/importer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import click
import jsonpath_rw
import psycopg2
import requests
from psycopg2 import sql
from sqlalchemy import Boolean, exc, inspect, text
from sqlalchemy.dialects.postgresql.base import PGInspector
Expand Down Expand Up @@ -184,6 +183,15 @@ def create_pk_lookup(self, tables: dict[str, Table]) -> None:

self.pk_values_lookup[table_name] = pks

def _schema_exists(self, schema_name: str) -> bool:
"""Check if a schema exists in the database."""
with self.engine.connect() as conn:
return bool(
conn.scalar(
"SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = %s)", schema_name
)
)

def generate_db_objects(
self,
table_id: str,
Expand Down Expand Up @@ -248,7 +256,7 @@ def generate_db_objects(
"Is this really what you want?"
)

if db_schema_name is not None:
if db_schema_name is not None and not self._schema_exists(db_schema_name):
self.create_schema(db_schema_name)

if ind_tables or ind_extra_index:
Expand Down

0 comments on commit 7bf0b8b

Please sign in to comment.