~ubuntu-branches/ubuntu/wily/heat/wily-proposed

« back to all changes in this revision

Viewing changes to heat/db/sqlalchemy/utils.py

  • Committer: Package Import Robot
  • Author(s): James Page, Corey Bryant, James Page
  • Date: 2015-03-30 11:11:18 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20150330111118-2qpycylx6swu4yhj
Tags: 2015.1~b3-0ubuntu1
[ Corey Bryant ]
* New upstream milestone release for OpenStack kilo:
  - d/control: Align with upstream dependencies.
  - d/p/sudoers_patch.patch: Rebased.
  - d/p/fix-requirements.patch: Rebased.

[ James Page ]
* d/p/fixup-assert-regex.patch: Tweak test to use assertRegexpMatches.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
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
 
5
#
 
6
#         http://www.apache.org/licenses/LICENSE-2.0
 
7
#
 
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
 
12
#    under the License.
 
13
 
 
14
# SQLAlchemy helper functions
 
15
 
 
16
import sqlalchemy
 
17
 
 
18
 
 
19
def clone_table(name, parent, meta, newcols=[], ignorecols=[], swapcols={},
 
20
                ignorecons=[]):
 
21
    """
 
22
    helper function that clones parent table schema onto
 
23
    new table.
 
24
 
 
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
 
31
 
 
32
    :return: sqlalchemy.Table instance
 
33
    """
 
34
 
 
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())
 
39
    cols.extend(newcols)
 
40
    new_table = sqlalchemy.Table(name, meta, *(cols))
 
41
 
 
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:
 
47
                    return True
 
48
 
 
49
        return False
 
50
 
 
51
    constraints = [c.copy() for c in parent.constraints
 
52
                   if c.name not in ignorecons
 
53
                   if not _is_ignorable(c)]
 
54
 
 
55
    for c in constraints:
 
56
        new_table.append_constraint(c)
 
57
 
 
58
    new_table.create()
 
59
    return new_table
 
60
 
 
61
 
 
62
def migrate_data(migrate_engine,
 
63
                 table,
 
64
                 new_table,
 
65
                 skip_columns=None):
 
66
 
 
67
    table_name = table.name
 
68
 
 
69
    list_of_rows = list(table.select().order_by(
 
70
        sqlalchemy.sql.expression.asc(table.c.created_at))
 
71
        .execute())
 
72
 
 
73
    colnames = [c.name for c in table.columns]
 
74
 
 
75
    for row in list_of_rows:
 
76
        values = dict(zip(colnames,
 
77
                          map(lambda colname: getattr(row, colname),
 
78
                              colnames)))
 
79
        if skip_columns is not None:
 
80
            for column in skip_columns:
 
81
                del values[column]
 
82
 
 
83
        migrate_engine.execute(new_table.insert(values))
 
84
 
 
85
    table.drop()
 
86
 
 
87
    new_table.rename(table_name)