Skip to content

Commit b59da2e

Browse files
committed
IDENTITY column attribute
1 parent cc8810a commit b59da2e

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

redshift_sqlalchemy/dialect.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sqlalchemy.engine import reflection
55
from sqlalchemy.ext.compiler import compiles
66
from sqlalchemy.sql.expression import BindParameter, Executable, ClauseElement
7-
from sqlalchemy.types import VARCHAR, NullType
7+
from sqlalchemy.types import VARCHAR, NullType, BigInteger, Integer
88

99

1010
class RedShiftDDLCompiler(PGDDLCompiler):
@@ -75,17 +75,16 @@ def post_create_table(self, table):
7575
return text
7676

7777
def get_column_specification(self, column, **kwargs):
78-
# aron - Apr 21, 2014: Redshift doesn't support serial types. So I
79-
# removed support for them here.
8078
colspec = self.preparer.format_column(column)
81-
colspec += " " + self.dialect.type_compiler.process(column.type)
8279

83-
colspec += self._fetch_redshift_column_attributes(column)
80+
colspec += " " + self.dialect.type_compiler.process(column.type)
8481

8582
default = self.get_column_default_string(column)
8683
if default is not None:
8784
colspec += " DEFAULT " + default
8885

86+
colspec += self._fetch_redshift_column_attributes(column)
87+
8988
if not column.nullable:
9089
colspec += " NOT NULL"
9190
return colspec
@@ -95,6 +94,10 @@ def _fetch_redshift_column_attributes(self, column):
9594
if not hasattr(column, 'info'):
9695
return text
9796
info = column.info
97+
identity = info.get('identity', None)
98+
if identity:
99+
text += " IDENTITY({0},{1})".format(identity[0], identity[1])
100+
98101
encode = info.get('encode', None)
99102
if encode:
100103
text += " ENCODE " + encode

tests/test_ddl_compiler.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import difflib
22

33
from pytest import fixture
4-
from sqlalchemy import Table, Column, Integer, String, MetaData
4+
from sqlalchemy import Table, Column, Integer, String, Sequence, MetaData
55
from sqlalchemy.exc import CompileError
66
from sqlalchemy.schema import CreateTable
77

@@ -40,6 +40,22 @@ def test_create_table_simple(self, compiler):
4040
u"\n\tPRIMARY KEY (id)\n)\n\n"
4141
assert expected == actual, self._compare_strings(expected, actual)
4242

43+
def test_create_table_with_identity(self, compiler):
44+
45+
table = Table('t1',
46+
MetaData(),
47+
Column('id', Integer, primary_key=True, info={'identity': [1, 2]}),
48+
Column('name', String))
49+
50+
51+
create_table = CreateTable(table)
52+
actual = compiler.process(create_table)
53+
expected = u"\nCREATE TABLE t1 ("\
54+
u"\n\tid INTEGER IDENTITY(1,2) NOT NULL, "\
55+
u"\n\tname VARCHAR, "\
56+
u"\n\tPRIMARY KEY (id)\n)\n\n"
57+
assert expected == actual, self._compare_strings(expected, actual)
58+
4359
def test_create_table_with_diststyle(self, compiler):
4460

4561
table = Table('t1',

0 commit comments

Comments
 (0)