~ubuntu-branches/ubuntu/trusty/ceilometer/trusty-proposed

« back to all changes in this revision

Viewing changes to ceilometer/storage/sqlalchemy/migrate_repo/versions/025_alarm_use_floatingprecision.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short, James Page, Chuck Short
  • Date: 2014-01-23 15:08:11 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20140123150811-1zaismsuyh1hcl8y
Tags: 2014.1~b2-0ubuntu1
[ James Page ]
* d/control: Add python-jsonpath-rw to BD's.
* d/p/fix-setup-requirements.patch: Bump WebOb to support < 1.4.
 (LP: #1261101)

[ Chuck Short ]
* New upstream version.
* debian/control, debian/ceilometer-common.install: Split out
  ceilometer-alarm-evaluator and ceilometer-alarm-notifier into their
  own packages. (LP: #1250002)
* debian/ceilometer-agent-central.logrotate,
  debian/ceilometer-agent-compute.logrotate,
  debian/ceilometer-api.logrotate,
  debian/ceilometer-collector.logrotate: Add logrotate files, 
  thanks to Ahmed Rahal. (LP: #1224223)
* Fix typos in upstart files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- encoding: utf-8 -*-
 
2
#
 
3
# Copyright © 2013 eNovance SAS <licensing@enovance.com>
 
4
#
 
5
# Author: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
 
6
#
 
7
# Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
# not use this file except in compliance with the License. You may obtain
 
9
# a copy of the License at
 
10
#
 
11
#      http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
# Unless required by applicable law or agreed to in writing, software
 
14
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
# License for the specific language governing permissions and limitations
 
17
# under the License.
 
18
 
 
19
import sqlalchemy as sa
 
20
 
 
21
from ceilometer.storage.sqlalchemy import models
 
22
 
 
23
 
 
24
def _paged(query, size):
 
25
    offset = 0
 
26
    while True:
 
27
        page = query.offset(offset).limit(size).execute()
 
28
        if page.rowcount <= 0:
 
29
            # There are no more rows
 
30
            break
 
31
        for row in page:
 
32
            yield row
 
33
        offset += size
 
34
 
 
35
 
 
36
def _convert_data_type(table, col, from_t, to_t, pk_attr='id'):
 
37
    temp_col_n = 'convert_data_type_temp_col'
 
38
    # Override column we're going to convert with from_t, since the type we're
 
39
    # replacing could be custom and we need to tell SQLALchemy how to perform
 
40
    # CRUD operations with it.
 
41
    table = sa.Table(table.name, table.metadata, sa.Column(col, from_t),
 
42
                     extend_existing=True)
 
43
    sa.Column(temp_col_n, to_t).create(table)
 
44
 
 
45
    key_attr = getattr(table.c, pk_attr)
 
46
    orig_col = getattr(table.c, col)
 
47
    new_col = getattr(table.c, temp_col_n)
 
48
 
 
49
    query = sa.select([key_attr, orig_col])
 
50
    for key, value in _paged(query, 1000):
 
51
        table.update().where(key_attr == key)\
 
52
            .values({temp_col_n: value}).execute()
 
53
 
 
54
    orig_col.drop()
 
55
    new_col.alter(name=col)
 
56
 
 
57
 
 
58
to_convert = [
 
59
    ('alarm', 'timestamp', 'id'),
 
60
    ('alarm', 'state_timestamp', 'id'),
 
61
    ('alarm_history', 'timestamp', 'alarm_id'),
 
62
]
 
63
 
 
64
 
 
65
def upgrade(migrate_engine):
 
66
    if migrate_engine.name == 'mysql':
 
67
        meta = sa.MetaData(bind=migrate_engine)
 
68
        for table_name, col_name, pk_attr in to_convert:
 
69
            table = sa.Table(table_name, meta, autoload=True)
 
70
            _convert_data_type(table, col_name, sa.DateTime(),
 
71
                               models.PreciseTimestamp(),
 
72
                               pk_attr=pk_attr)
 
73
 
 
74
 
 
75
def downgrade(migrate_engine):
 
76
    if migrate_engine.name == 'mysql':
 
77
        meta = sa.MetaData(bind=migrate_engine)
 
78
        for table_name, col_name, pk_attr in to_convert:
 
79
            table = sa.Table(table_name, meta, autoload=True)
 
80
            _convert_data_type(table, col_name, models.PreciseTimestamp(),
 
81
                               sa.DateTime(),
 
82
                               pk_attr=pk_attr)