Skip to content

Commit

Permalink
Add ability to set ORDER / NOORDER sequence on columns with IDENTITY (#…
Browse files Browse the repository at this point in the history
…493)

* Update base.py to add ability to set ORDER / NOORDER sequence

* Set ORDER / NOORDER only if the argument was set. Add test_table_with_identity unit test.
  • Loading branch information
DanielTatarkin authored May 10, 2024
1 parent 107b0b1 commit 9b2f325
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/snowflake/sqlalchemy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,11 +966,14 @@ def visit_drop_column_comment(self, drop, **kw):
)

def visit_identity_column(self, identity, **kw):
text = " IDENTITY"
text = "IDENTITY"
if identity.start is not None or identity.increment is not None:
start = 1 if identity.start is None else identity.start
increment = 1 if identity.increment is None else identity.increment
text += f"({start},{increment})"
if identity.order is not None:
order = "ORDER" if identity.order else "NOORDER"
text += f" {order}"
return text

def get_identity_options(self, identity_options):
Expand Down
26 changes: 26 additions & 0 deletions tests/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from sqlalchemy import (
Column,
Identity,
Integer,
MetaData,
Sequence,
Expand All @@ -13,6 +14,7 @@
select,
)
from sqlalchemy.sql import text
from sqlalchemy.sql.ddl import CreateTable


def test_table_with_sequence(engine_testaccount, db_parameters):
Expand Down Expand Up @@ -135,3 +137,27 @@ def test_table_with_autoincrement(engine_testaccount):

finally:
metadata.drop_all(engine_testaccount)


def test_table_with_identity(sql_compiler):
test_table_name = "identity"
metadata = MetaData()
identity_autoincrement_table = Table(
test_table_name,
metadata,
Column(
"id", Integer, Identity(start=1, increment=1, order=True), primary_key=True
),
Column("identity_col_unordered", Integer, Identity(order=False)),
Column("identity_col", Integer, Identity()),
)
create_table = CreateTable(identity_autoincrement_table)
actual = sql_compiler(create_table)
expected = (
"CREATE TABLE identity ("
"\tid INTEGER NOT NULL IDENTITY(1,1) ORDER, "
"\tidentity_col_unordered INTEGER NOT NULL IDENTITY NOORDER, "
"\tidentity_col INTEGER NOT NULL IDENTITY, "
"\tPRIMARY KEY (id))"
)
assert actual == expected

0 comments on commit 9b2f325

Please sign in to comment.