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

Fix postgres detect serial in autogenerate (#1479) #1486

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
3 changes: 2 additions & 1 deletion alembic/ddl/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ def autogen_column_reflect(self, inspector, table, column_info):
"join pg_class t on t.oid=d.refobjid "
"join pg_attribute a on a.attrelid=t.oid and "
"a.attnum=d.refobjsubid "
"where c.relkind='S' and c.relname=:seqname"
"where c.relkind='S' and "
"c.oid=cast(:seqname as regclass)"
CaselIT marked this conversation as resolved.
Show resolved Hide resolved
),
seqname=seq_match.group(1),
).first()
Expand Down
6 changes: 6 additions & 0 deletions docs/build/unreleased/1479.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.. change::
:tags: bug, autogenerate, postgresql
:tickets: 1479

Fixed the detection of serial column in autogenerate with tables
not under default schema on PostgreSQL
44 changes: 28 additions & 16 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,8 @@ def teardown_class(cls):
clear_staging_env()

@provide_metadata
def _expect_default(self, c_expected, col, seq=None):
Table("t", self.metadata, col)
def _expect_default(self, c_expected, col, schema=None, seq=None):
Table("t", self.metadata, col, schema=schema)

self.autogen_context.metadata = self.metadata

Expand All @@ -871,7 +871,7 @@ def _expect_default(self, c_expected, col, seq=None):
insp = inspect(config.db)

uo = ops.UpgradeOps(ops=[])
_compare_tables({(None, "t")}, set(), insp, uo, self.autogen_context)
_compare_tables({(schema, "t")}, set(), insp, uo, self.autogen_context)
diffs = uo.as_diffs()
tab = diffs[0][1]

Expand All @@ -884,12 +884,12 @@ def _expect_default(self, c_expected, col, seq=None):

insp = inspect(config.db)
uo = ops.UpgradeOps(ops=[])
m2 = MetaData()
m2 = MetaData(schema=schema)
Table("t", m2, Column("x", BigInteger()))
self.autogen_context.metadata = m2
_compare_tables(
{(None, "t")},
{(None, "t")},
{(schema, "t")},
{(schema, "t")},
insp,
uo,
self.autogen_context,
Expand All @@ -903,35 +903,47 @@ def _expect_default(self, c_expected, col, seq=None):
c_expected,
)

def test_serial(self):
self._expect_default(None, Column("x", Integer, primary_key=True))
@testing.combinations((None,), ("test_schema",))
def test_serial(self, schema):
self._expect_default(
None, Column("x", Integer, primary_key=True), schema
)

def test_separate_seq(self):
seq = Sequence("x_id_seq")
@testing.combinations((None,), ("test_schema",))
def test_separate_seq(self, schema):
seq = Sequence("x_id_seq", schema=schema)
seq_name = seq.name if schema is None else f"{schema}.{seq.name}"
self._expect_default(
"nextval('x_id_seq'::regclass)",
f"nextval('{seq_name}'::regclass)",
Column(
"x", Integer, server_default=seq.next_value(), primary_key=True
),
schema,
seq,
)

def test_numeric(self):
seq = Sequence("x_id_seq")
@testing.combinations((None,), ("test_schema",))
def test_numeric(self, schema):
seq = Sequence("x_id_seq", schema=schema)
seq_name = seq.name if schema is None else f"{schema}.{seq.name}"
self._expect_default(
"nextval('x_id_seq'::regclass)",
f"nextval('{seq_name}'::regclass)",
Column(
"x",
Numeric(8, 2),
server_default=seq.next_value(),
primary_key=True,
),
schema,
seq,
)

def test_no_default(self):
@testing.combinations((None,), ("test_schema",))
def test_no_default(self, schema):
self._expect_default(
None, Column("x", Integer, autoincrement=False, primary_key=True)
None,
Column("x", Integer, autoincrement=False, primary_key=True),
schema,
)


Expand Down
Loading