From 7bf0b8b676e47be766c2be8365c929ce6589b57b Mon Sep 17 00:00:00 2001 From: JKoetsier Date: Thu, 19 Oct 2023 14:34:37 +0200 Subject: [PATCH] Skip schema creation if schema already exists (#520) --- CHANGES.md | 5 +++++ setup.cfg | 2 +- src/schematools/importer/base.py | 12 ++++++++++-- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7ddd6962..991ed246 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 diff --git a/setup.cfg b/setup.cfg index 39caed91..c8ee79c7 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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) diff --git a/src/schematools/importer/base.py b/src/schematools/importer/base.py index 4e429966..5d0b6623 100644 --- a/src/schematools/importer/base.py +++ b/src/schematools/importer/base.py @@ -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 @@ -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, @@ -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: