2
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3
# not use this file except in compliance with the License. You may obtain
4
# a copy of the License at
6
# http://www.apache.org/licenses/LICENSE-2.0
8
# Unless required by applicable law or agreed to in writing, software
9
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11
# License for the specific language governing permissions and limitations
14
# SQLAlchemy helper functions
19
def clone_table(name, parent, meta, newcols=[], ignorecols=[], swapcols={},
22
helper function that clones parent table schema onto
25
:param name: new table name
26
:param parent: parent table to copy schema from
27
:param newcols: names of new columns to be added
28
:param ignorecols: names of columns to be ignored while cloning
29
:param swapcols: alternative column schema
30
:param ignorecons: names of constraints to be ignored
32
:return: sqlalchemy.Table instance
35
cols = [c.copy() for c in parent.columns
36
if c.name not in ignorecols
37
if c.name not in swapcols]
38
cols.extend(swapcols.values())
40
new_table = sqlalchemy.Table(name, meta, *(cols))
42
def _is_ignorable(cons):
43
# consider constraints on columns only
44
if hasattr(cons, 'columns'):
45
for col in ignorecols:
46
if col in cons.columns:
51
constraints = [c.copy() for c in parent.constraints
52
if c.name not in ignorecons
53
if not _is_ignorable(c)]
56
new_table.append_constraint(c)
62
def migrate_data(migrate_engine,
67
table_name = table.name
69
list_of_rows = list(table.select().order_by(
70
sqlalchemy.sql.expression.asc(table.c.created_at))
73
colnames = [c.name for c in table.columns]
75
for row in list_of_rows:
76
values = dict(zip(colnames,
77
map(lambda colname: getattr(row, colname),
79
if skip_columns is not None:
80
for column in skip_columns:
83
migrate_engine.execute(new_table.insert(values))
87
new_table.rename(table_name)