"Fossies" - the Fresh Open Source Software Archive

Member "nova-19.0.1/nova/db/sqlalchemy/api_migrations/migrate_repo/versions/044_placement_add_projects_users.py" (6 Jun 2019, 2650 Bytes) of package /linux/misc/openstack/nova-19.0.1.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 "044_placement_add_projects_users.py" see the Fossies "Dox" file reference documentation.

    1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 #    not use this file except in compliance with the License. You may obtain
    3 #    a copy of the License at
    4 #
    5 #         http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 #    Unless required by applicable law or agreed to in writing, software
    8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 #    License for the specific language governing permissions and limitations
   11 #    under the License.
   12 
   13 """Streamlines consumers table and adds projects and users table"""
   14 
   15 from migrate import UniqueConstraint
   16 from sqlalchemy import Column
   17 from sqlalchemy import DateTime
   18 from sqlalchemy import Integer
   19 from sqlalchemy import MetaData
   20 from sqlalchemy import String
   21 from sqlalchemy import Table
   22 
   23 
   24 def upgrade(migrate_engine):
   25     meta = MetaData()
   26     meta.bind = migrate_engine
   27 
   28     projects = Table('projects', meta,
   29         Column('id', Integer, primary_key=True, nullable=False,
   30                autoincrement=True),
   31         Column('external_id', String(length=255), nullable=False),
   32         Column('created_at', DateTime),
   33         Column('updated_at', DateTime),
   34         UniqueConstraint('external_id', name='uniq_projects0external_id'),
   35         mysql_engine='InnoDB',
   36         mysql_charset='latin1'
   37     )
   38 
   39     projects.create(checkfirst=True)
   40 
   41     users = Table('users', meta,
   42         Column('id', Integer, primary_key=True, nullable=False,
   43                autoincrement=True),
   44         Column('external_id', String(length=255), nullable=False),
   45         Column('created_at', DateTime),
   46         Column('updated_at', DateTime),
   47         UniqueConstraint('external_id', name='uniq_users0external_id'),
   48         mysql_engine='InnoDB',
   49         mysql_charset='latin1'
   50     )
   51 
   52     users.create(checkfirst=True)
   53 
   54     consumers = Table('consumers', meta, autoload=True)
   55     project_id_col = consumers.c.project_id
   56     user_id_col = consumers.c.user_id
   57 
   58     # NOTE(jaypipes): For PostgreSQL, we can't do col.alter(type=Integer)
   59     # because NVARCHAR and INTEGER are not compatible, so we need to do this
   60     # manual ALTER TABLE ... USING approach.
   61     if migrate_engine.name == 'postgresql':
   62         migrate_engine.execute(
   63             "ALTER TABLE consumers ALTER COLUMN project_id "
   64             "TYPE INTEGER USING project_id::integer"
   65         )
   66         migrate_engine.execute(
   67             "ALTER TABLE consumers ALTER COLUMN user_id "
   68             "TYPE INTEGER USING user_id::integer"
   69         )
   70     else:
   71         project_id_col.alter(type=Integer)
   72         user_id_col.alter(type=Integer)