8000 This commit removes pkg_resources requirements as they are deprecated… · Shiphero/sqlalchemy-redshift@12c1844 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit 12c1844

Browse files
committed
This commit removes pkg_resources requirements as they are deprecated with more recent versions of Python.
importlib.metadata.distribution is used in place of pkg_resources.get_distribution importlib.metadata.version is used in place of pkg_resources.version ModuleNotFoundError is used instead of pkg_resources.DistributionNotFound Port of sqlalchemy-redshift#294
2 parents 4040bd4 + 5fe878c commit 12c1844

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

sqlalchemy_redshift/dialect.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections import defaultdict, namedtuple
55
from logging import getLogger
66
from pathlib import Path
7+
from typing import Any, Optional
78

89
import sqlalchemy as sa
910
from packaging.version import Version
@@ -438,9 +439,9 @@ def __new__(cls, name, schema=None, connection=None):
438439

439440
def __str__(self):
440441
if self.schema is None:
441-
return self.name
442+
return RelationKey._unquote(self.name)
442443
else:
443-
return self.schema + "." + self.name
444+
return RelationKey._unquote(self.schema) + "." + RelationKey._unquote(self.name)
444445

445446
@staticmethod
446447
def _unquote(part):
@@ -654,6 +655,9 @@ def visit_HLLSKETCH(self, type_, **kw):
654655
class RedshiftIdentifierPreparer(PGIdentifierPreparer):
655656
reserved_words = RESERVED_WORDS
656657

658+
def quote_schema(self, schema: Any, force: Optional[bool] = ...) -> str:
659+
return schema
660+
657661

658662
class RedshiftDialectMixin(DefaultDialect):
659663
"""
@@ -670,6 +674,7 @@ class RedshiftDialectMixin(DefaultDialect):
670674
statement_compiler = RedshiftCompiler
671675
ddl_compiler = RedshiftDDLCompiler
672676
preparer = RedshiftIdentifierPreparer
677+
identifier_preparer = RedshiftIdentifierPreparer
673678
type_compiler = RedshiftTypeCompiler
674679
construct_arguments = [
675680
(sa.schema.Index, {
@@ -793,12 +798,12 @@ def get_check_constraints(self, connection, table_name, schema=None, **kw):
793798
def get_table_oid(self, connection, table_name, schema=None, **kw):
794799
"""Fetch the oid for schema.table_name.
795800
Return null if not found (external table does not have table oid)"""
796-
schema_field = '"{schema}".'.format(schema=schema) if schema else ""
801+
schema_field = '{schema}.'.format(schema=schema) if schema else ""
797802

798803
result = connection.execute(
799804
sa.text(
800805
"""
801-
select '{schema_field}"{table_name}"'::regclass::oid;
806+
select '{schema_field}{table_name}'::regclass::oid;
802807
""".format(
803808
schema_field=schema_field,
804809
table_name=table_name
@@ -857,8 +862,8 @@ def get_foreign_keys(self, connection, table_name, schema=None, **kw):
857862
fkey_d = {
858863
'name': conname,
859864
'constrained_columns': constrained_columns,
860-
'referred_schema': referred_schema,
861-
'referred_table': referred_table,
865+
'referred_schema': self.unquote(referred_schema),
866+
'referred_table': self.unquote(referred_table),
862867
'referred_columns': referred_columns,
863868
}
864869
fkeys.append(fkey_d)
@@ -908,6 +913,16 @@ def get_indexes(self, connection, table_name, schema, **kw):
908913
"""
909914
return []
910915

916+
@staticmethod
917+
def unquote(text):
918+
if text is None:
919+
return None
920+
921+
if text.startswith('"') and text.endswith('"'):
922+
return text[1:-1]
923+
924+
return text
925+
911926
@reflection.cache
912927
def get_unique_constraints(self, connection, table_name,
913928
schema=None, **kw):
@@ -977,7 +992,7 @@ def _get_table_or_view_names(self, relkind, connection, schema=None, **kw):
977992
relation_names = []
978993
for key, relation in all_relations.items():
979994
if key.schema == schema and relation.relkind == relkind:
980-
relation_names.append(key.name)
995+
relation_names.append(self.unquote(key.name))
981996
return relation_names
982997

983998
def _get_column_info(self, *args, **kwargs):
@@ -1110,6 +1125,7 @@ def _get_all_relation_info(self, connection, **kw):
11101125
@reflection.cache
11111126
def _get_schema_column_info(self, connection, **kw):
11121127
schema = kw.get('schema', None)
1128+
schema = self.unquote(schema)
11131129
schema_clause = (
11141130
"AND schema = '{schema}'".format(schema=schema) if schema else ""
11151131
)

tests/rs_sqla_test_utils/models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ class BasicInOtherSchema(Base):
3232
col1 = sa.Column(sa.Integer(), primary_key=True)
3333

3434

35+
class BasicInIncludingDotSchema(Base):
36+
__tablename__ = 'basic'
37+
__table_args__ = (
38+
{'schema': 'dotted.schema',
39+
'redshift_diststyle': 'KEY',
40+
'redshift_distkey': 'col1',
41+
'redshift_sortkey': 'col1'}
42+
)
43+
col1 = sa.Column(sa.Integer(), primary_key=True)
44+
45+
3546
class ReflectionDistKey(Base):
3647
__tablename__ = 'reflection_distkey'
3748
col1 = sa.Column(sa.Integer(), primary_key=True)

tests/test_compiler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
from sqlalchemy import func, select
22

3+
from tests.rs_sqla_test_utils import models
4+
35

46
def test_func_now(stub_redshift_dialect):
57
dialect = stub_redshift_dialect
68
s = select([func.NOW().label("time")])
79
compiled = s.compile(dialect=dialect)
810
assert str(compiled) == "SELECT SYSDATE AS time"
11+
12+
def test_unquoting_schema(stub_redshift_dialect):
13+
dialect = stub_redshift_dialect
14+
s = select(models.BasicInIncludingDotSchema.col1).where(models.BasicInIncludingDotSchema.col1 == 1)
15+
compiled = s.compile(dialect=dialect)
16+
assert "dotted.schema" in str(compiled)
17+
assert "\"dotted.schema\"" not in str(compiled)

0 commit comments

Comments
 (0)
0