Skip to content

Commit

Permalink
ensure rename_column works on SQLite
Browse files Browse the repository at this point in the history
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
  • Loading branch information
zzzeek committed Nov 29, 2024
1 parent e09ef30 commit cc582bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions alembic/ddl/sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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" % (
Expand Down
7 changes: 7 additions & 0 deletions docs/build/unreleased/1576.rst
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 5 additions & 0 deletions tests/test_sqlite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit cc582bd

Please sign in to comment.