From: Federico Caselli Date: Wed, 15 Mar 2023 19:23:40 +0000 (+0100) Subject: automatically provision hstore for pg13+ X-Git-Tag: rel_2_0_8~18^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7aa97a8c249e75dcc08f509313018520f74a86cb;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git automatically provision hstore for pg13+ Change-Id: I5cd7e9e9ab8a1dae2bd467a1e4299d7f26183301 --- diff --git a/README.unittests.rst b/README.unittests.rst index 5947284942..9cf309d2d7 100644 --- a/README.unittests.rst +++ b/README.unittests.rst @@ -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 diff --git a/lib/sqlalchemy/dialects/postgresql/provision.py b/lib/sqlalchemy/dialects/postgresql/provision.py index 5c6b9fcea6..5821576046 100644 --- a/lib/sqlalchemy/dialects/postgresql/provision.py +++ b/lib/sqlalchemy/dialects/postgresql/provision.py @@ -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()