"Fossies" - the Fresh Open Source Software Archive

Member "cloudkitty-13.0.0/cloudkitty/rating/hash/db/sqlalchemy/alembic/versions/54cc17accf2c_fixed_constraint_name.py" (14 Oct 2020, 4200 Bytes) of package /linux/misc/openstack/cloudkitty-13.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 "54cc17accf2c_fixed_constraint_name.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 12.1.0_vs_13.0.0.

    1 #
    2 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    3 # not use this file except in compliance with the License. You may obtain
    4 # a copy of the License at
    5 #
    6 #      http://www.apache.org/licenses/LICENSE-2.0
    7 #
    8 # Unless required by applicable law or agreed to in writing, software
    9 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
   10 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   11 # License for the specific language governing permissions and limitations
   12 # under the License.
   13 
   14 """Fixed constraint name.
   15 
   16 Revision ID: 54cc17accf2c
   17 Revises: 4fa888fd7eda
   18 Create Date: 2015-05-28 16:44:32.936076
   19 
   20 """
   21 
   22 # revision identifiers, used by Alembic.
   23 revision = '54cc17accf2c'
   24 down_revision = '4fa888fd7eda'
   25 
   26 from alembic import op  # noqa: E402
   27 import sqlalchemy as sa  # noqa: E402
   28 
   29 
   30 def create_table(is_old=False):
   31     if is_old:
   32         constraints = ['uniq_field_mapping', 'uniq_service_mapping']
   33     else:
   34         constraints = ['uniq_field_threshold', 'uniq_service_threshold']
   35     table = op.create_table(
   36         'tmig_hashmap_thresholds',
   37         sa.Column('id', sa.Integer(), nullable=False),
   38         sa.Column('threshold_id', sa.String(length=36), nullable=False),
   39         sa.Column('level', sa.Numeric(precision=20, scale=8), nullable=True),
   40         sa.Column('cost', sa.Numeric(precision=20, scale=8), nullable=False),
   41         sa.Column(
   42             'map_type',
   43             sa.Enum('flat', 'rate', name='enum_map_type'),
   44             nullable=False),
   45         sa.Column('service_id', sa.Integer(), nullable=True),
   46         sa.Column('field_id', sa.Integer(), nullable=True),
   47         sa.Column('group_id', sa.Integer(), nullable=True),
   48         sa.ForeignKeyConstraint(
   49             ['field_id'],
   50             ['hashmap_fields.id'],
   51             ondelete='CASCADE'),
   52         sa.ForeignKeyConstraint(
   53             ['group_id'],
   54             ['hashmap_groups.id'],
   55             ondelete='SET NULL'),
   56         sa.ForeignKeyConstraint(
   57             ['service_id'],
   58             ['hashmap_services.id'],
   59             ondelete='CASCADE'),
   60         sa.PrimaryKeyConstraint('id'),
   61         sa.UniqueConstraint('threshold_id'),
   62         sa.UniqueConstraint('level', 'field_id', name=constraints[0]),
   63         sa.UniqueConstraint('level', 'service_id', name=constraints[1]))
   64     return table
   65 
   66 
   67 def upgrade():
   68     dialect = op.get_context().dialect.name
   69     try:
   70         # Needs sqlalchemy 0.8
   71         if dialect != 'postgresql':
   72             with op.batch_alter_table('hashmap_thresholds') as batch_op:
   73                 batch_op.drop_constraint(
   74                     u'uniq_field_mapping',
   75                     type_='unique')
   76                 batch_op.drop_constraint(
   77                     u'uniq_service_mapping',
   78                     type_='unique')
   79                 batch_op.create_unique_constraint(
   80                     'uniq_field_threshold',
   81                     ['level', 'field_id'])
   82                 batch_op.create_unique_constraint(
   83                     'uniq_service_threshold',
   84                     ['level', 'service_id'])
   85     except AttributeError:
   86         # No support for batch operations
   87         if dialect == 'sqlite':
   88             new_table = create_table()
   89             sel = sa.sql.expression.select(new_table.columns.keys())
   90             op.execute(
   91                 new_table.insert().from_select(
   92                     new_table.columns.keys(),
   93                     sel.select_from('hashmap_thresholds')))
   94             op.drop_table('hashmap_thresholds')
   95             op.rename_table('tmig_hashmap_thresholds', 'hashmap_thresholds')
   96         else:
   97             op.drop_constraint(
   98                 u'uniq_field_mapping',
   99                 'hashmap_thresholds',
  100                 type_='unique')
  101             op.drop_constraint(
  102                 u'uniq_service_mapping',
  103                 'hashmap_thresholds',
  104                 type_='unique')
  105             op.create_unique_constraint(
  106                 'uniq_field_threshold',
  107                 'hashmap_thresholds',
  108                 ['level', 'field_id'])
  109             op.create_unique_constraint(
  110                 'uniq_service_threshold',
  111                 'hashmap_thresholds',
  112                 ['level', 'service_id'])