# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2012 OpenStack LLC.
#
#    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.

from nova.openstack.common import log as logging
from sqlalchemy import Boolean, Column, DateTime, Integer
from sqlalchemy import MetaData, String, Table

LOG = logging.getLogger(__name__)


def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine;
    # bind migrate_engine to your metadata
    meta = MetaData()
    meta.bind = migrate_engine

    # Add 'user_id' column to quota_usages table.
    quota_usages = Table('quota_usages', meta, autoload=True)
    user_id = Column('user_id',
                     String(length=255, convert_unicode=False,
                            assert_unicode=None, unicode_error=None,
                            _warn_on_bytestring=False))
    quota_usages.create_column(user_id)

    # Add 'user_id' column to reservations table.
    reservations = Table('reservations', meta, autoload=True)
    user_id = Column('user_id',
                     String(length=255, convert_unicode=False,
                            assert_unicode=None, unicode_error=None,
                            _warn_on_bytestring=False))
    reservations.create_column(user_id)

    # New table.
    user_quotas = Table('user_quotas', meta,
                        Column('id', Integer(), primary_key=True),
                        Column('created_at', DateTime(timezone=False)),
                        Column('updated_at', DateTime(timezone=False)),
                        Column('deleted_at', DateTime(timezone=False)),
                        Column('deleted', Boolean(), default=False),
                        Column('user_id',
                               String(length=255, convert_unicode=False,
                                      assert_unicode=None, unicode_error=None,
                                      _warn_on_bytestring=False)),
                        Column('project_id',
                               String(length=255, convert_unicode=False,
                                      assert_unicode=None, unicode_error=None,
                                      _warn_on_bytestring=False)),
                        Column('resource',
                               String(length=255, convert_unicode=False,
                                      assert_unicode=None, unicode_error=None,
                                      _warn_on_bytestring=False),
                               nullable=False),
                        Column('hard_limit', Integer(), nullable=True),
                        mysql_engine='InnoDB',
                        mysql_charset='utf8',
                        )

    try:
        user_quotas.create()
    except Exception:
        LOG.error(_("Table |%s| not created!"), repr(user_quotas))
        raise


def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    meta = MetaData()
    meta.bind = migrate_engine

    quota_usages = Table('quota_usages', meta, autoload=True)
    quota_usages.drop_column('user_id')

    reservations = Table('reservations', meta, autoload=True)
    reservations.drop_column('user_id')

    user_quotas = Table('user_quotas', meta, autoload=True)
    try:
        user_quotas.drop()
    except Exception:
        LOG.error(_("user_quotas table not dropped"))
        raise
