diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-07-12 19:36:57 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2022-07-12 19:38:56 -0400 |
commit | 8033c78a52b25a5145557a21630d10a34f2209e5 (patch) | |
tree | ba2674ccfd4db9d5f7ed9b5cb0474430d08bcf18 /alembic/ddl | |
parent | 4fbf99a6ad9f022888ddf709d1e9744d19a76ca6 (diff) | |
download | alembic-8033c78a52b25a5145557a21630d10a34f2209e5.tar.gz |
implement SQLite RENAME TABLE w schema syntax
Fixed bug where the SQLite implementation of
:meth:`.Operations.rename_table` would render an explicit schema name for
both the old and new table name, which while is the standard ALTER syntax,
is not accepted by SQLite's syntax which doesn't support a rename across
schemas. In particular, the syntax issue would prevent batch mode from
working for SQLite databases that made use of attached databases (which are
treated as "schemas" in SQLAlchemy).
Change-Id: I02d8163b39cd33568c7528158218828ff0813695
Fixes: #1065
Diffstat (limited to 'alembic/ddl')
-rw-r--r-- | alembic/ddl/sqlite.py | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/alembic/ddl/sqlite.py b/alembic/ddl/sqlite.py index 9b38766..f986c32 100644 --- a/alembic/ddl/sqlite.py +++ b/alembic/ddl/sqlite.py @@ -11,12 +11,17 @@ from sqlalchemy import cast from sqlalchemy import JSON from sqlalchemy import schema from sqlalchemy import sql +from sqlalchemy.ext.compiler import compiles +from .base import alter_table +from .base import format_table_name +from .base import RenameTable from .impl import DefaultImpl from .. import util if TYPE_CHECKING: from sqlalchemy.engine.reflection import Inspector + from sqlalchemy.sql.compiler import DDLCompiler from sqlalchemy.sql.elements import Cast from sqlalchemy.sql.elements import ClauseElement from sqlalchemy.sql.schema import Column @@ -178,6 +183,16 @@ class SQLiteImpl(DefaultImpl): ) +@compiles(RenameTable, "sqlite") +def visit_rename_table( + element: "RenameTable", compiler: "DDLCompiler", **kw +) -> str: + return "%s RENAME TO %s" % ( + alter_table(compiler, element.table_name, element.schema), + format_table_name(compiler, element.new_table_name, None), + ) + + # @compiles(AddColumn, 'sqlite') # def visit_add_column(element, compiler, **kw): # return "%s %s" % ( |