"Fossies" - the Fresh Open Source Software Archive

Member "tacker-4.0.0/tacker/db/migration/alembic_migrations/versions/975e28392888_add_unique_key_vnf_package_vnfd.py" (14 Oct 2020, 2637 Bytes) of package /linux/misc/openstack/tacker-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "975e28392888_add_unique_key_vnf_package_vnfd.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 3.0.0_vs_4.0.0.

    1 # Copyright (C) 2020 NTT DATA
    2 # All Rights Reserved.
    3 #
    4 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    5 #    not use this file except in compliance with the License. You may obtain
    6 #    a copy of the License at
    7 #
    8 #         http://www.apache.org/licenses/LICENSE-2.0
    9 #
   10 #    Unless required by applicable law or agreed to in writing, software
   11 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   12 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   13 #    License for the specific language governing permissions and limitations
   14 #    under the License.
   15 #
   16 
   17 """Add unique constraints on vnfd_id,deleted in vnf_package_vnfd
   18 
   19 Revision ID: 975e28392888
   20 Revises: abbef484b34c
   21 Create Date: 2019-12-10 02:40:12.966027
   22 
   23 """
   24 
   25 # revision identifiers, used by Alembic.
   26 revision = '975e28392888'
   27 down_revision = 'abbef484b34c'
   28 
   29 from alembic import op  # noqa: E402
   30 import sqlalchemy as sa  # noqa: E402
   31 from sqlalchemy.engine import reflection  # noqa: E402
   32 
   33 
   34 def _migrate_duplicate_vnf_package_vnfd_id(table):
   35 
   36     meta = sa.MetaData(bind=op.get_bind())
   37     t = sa.Table(table, meta, autoload=True)
   38 
   39     session = sa.orm.Session(bind=op.get_bind())
   40     with session.begin(subtransactions=True):
   41         dup_vnfd_ids = session.query(t.c.vnfd_id).group_by(
   42             t.c.vnfd_id).having(sa.func.count() > 1).all()
   43         if dup_vnfd_ids:
   44             for vnfd_id in dup_vnfd_ids:
   45                 duplicate_obj_query = session.query(t).filter(
   46                     t.c.vnfd_id == vnfd_id[0]).all()
   47                 for dup_obj in duplicate_obj_query:
   48                     if dup_obj.deleted == '1':
   49                         session.execute(t.update().where(
   50                             t.c.id == dup_obj.id).values(deleted=dup_obj.id))
   51     session.commit()
   52 
   53     op.create_unique_constraint(
   54         constraint_name='uniq_%s0vnfd_id0deleted' % table,
   55         table_name=table,
   56         columns=['vnfd_id', 'deleted'])
   57 
   58 
   59 def upgrade(active_plugins=None, options=None):
   60     check_constraints = (reflection.Inspector.from_engine(op.get_bind())
   61                          .get_check_constraints('vnf_package_vnfd'))
   62     for constraint in check_constraints:
   63         if '`deleted`' in constraint['sqltext']:
   64             op.drop_constraint(
   65                 constraint_name=constraint['name'],
   66                 table_name='vnf_package_vnfd',
   67                 type_="check"
   68             )
   69             break
   70 
   71     op.alter_column('vnf_package_vnfd',
   72                     'deleted',
   73                     type_=sa.String(36), default="0")
   74     _migrate_duplicate_vnf_package_vnfd_id('vnf_package_vnfd')