summaryrefslogtreecommitdiff
path: root/glance/db/sqlalchemy/alembic_migrations/versions/ocata01_add_visibility_remove_is_public.py
blob: 5d66513e3612d48d0c45fdd5e2f1017d6cd301bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""add visibility to and remove is_public from images

Revision ID: ocata01
Revises: mitaka02
Create Date: 2017-01-20 12:58:16.647499

"""

import os

from alembic import op
from sqlalchemy import Column, Enum, MetaData, select, Table, not_, and_
import sqlparse

# revision identifiers, used by Alembic.
revision = 'ocata01'
down_revision = 'mitaka02'
branch_labels = None
depends_on = None


def upgrade():
    migrate_engine = op.get_bind()
    meta = MetaData(bind=migrate_engine)

    engine_name = migrate_engine.engine.name
    if engine_name == 'sqlite':
        sql_file = os.path.splitext(__file__)[0]
        sql_file += '.sql'
        with open(sql_file, 'r') as sqlite_script:
            sql = sqlparse.format(sqlite_script.read(), strip_comments=True)
            for statement in sqlparse.split(sql):
                op.execute(statement)
        return

    enum = Enum('private', 'public', 'shared', 'community', metadata=meta,
                name='image_visibility')
    enum.create()
    v_col = Column('visibility', enum, nullable=False, server_default='shared')
    op.add_column('images', v_col)

    op.create_index('visibility_image_idx', 'images', ['visibility'])

    images = Table('images', meta, autoload=True)
    images.update(values={'visibility': 'public'}).where(
        images.c.is_public).execute()

    image_members = Table('image_members', meta, autoload=True)

    # NOTE(dharinic): Mark all the non-public images as 'private' first
    images.update().values(visibility='private').where(
        not_(images.c.is_public)).execute()
    # NOTE(dharinic): Identify 'shared' images from the above
    images.update().values(visibility='shared').where(and_(
        images.c.visibility == 'private', images.c.id.in_(select(
            [image_members.c.image_id]).distinct().where(
                not_(image_members.c.deleted))))).execute()

    op.drop_index('ix_images_is_public', 'images')
    op.drop_column('images', 'is_public')