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

Switch to column keyword arguments #161

Merged
merged 9 commits into from Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
@@ -1,5 +1,8 @@
0.7.4 (unreleased)
------------------

- Switch from info to keyword arguments on columns for SQLAlchemy >= 1.3.0
ms32035 marked this conversation as resolved.
Show resolved Hide resolved
(`Issue #161 <https://github.com/sqlalchemy-redshift/sqlalchemy-redshift/pull/161>`_)
- Add support for column info on redshift late binding views
- Add support for ``MAXFILESIZE`` argument to ``UNLOAD``.
(`Issue #123 <https://github.com/sqlalchemy-redshift/sqlalchemy-redshift/issues/123>`_)
Expand Down
39 changes: 32 additions & 7 deletions sqlalchemy_redshift/dialect.py
Expand Up @@ -228,14 +228,15 @@ class RedshiftDDLCompiler(PGDDLCompiler):
<BLANKLINE>
<BLANKLINE>

Column-level special syntax can also be applied using the column info
dictionary. For example, we can specify the ENCODE for a column:
Column-level special syntax can also be applied using Redshift dialect
specific keyword arguments.
For example, we can specify the ENCODE for a column:

>>> product = sa.Table(
... 'product',
... metadata,
... sa.Column('id', sa.Integer, primary_key=True),
... sa.Column('name', sa.String, info={'encode': 'lzo'})
... sa.Column('name', sa.String, redshift_encode='lzo')
jklukas marked this conversation as resolved.
Show resolved Hide resolved
... )
>>> print(CreateTable(product).compile(engine))
<BLANKLINE>
Expand All @@ -247,14 +248,28 @@ class RedshiftDDLCompiler(PGDDLCompiler):
<BLANKLINE>
<BLANKLINE>

For SQLAlchemy versions < 1.3.0, passing Redshift dialect options
as keyword arguments is not supported on the column level.
Instead, a column info dictionary can be used:

>>> product_pre_1_3_0 = sa.Table(
... 'product_pre_1_3_0',
... metadata,
... sa.Column('id', sa.Integer, primary_key=True),
... sa.Column('name', sa.String, info={'encode': 'lzo'})
... )

We can also specify the distkey and sortkey options:

>>> sku = sa.Table(
... 'sku',
... metadata,
... sa.Column('id', sa.Integer, primary_key=True),
... sa.Column(
... 'name', sa.String, info={'distkey': True, 'sortkey': True}
... 'name',
... sa.String,
... redshift_distkey=True,
... redshift_sortkey=True
... )
... )
>>> print(CreateTable(sku).compile(engine))
Expand Down Expand Up @@ -328,9 +343,13 @@ def get_column_specification(self, column, **kwargs):

def _fetch_redshift_column_attributes(self, column):
text = ""
if not hasattr(column, 'info'):
return text
info = column.info
if sa.__version__ >= '1.3.0':
info = column.dialect_options['redshift']
jklukas marked this conversation as resolved.
Show resolved Hide resolved
else:
if not hasattr(column, 'info'):
return text
info = column.info

identity = info.get('identity')
if identity:
text += " IDENTITY({0},{1})".format(identity[0], identity[1])
Expand Down Expand Up @@ -381,6 +400,12 @@ class RedshiftDialect(PGDialect_psycopg2):
"sortkey": None,
"interleaved_sortkey": None,
}),
(sa.schema.Column, {
"encode": None,
"distkey": None,
"sortkey": None,
"identity": None,
}),
]

def __init__(self, *args, **kw):
Expand Down
8 changes: 3 additions & 5 deletions tests/rs_sqla_test_utils/models.py
Expand Up @@ -13,7 +13,7 @@ class Basic(Base):
__tablename__ = 'basic'
name = sa.Column(
sa.Unicode(64), primary_key=True,
info={'distkey': True, 'sortkey': True, 'encode': 'lzo'}
redshift_distkey=True, redshift_sortkey=True, redshift_encode='lzo'
)


Expand Down Expand Up @@ -156,7 +156,7 @@ class ReflectionDefaultValue(Base):
class ReflectionIdentity(Base):
__tablename__ = 'reflection_identity'
col1 = sa.Column(sa.Integer(), primary_key=True)
col2 = sa.Column(sa.Integer(), info={'identity': (1, 3)})
col2 = sa.Column(sa.Integer(), redshift_identity=(1, 3))
col3 = sa.Column(sa.Integer())
__table_args__ = (
{'redshift_diststyle': 'EVEN'}
Expand Down Expand Up @@ -218,9 +218,7 @@ class Referenced(Base):
__tablename__ = 'referenced'
id = sa.Column(
sa.Integer(), primary_key=True, nullable=False,
info={
'identity': (1, 1),
}
redshift_identity=(1, 1)
)
__table_args__ = {
'redshift_diststyle': 'EVEN',
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ddl_compiler.py
Expand Up @@ -45,7 +45,7 @@ def test_create_table_with_identity(self, compiler):
table = Table(
't1',
MetaData(),
Column('id', Integer, primary_key=True, info={'identity': [1, 2]}),
Column('id', Integer, primary_key=True, redshift_identity=[1, 2]),
Column('name', String),
)

Expand Down Expand Up @@ -192,7 +192,7 @@ def test_create_column_with_sortkey(self, compiler):
table = Table('t1',
MetaData(),
Column('id', Integer, primary_key=True,
info=dict(sortkey=True)),
redshift_sortkey=True),
Column('name', String)
)

Expand All @@ -210,7 +210,7 @@ def test_create_column_with_distkey(self, compiler):
table = Table('t1',
MetaData(),
Column('id', Integer, primary_key=True,
info=dict(distkey=True)),
redshift_distkey=True),
Column('name', String)
)

Expand All @@ -228,7 +228,7 @@ def test_create_column_with_encoding(self, compiler):
table = Table('t1',
MetaData(),
Column('id', Integer, primary_key=True,
info=dict(encode="LZO")),
redshift_encode="LZO"),
Column('name', String)
)

Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -7,7 +7,7 @@ commands = py.test {posargs}
deps =
requests==2.7.0
psycopg2==2.7.3.2
sqlalchemy==1.2.0
sqlalchemy==1.3.0
pytest==3.10.1
alembic==0.7.6

Expand Down