[LNT] r309408 - migrations.util: Fix quoting for renaming tables
Matthias Braun via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 28 11:02:42 PDT 2017
Author: matze
Date: Fri Jul 28 11:02:42 2017
New Revision: 309408
URL: http://llvm.org/viewvc/llvm-project?rev=309408&view=rev
Log:
migrations.util: Fix quoting for renaming tables
Also switched the rename API to use old_name instead of table (as it
only works correctly with plain names and will fail with schemas etc.)
Modified:
lnt/trunk/lnt/server/db/migrations/upgrade_13_to_14.py
lnt/trunk/lnt/server/db/migrations/util.py
Modified: lnt/trunk/lnt/server/db/migrations/upgrade_13_to_14.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/upgrade_13_to_14.py?rev=309408&r1=309407&r2=309408&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/upgrade_13_to_14.py (original)
+++ lnt/trunk/lnt/server/db/migrations/upgrade_13_to_14.py Fri Jul 28 11:02:42 2017
@@ -81,8 +81,7 @@ def upgrade(engine):
_drop_suite(trans, 'compile', engine)
else:
for rename in table_renames:
- tab = introspect_table(engine, rename.old_name)
- rename_table(engine, tab, rename.new_name)
+ rename_table(engine, rename.old_name, rename.new_name)
# Just change the DB_Key to match the name
trans.execute(update(test_suite)
.where(test_suite.c.Name == 'compile')
Modified: lnt/trunk/lnt/server/db/migrations/util.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/migrations/util.py?rev=309408&r1=309407&r2=309408&view=diff
==============================================================================
--- lnt/trunk/lnt/server/db/migrations/util.py (original)
+++ lnt/trunk/lnt/server/db/migrations/util.py Fri Jul 28 11:02:42 2017
@@ -17,6 +17,19 @@ def _visit_add_column(element, compiler,
compiler.get_column_specification(element.column)))
+class _RenameTable(DDLElement):
+ def __init__(self, old_name, new_name):
+ self.old_name = old_name
+ self.new_name = new_name
+
+
+ at compiles(_RenameTable)
+def _visite_rename_table(element, compiler, **kw):
+ return ("ALTER TABLE %s RENAME TO %s" %
+ (compiler.preparer.quote(element.old_name),
+ compiler.preparer.quote(element.new_name)))
+
+
def add_column(engine, table, column):
# type: (sqlalchemy.engine.Engine, sqlalchemy.Table, sqlalchemy.Column) -> None
"""Add this column to the table.
@@ -42,20 +55,11 @@ def introspect_table(engine, name):
return target_table
-def rename_table(engine, old_table, new_name):
- # type: (sqlalchemy.engine.Engine, sqlalchemy.Table, str) -> None
- """Rename the old_table to new_table.
-
- Renames the table by Old_Table -> New_Table_x -> New_Table.
-
- :param engine: to execute on.
- :param old_table: the Table to rename.
- :param new_name: the string name to change the table to.
-
- """
- rename = DDL("ALTER TABLE %(table)s RENAME TO %(new_name)s_x",
- context=dict(new_name=new_name))
- rename.execute(bind=engine, target=old_table)
- rename = DDL("ALTER TABLE %(new_name)s_x RENAME TO %(new_name)s",
- context=dict(new_name=new_name))
+def rename_table(engine, old_name, new_name):
+ """Rename table wiht name \p old_name to \p new_name."""
+ # sqlite refuses to rename "BAR" to "bar" so we go
+ # "BAR" -> "BAR_x" -> "bar"
+ rename = _RenameTable(old_name, old_name+"_x")
+ rename.execute(bind=engine)
+ rename = _RenameTable(old_name+"_x", new_name)
rename.execute(bind=engine)
More information about the llvm-commits
mailing list