# Copyright 2016 OpenStack Foundation
#
#    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.
#

"""migrate_legacy_resources_to_db2

Revision ID: ffc7bbeec0b0
Revises: 8f376189b9eb
Create Date: 2016-04-14 15:57:13.072128

"""
import json

from alembic import op
import sqlalchemy as sa

from gnocchi.indexer import sqlalchemy_legacy_resources as legacy

# revision identifiers, used by Alembic.
revision = 'ffc7bbeec0b0'
down_revision = '8f376189b9eb'
branch_labels = None
depends_on = None


def upgrade():
    bind = op.get_bind()

    resource_type = sa.Table(
        'resource_type', sa.MetaData(),
        sa.Column('name', sa.String(255), nullable=False),
        sa.Column('tablename', sa.String(18), nullable=False),
        sa.Column('attributes', sa.Text, nullable=False)
    )

    resource_type_names = [rt.name for rt in
                           list(bind.execute(resource_type.select()))]

    for name, attributes in legacy.ceilometer_resources.items():
        if name in resource_type_names:
            continue
        tablename = legacy.ceilometer_tablenames.get(name, name)
        text_attributes = json.dumps(attributes)
        op.execute(resource_type.insert().values({
            resource_type.c.attributes: text_attributes,
            resource_type.c.name: name,
            resource_type.c.tablename: tablename,
        }))

Fix broken ceilometer resources migration script
Fix broken ceilometer resources migration script

Migration script 828c16f70cce have a bug, the following command doesn't
insert anything:

    op.execute(resource_type.insert().from_select(
           ['name'], sa.select([resource.c.type]).distinct()))

So all migration scripts that manipulates resource_type next, do not
update upgrade the resource_type for legacy ceilometer resources.

This change fixes that.

Change-Id: I5f8c47721c313eea3936132f3140793ec80ef3b1
