]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
ensure rename_column works on SQLite
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Nov 2024 16:58:53 +0000 (11:58 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 29 Nov 2024 17:05:29 +0000 (12:05 -0500)
Modified SQLite's dialect to render "ALTER TABLE <t> RENAME COLUMN" when
:meth:`.Operations.alter_column` is used with a straight rename, supporting
SQLite's recently added column rename feature.

References: #1576
Change-Id: Ia84c4fda144c2767393e4748ced60479016f2c91

alembic/ddl/sqlite.py
docs/build/unreleased/1576.rst [new file with mode: 0644]
tests/test_sqlite.py

index 762e8ca198a6af4d001afd362ff15ac9c43a2821..fce5c2396a3379374ce3a59d18e15fed66b50b07 100644 (file)
@@ -16,6 +16,8 @@ from sqlalchemy import schema
 from sqlalchemy import sql
 
 from .base import alter_table
+from .base import ColumnName
+from .base import format_column_name
 from .base import format_table_name
 from .base import RenameTable
 from .impl import DefaultImpl
@@ -207,6 +209,15 @@ def visit_rename_table(
     )
 
 
+@compiles(ColumnName, "sqlite")
+def visit_column_name(element: ColumnName, compiler: DDLCompiler, **kw) -> str:
+    return "%s RENAME COLUMN %s TO %s" % (
+        alter_table(compiler, element.table_name, element.schema),
+        format_column_name(compiler, element.column_name),
+        format_column_name(compiler, element.newname),
+    )
+
+
 # @compiles(AddColumn, 'sqlite')
 # def visit_add_column(element, compiler, **kw):
 #    return "%s %s" % (
diff --git a/docs/build/unreleased/1576.rst b/docs/build/unreleased/1576.rst
new file mode 100644 (file)
index 0000000..f7d4d31
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: usecase, sqlite
+    :tickets: 1576
+
+    Modified SQLite's dialect to render "ALTER TABLE <t> RENAME COLUMN" when
+    :meth:`.Operations.alter_column` is used with a straight rename, supporting
+    SQLite's recently added column rename feature.
index d3c5367b879cf5f9e45d8aeaaadd0fda2e96cc73..8a62f39bd44f6b97cae387c04f6cfca63088821c 100644 (file)
@@ -36,6 +36,11 @@ class SQLiteTest(TestBase):
         op.add_column("t1", Column("c1", Integer))
         context.assert_("ALTER TABLE t1 ADD COLUMN c1 INTEGER")
 
+    def test_rename_column(self):
+        context = op_fixture("sqlite")
+        op.alter_column("t1", column_name="old", new_column_name="new")
+        context.assert_("ALTER TABLE t1 RENAME COLUMN old TO new")
+
     def test_add_column_implicit_constraint(self):
         context = op_fixture("sqlite")
         op.add_column("t1", Column("c1", Boolean))