1
# -*- encoding: utf-8 -*-
3
# Copyright © 2013 eNovance SAS <licensing@enovance.com>
5
# Author: Mehdi Abaakouk <mehdi.abaakouk@enovance.com>
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
11
# http://www.apache.org/licenses/LICENSE-2.0
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
19
import sqlalchemy as sa
21
from ceilometer.storage.sqlalchemy import models
24
def _paged(query, size):
27
page = query.offset(offset).limit(size).execute()
28
if page.rowcount <= 0:
29
# There are no more rows
36
def _convert_data_type(table, col, from_t, to_t, pk_attr='id', index=False):
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),
43
sa.Column(temp_col_n, to_t).create(table)
45
key_attr = getattr(table.c, pk_attr)
46
orig_col = getattr(table.c, col)
47
new_col = getattr(table.c, temp_col_n)
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()
55
new_col.alter(name=col)
57
sa.Index('ix_%s_%s' % (table.name, col), new_col).create()
60
def upgrade(migrate_engine):
61
if migrate_engine.name == 'mysql':
62
meta = sa.MetaData(bind=migrate_engine)
63
event = sa.Table('event', meta, autoload=True)
64
_convert_data_type(event, 'generated', sa.Float(),
65
models.PreciseTimestamp(),
66
pk_attr='id', index=True)
67
trait = sa.Table('trait', meta, autoload=True)
68
_convert_data_type(trait, 't_datetime', sa.Float(),
69
models.PreciseTimestamp(),
70
pk_attr='id', index=True)
73
def downgrade(migrate_engine):
74
if migrate_engine.name == 'mysql':
75
meta = sa.MetaData(bind=migrate_engine)
76
event = sa.Table('event', meta, autoload=True)
77
_convert_data_type(event, 'generated', models.PreciseTimestamp(),
78
sa.Float(), pk_attr='id', index=True)
79
trait = sa.Table('trait', meta, autoload=True)
80
_convert_data_type(trait, 't_datetime', models.PreciseTimestamp(),
81
sa.Float(), pk_attr='id', index=True)