]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
automatically provision hstore for pg13+
authorFederico Caselli <cfederico87@gmail.com>
Wed, 15 Mar 2023 19:23:40 +0000 (20:23 +0100)
committerFederico Caselli <cfederico87@gmail.com>
Wed, 15 Mar 2023 19:23:54 +0000 (20:23 +0100)
Change-Id: I5cd7e9e9ab8a1dae2bd467a1e4299d7f26183301

README.unittests.rst
lib/sqlalchemy/dialects/postgresql/provision.py

index 594728494295647ce794f98abda0ca78b63e5e16..9cf309d2d7e447140220087ff690c87a5b73db71 100644 (file)
@@ -191,20 +191,15 @@ Additional steps specific to individual databases are as follows::
 
         postgres=# create database test with owner=scott encoding='utf8' template=template0;
 
-    To include tests for HSTORE, create the HSTORE type engine::
+    To include tests for HSTORE and CITEXT for PostgreSQL versions lower than 13,
+    create the extensions; for PostgreSQL 13 and above, these
+    extensions are created automatically as part of the test suite if not
+    already present::
 
         postgres=# \c test;
         You are now connected to database "test" as user "postgresql".
         test=# create extension hstore;
         CREATE EXTENSION
-
-    To include tests for CITEXT for PostgreSQL versions lower than 13,
-    create the CITEXT extension; for PostgreSQL 13 and above, this
-    extension is created automatically as part of the test suite if not
-    already present::
-
-        postgres=# \c test;
-        You are now connected to database "test" as user "postgresql".
         test=# create extension citext;
         CREATE EXTENSION
 
@@ -259,7 +254,7 @@ intended for production use!
 
     # configure the database
     sleep 10
-    docker exec -ti postgres psql -U scott -c 'CREATE SCHEMA test_schema; CREATE SCHEMA test_schema_2;CREATE EXTENSION hstore;' test
+    docker exec -ti postgres psql -U scott -c 'CREATE SCHEMA test_schema; CREATE SCHEMA test_schema_2;CREATE EXTENSION hstore;CREATE EXTENSION citext;' test
     # this last command is optional
     docker exec -ti postgres sed -i 's/#max_prepared_transactions = 0/max_prepared_transactions = 10/g' /var/lib/postgresql/data/postgresql.conf
 
index 5c6b9fcea6344059d1a87d9bc57041fc569f16bf..58215760462f5ad754ebc9d5c9b9c5693a58a97b 100644 (file)
@@ -148,9 +148,18 @@ def _upsert(cfg, table, returning, set_lambda=None):
     return stmt
 
 
+_extensions = [
+    ("citext", (13,)),
+    ("hstore", (13,)),
+]
+
+
 @post_configure_engine.for_db("postgresql")
 def _create_citext_extension(url, engine, follower_ident):
     with engine.connect() as conn:
-        if conn.dialect.server_version_info >= (13,):
-            conn.execute(text("CREATE EXTENSION IF NOT EXISTS citext"))
-            conn.commit()
+        for extension, min_version in _extensions:
+            if conn.dialect.server_version_info >= min_version:
+                conn.execute(
+                    text(f"CREATE EXTENSION IF NOT EXISTS {extension}")
+                )
+                conn.commit()