Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for SQLAlchemy>=2 compatibility #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion citext/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
class CIText(types.Concatenable, types.UserDefinedType):
# This is copied from the `literal_processor` of sqlalchemy's own `String`
# type.
cache_ok = True
def literal_processor(self, dialect):
def process(value):
value = value.replace("'", "''")
Expand Down Expand Up @@ -43,7 +44,10 @@ def process(value):

def register_citext_array(engine):
"""Call once with an engine for citext values to be returned as strings instead of characters"""
results = engine.execute(sqlalchemy.text("SELECT typarray FROM pg_type WHERE typname = 'citext'"))
with engine.connect() as connection:
# Using engine.execute() is deprecated in SQLAlchemy >=1.4,<2, removed in >=2
# Using engine.connect().execute() is compatible with *all* SQLAlchemy >= 1.1 (and probably further back)
results = connection.execute(sqlalchemy.text("SELECT typarray FROM pg_type WHERE typname = 'citext'"))
oids = tuple(row[0] for row in results)
array_type = psycopg2.extensions.new_array_type(oids, 'citext[]', psycopg2.STRING)
psycopg2.extensions.register_type(array_type, None)
Expand Down