diff --git a/alembic/ddl/sqlite.py b/alembic/ddl/sqlite.py index 762e8ca1..fce5c239 100644 --- a/alembic/ddl/sqlite.py +++ b/alembic/ddl/sqlite.py @@ -16,6 +16,8 @@ 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 index 00000000..f7d4d313 --- /dev/null +++ b/docs/build/unreleased/1576.rst @@ -0,0 +1,7 @@ +.. change:: + :tags: usecase, sqlite + :tickets: 1576 + + Modified SQLite's dialect to render "ALTER TABLE RENAME COLUMN" when + :meth:`.Operations.alter_column` is used with a straight rename, supporting + SQLite's recently added column rename feature. diff --git a/tests/test_sqlite.py b/tests/test_sqlite.py index d3c5367b..8a62f39b 100644 --- a/tests/test_sqlite.py +++ b/tests/test_sqlite.py @@ -36,6 +36,11 @@ def test_add_column(self): 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))