]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
Fix postgres detect serial in autogenerate (#1479)
authorzhouyizhen <zhouyizhen@metrodata.tech>
Wed, 5 Jun 2024 19:28:53 +0000 (15:28 -0400)
committersqla-tester <sqla-tester@sqlalchemy.org>
Wed, 5 Jun 2024 19:28:53 +0000 (15:28 -0400)
<!-- Provide a general summary of your proposed changes in the Title field above -->
Fixes: https://github.com/sqlalchemy/alembic/issues/1479
### Description
<!-- Describe your changes in detail -->
In https://github.com/sqlalchemy/alembic/issues/73, it tries to detact postgresql serial in autogenerate, so it won't take `nextval('seq'::regclass)` as server default for that column.
But it takes not effect for tables not in search path. This PR fixed it.

### Checklist
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)

-->

This pull request is:

- [ ] A documentation / typographical error fix
- Good to go, no issue or tests are needed
- [x] A short code fix
- please include the issue number, and create an issue if none exists, which
  must include a complete example of the issue.  one line code fixes without an
  issue and demonstration will not be accepted.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.   one line code fixes without tests will not be accepted.
- [ ] A new feature implementation
- please include the issue number, and create an issue if none exists, which must
  include a complete example of how the feature would look.
- Please include: `Fixes: #<issue number>` in the commit message
- please include tests.

**Have a nice day!**

Closes: #1486
Pull-request: https://github.com/sqlalchemy/alembic/pull/1486
Pull-request-sha: 24df8f906d281df92c531df5a9e1f64d8cdb8527

Change-Id: I50276875bfb1d4f920f0fcd20136337ae09b5384

alembic/ddl/postgresql.py
docs/build/unreleased/1479.rst [new file with mode: 0644]
tests/test_postgresql.py

index 6507fcbdd75c82873e3fc4ff5e3030d8638c0474..de64a4e05b5b2252d77f14a33380ac626a5c6c54 100644 (file)
@@ -218,7 +218,8 @@ class PostgresqlImpl(DefaultImpl):
                         "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/docs/build/unreleased/1479.rst b/docs/build/unreleased/1479.rst
new file mode 100644 (file)
index 0000000..5a65320
--- /dev/null
@@ -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
index 5741b080e878113a3caba83e4951572e036f8926..e42ea9d3b2beb46d352596e4d8534a1d38958d3b 100644 (file)
@@ -859,8 +859,8 @@ class PostgresqlDetectSerialTest(TestBase):
         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
 
@@ -871,7 +871,7 @@ class PostgresqlDetectSerialTest(TestBase):
         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]
 
@@ -884,12 +884,12 @@ class PostgresqlDetectSerialTest(TestBase):
 
         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,
@@ -903,35 +903,47 @@ class PostgresqlDetectSerialTest(TestBase):
             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,
         )