|
@@ -1,6 +1,7 @@
|
1
|
1
|
# -*- coding: utf-8 -*-
|
2
|
2
|
|
3
|
3
|
import os
|
|
4
|
+import time
|
4
|
5
|
|
5
|
6
|
import sqlalchemy as sqla
|
6
|
7
|
from sqlalchemy.ext.compiler import compiles
|
|
@@ -55,8 +56,42 @@ def visit_drop_column(element, ddlcompiler, **kw):
|
55
|
56
|
return 'ALTER TABLE %s DROP COLUMN %s'%(tname, colname)
|
56
|
57
|
|
57
|
58
|
@compiles(DropColumn)
|
|
59
|
+##
|
|
60
|
+# @warning Returns more than one query @ref Database.sqlutils.ddl_execute
|
58
|
61
|
def visit_drop_column(element, ddlcompiler, **kw):
|
59
|
|
- raise NotImplementedError('Drop column not yet implemented for '+str(ddlcompiler.dialect.name))
|
|
62
|
+ prep = ddlcompiler.sql_compiler.preparer
|
|
63
|
+ tname = prep.format_table(element.table)
|
|
64
|
+
|
|
65
|
+ #Temporary table
|
|
66
|
+ tmpname = str(element.table)+'_copydropcol'
|
|
67
|
+ tmpTable = sqla.Table(tmpname, sqla.MetaData())
|
|
68
|
+ tmptname = prep.format_table(tmpTable)
|
|
69
|
+
|
|
70
|
+ query = 'ALTER TABLE %s RENAME TO %s; '%(tname, tmptname)
|
|
71
|
+
|
|
72
|
+ colname = prep.format_column(element.col)
|
|
73
|
+
|
|
74
|
+ meta = sqla.MetaData()
|
|
75
|
+ newtable = sqla.Table(element.table, meta)
|
|
76
|
+ clist = element.table.columns
|
|
77
|
+
|
|
78
|
+ cols = []
|
|
79
|
+ for col in clist:
|
|
80
|
+ if col.name != element.col.name:
|
|
81
|
+ newtable.append_column(col.copy())
|
|
82
|
+ cols.append(prep.format_column(col))
|
|
83
|
+ cols=', '.join(cols)
|
|
84
|
+
|
|
85
|
+ query += str(sqla.schema.CreateTable(newtable).compile(dialect = ddlcompiler.dialect))+';'
|
|
86
|
+
|
|
87
|
+ query += 'INSERT INTO %s ( %s ) SELECT %s FROM %s;' % (newtable, cols, cols, tmptname)
|
|
88
|
+
|
|
89
|
+ query += 'DROP TABLE %s'% (tmpname)
|
|
90
|
+
|
|
91
|
+ return query
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ #raise NotImplementedError('Drop column not yet implemented for '+str(ddlcompiler.dialect.name))
|
60
|
95
|
|
61
|
96
|
class AlterColumn(sqla.schema.DDLElement):
|
62
|
97
|
""" Defines the DDL for changing the type of a column """
|