From bd4fa1b458a78681c307564f6bbfc58ccbe81cad Mon Sep 17 00:00:00 2001 From: zhouyizhen Date: Tue, 4 Jun 2024 16:58:01 +0800 Subject: [PATCH 1/3] Fix postgres detect serial in autogenerate (#1479) --- alembic/ddl/postgresql.py | 3 ++- tests/test_postgresql.py | 20 +++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index 6507fcbd..de64a4e0 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -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)" ), seqname=seq_match.group(1), ).first() diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 5741b080..6ce4a7b9 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -860,7 +860,7 @@ def teardown_class(cls): @provide_metadata def _expect_default(self, c_expected, col, seq=None): - Table("t", self.metadata, col) + Table("t", self.metadata, col, schema="test_schema") self.autogen_context.metadata = self.metadata @@ -871,7 +871,9 @@ 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( + {("test_schema", "t")}, set(), insp, uo, self.autogen_context + ) diffs = uo.as_diffs() tab = diffs[0][1] @@ -884,12 +886,12 @@ def _expect_default(self, c_expected, col, seq=None): insp = inspect(config.db) uo = ops.UpgradeOps(ops=[]) - m2 = MetaData() + m2 = MetaData(schema="test_schema") Table("t", m2, Column("x", BigInteger())) self.autogen_context.metadata = m2 _compare_tables( - {(None, "t")}, - {(None, "t")}, + {("test_schema", "t")}, + {("test_schema", "t")}, insp, uo, self.autogen_context, @@ -907,9 +909,9 @@ def test_serial(self): self._expect_default(None, Column("x", Integer, primary_key=True)) def test_separate_seq(self): - seq = Sequence("x_id_seq") + seq = Sequence("x_id_seq", schema="test_schema") self._expect_default( - "nextval('x_id_seq'::regclass)", + "nextval('test_schema.x_id_seq'::regclass)", Column( "x", Integer, server_default=seq.next_value(), primary_key=True ), @@ -917,9 +919,9 @@ def test_separate_seq(self): ) def test_numeric(self): - seq = Sequence("x_id_seq") + seq = Sequence("x_id_seq", schema="test_schema") self._expect_default( - "nextval('x_id_seq'::regclass)", + "nextval('test_schema.x_id_seq'::regclass)", Column( "x", Numeric(8, 2), From 75716c3440c66d3d7b3ea846b626ed9527ae30dc Mon Sep 17 00:00:00 2001 From: zhouyizhen Date: Wed, 5 Jun 2024 11:39:02 +0800 Subject: [PATCH 2/3] add test for public schema --- tests/test_postgresql.py | 46 ++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 6ce4a7b9..e42ea9d3 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -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, schema="test_schema") + def _expect_default(self, c_expected, col, schema=None, seq=None): + Table("t", self.metadata, col, schema=schema) self.autogen_context.metadata = self.metadata @@ -871,9 +871,7 @@ def _expect_default(self, c_expected, col, seq=None): insp = inspect(config.db) uo = ops.UpgradeOps(ops=[]) - _compare_tables( - {("test_schema", "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] @@ -886,12 +884,12 @@ def _expect_default(self, c_expected, col, seq=None): insp = inspect(config.db) uo = ops.UpgradeOps(ops=[]) - m2 = MetaData(schema="test_schema") + m2 = MetaData(schema=schema) Table("t", m2, Column("x", BigInteger())) self.autogen_context.metadata = m2 _compare_tables( - {("test_schema", "t")}, - {("test_schema", "t")}, + {(schema, "t")}, + {(schema, "t")}, insp, uo, self.autogen_context, @@ -905,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", schema="test_schema") + @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('test_schema.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", schema="test_schema") + @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('test_schema.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, ) From 24df8f906d281df92c531df5a9e1f64d8cdb8527 Mon Sep 17 00:00:00 2001 From: zhouyizhen Date: Wed, 5 Jun 2024 11:53:50 +0800 Subject: [PATCH 3/3] add change log --- docs/build/unreleased/1479.rst | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 docs/build/unreleased/1479.rst diff --git a/docs/build/unreleased/1479.rst b/docs/build/unreleased/1479.rst new file mode 100644 index 00000000..5a653201 --- /dev/null +++ b/docs/build/unreleased/1479.rst @@ -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