Skip to content

Commit 7aa97a8

Browse files
committed
automatically provision hstore for pg13+
Change-Id: I5cd7e9e9ab8a1dae2bd467a1e4299d7f26183301
1 parent e8baf5a commit 7aa97a8

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

README.unittests.rst

+5-10
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,15 @@ Additional steps specific to individual databases are as follows::
191191

192192
postgres=# create database test with owner=scott encoding='utf8' template=template0;
193193

194-
To include tests for HSTORE, create the HSTORE type engine::
194+
To include tests for HSTORE and CITEXT for PostgreSQL versions lower than 13,
195+
create the extensions; for PostgreSQL 13 and above, these
196+
extensions are created automatically as part of the test suite if not
197+
already present::
195198

196199
postgres=# \c test;
197200
You are now connected to database "test" as user "postgresql".
198201
test=# create extension hstore;
199202
CREATE EXTENSION
200-
201-
To include tests for CITEXT for PostgreSQL versions lower than 13,
202-
create the CITEXT extension; for PostgreSQL 13 and above, this
203-
extension is created automatically as part of the test suite if not
204-
already present::
205-
206-
postgres=# \c test;
207-
You are now connected to database "test" as user "postgresql".
208203
test=# create extension citext;
209204
CREATE EXTENSION
210205

@@ -259,7 +254,7 @@ intended for production use!
259254

260255
# configure the database
261256
sleep 10
262-
docker exec -ti postgres psql -U scott -c 'CREATE SCHEMA test_schema; CREATE SCHEMA test_schema_2;CREATE EXTENSION hstore;' test
257+
docker exec -ti postgres psql -U scott -c 'CREATE SCHEMA test_schema; CREATE SCHEMA test_schema_2;CREATE EXTENSION hstore;CREATE EXTENSION citext;' test
263258
# this last command is optional
264259
docker exec -ti postgres sed -i 's/#max_prepared_transactions = 0/max_prepared_transactions = 10/g' /var/lib/postgresql/data/postgresql.conf
265260

lib/sqlalchemy/dialects/postgresql/provision.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,18 @@ def _upsert(cfg, table, returning, set_lambda=None):
148148
return stmt
149149

150150

151+
_extensions = [
152+
("citext", (13,)),
153+
("hstore", (13,)),
154+
]
155+
156+
151157
@post_configure_engine.for_db("postgresql")
152158
def _create_citext_extension(url, engine, follower_ident):
153159
with engine.connect() as conn:
154-
if conn.dialect.server_version_info >= (13,):
155-
conn.execute(text("CREATE EXTENSION IF NOT EXISTS citext"))
156-
conn.commit()
160+
for extension, min_version in _extensions:
161+
if conn.dialect.server_version_info >= min_version:
162+
conn.execute(
163+
text(f"CREATE EXTENSION IF NOT EXISTS {extension}")
164+
)
165+
conn.commit()

0 commit comments

Comments
 (0)