~ubuntu-branches/ubuntu/vivid/ceilometer/vivid

« back to all changes in this revision

Viewing changes to ceilometer/storage/sqlalchemy/alembic/versions/b6ae66d05e3_remove_extra_indexes.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-03-06 14:44:28 UTC
  • mto: (28.1.1 utopic-proposed) (1.2.1)
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: package-import@ubuntu.com-20140306144428-rvphsh4igwyulzf0
Tags: upstream-2014.1~b3
ImportĀ upstreamĀ versionĀ 2014.1~b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- encoding: utf-8 -*-
2
 
#
3
 
# Copyright 2010-2011 OpenStack Foundation
4
 
# Copyright 2012-2013 IBM Corp. #
5
 
#
6
 
# Authors: Svetlana Shturm <sshturm@mirantis.com>
7
 
#
8
 
# Licensed under the Apache License, Version 2.0 (the "License"); you may
9
 
# not use this file except in compliance with the License. You may obtain
10
 
# a copy of the License at
11
 
#
12
 
#      http://www.apache.org/licenses/LICENSE-2.0
13
 
#
14
 
# Unless required by applicable law or agreed to in writing, software
15
 
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
 
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
 
# License for the specific language governing permissions and limitations
18
 
# under the License.
19
 
"""Remove extra indexes
20
 
 
21
 
Revision ID: b6ae66d05e3
22
 
Revises: 17738166b91
23
 
Create Date: 2013-08-19 15:54:43.529222
24
 
 
25
 
"""
26
 
 
27
 
# revision identifiers, used by Alembic.
28
 
revision = 'b6ae66d05e3'
29
 
down_revision = '17738166b91'
30
 
 
31
 
from alembic import op
32
 
import sqlalchemy as sa
33
 
 
34
 
 
35
 
INDEXES = (
36
 
    # ([dialects], table_name, index_name, create/delete, uniq/not_uniq,
37
 
    # length_limited)
38
 
    (['mysql', 'sqlite', 'postgresql'],
39
 
     'resource',
40
 
     'resource_user_id_project_id_key',
41
 
     ('user_id', 'project_id'), True, False, True),
42
 
    (['mysql'], 'source', 'id', ('id',), False, True, False))
43
 
 
44
 
 
45
 
def index_cleanup(engine_names, table_name, uniq_name, columns, create,
46
 
                  unique, limited):
47
 
    bind = op.get_bind()
48
 
    engine = bind.engine
49
 
    if engine.name not in engine_names:
50
 
        return
51
 
    if create:
52
 
        if limited and engine.name == 'mysql':
53
 
            # For some versions of mysql we can get an error
54
 
            # "Specified key was too long; max key length is 1000 bytes".
55
 
            # We should create an index by hand in this case with limited
56
 
            # length of columns.
57
 
            meta = sa.MetaData()
58
 
            meta.bind = engine
59
 
            table = sa.Table(table_name, meta, autoload=True)
60
 
            columns_mysql = ",".join((c + "(100)" for c in columns))
61
 
            sql = ("create index %s ON %s (%s)" % (uniq_name, table,
62
 
                                                   columns_mysql))
63
 
            engine.execute(sql)
64
 
        else:
65
 
            op.create_index(uniq_name, table_name, columns, unique=unique)
66
 
    else:
67
 
        if unique:
68
 
            op.drop_constraint(uniq_name, table_name, type='unique')
69
 
        else:
70
 
            op.drop_index(uniq_name, table_name=table_name)
71
 
 
72
 
 
73
 
def upgrade():
74
 
    for (engine_names, table_name, uniq_name, columns, create, uniq,
75
 
         limited) in INDEXES:
76
 
        index_cleanup(engine_names,
77
 
                      table_name,
78
 
                      uniq_name,
79
 
                      columns,
80
 
                      create,
81
 
                      uniq,
82
 
                      limited)
83
 
 
84
 
 
85
 
def downgrade():
86
 
    for (engine_names, table_name, uniq_name, columns, create, uniq,
87
 
         limited) in INDEXES:
88
 
        index_cleanup(engine_names,
89
 
                      table_name,
90
 
                      uniq_name,
91
 
                      columns,
92
 
                      not create,
93
 
                      uniq,
94
 
                      limited)