"Fossies" - the Fresh Open Source Software Archive

Member "keystone-17.0.0/keystone/common/sql/contract_repo/versions/014_contract_add_domain_id_to_user_table.py" (13 May 2020, 4297 Bytes) of package /linux/misc/openstack/keystone-17.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 "014_contract_add_domain_id_to_user_table.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 16.0.1_vs_17.0.0.

    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 import migrate
   14 import sqlalchemy as sql
   15 
   16 from keystone.common.sql import upgrades
   17 
   18 
   19 def upgrade(migrate_engine):
   20     meta = sql.MetaData()
   21     meta.bind = migrate_engine
   22     inspector = sql.inspect(migrate_engine)
   23 
   24     user = sql.Table('user', meta, autoload=True)
   25     local_user = sql.Table('local_user', meta, autoload=True)
   26     nonlocal_user = sql.Table('nonlocal_user', meta, autoload=True)
   27 
   28     # drop previous fk constraints
   29     fk_name = _get_fk_name(inspector, 'local_user', 'user_id')
   30     if fk_name:
   31         migrate.ForeignKeyConstraint(columns=[local_user.c.user_id],
   32                                      refcolumns=[user.c.id],
   33                                      name=fk_name).drop()
   34 
   35     fk_name = _get_fk_name(inspector, 'nonlocal_user', 'user_id')
   36     if fk_name:
   37         migrate.ForeignKeyConstraint(columns=[nonlocal_user.c.user_id],
   38                                      refcolumns=[user.c.id],
   39                                      name=fk_name).drop()
   40 
   41     # create user unique constraint needed for the new composite fk constraint
   42     migrate.UniqueConstraint(user.c.id, user.c.domain_id,
   43                              name='ixu_user_id_domain_id').create()
   44     # create new composite fk constraints
   45     migrate.ForeignKeyConstraint(
   46         columns=[local_user.c.user_id, local_user.c.domain_id],
   47         refcolumns=[user.c.id, user.c.domain_id],
   48         onupdate='CASCADE', ondelete='CASCADE').create()
   49     migrate.ForeignKeyConstraint(
   50         columns=[nonlocal_user.c.user_id, nonlocal_user.c.domain_id],
   51         refcolumns=[user.c.id, user.c.domain_id],
   52         onupdate='CASCADE', ondelete='CASCADE').create()
   53 
   54     # drop triggers
   55     if upgrades.USE_TRIGGERS:
   56         if migrate_engine.name == 'postgresql':
   57             drop_local_user_insert_trigger = (
   58                 'DROP TRIGGER local_user_after_insert_trigger on local_user;')
   59             drop_local_user_update_trigger = (
   60                 'DROP TRIGGER local_user_after_update_trigger on local_user;')
   61             drop_nonlocal_user_insert_trigger = (
   62                 'DROP TRIGGER nonlocal_user_after_insert_trigger '
   63                 'on nonlocal_user;')
   64             drop_nonlocal_user_update_trigger = (
   65                 'DROP TRIGGER nonlocal_user_after_update_trigger '
   66                 'on nonlocal_user;')
   67         elif migrate_engine.name == 'mysql':
   68             drop_local_user_insert_trigger = (
   69                 'DROP TRIGGER local_user_after_insert_trigger;')
   70             drop_local_user_update_trigger = (
   71                 'DROP TRIGGER local_user_after_update_trigger;')
   72             drop_nonlocal_user_insert_trigger = (
   73                 'DROP TRIGGER nonlocal_user_after_insert_trigger;')
   74             drop_nonlocal_user_update_trigger = (
   75                 'DROP TRIGGER nonlocal_user_after_update_trigger;')
   76         else:
   77             drop_local_user_insert_trigger = (
   78                 'DROP TRIGGER IF EXISTS local_user_after_insert_trigger;')
   79             drop_local_user_update_trigger = (
   80                 'DROP TRIGGER IF EXISTS local_user_after_update_trigger;')
   81             drop_nonlocal_user_insert_trigger = (
   82                 'DROP TRIGGER IF EXISTS nonlocal_user_after_insert_trigger;')
   83             drop_nonlocal_user_update_trigger = (
   84                 'DROP TRIGGER IF EXISTS nonlocal_user_after_update_trigger;')
   85         migrate_engine.execute(drop_local_user_insert_trigger)
   86         migrate_engine.execute(drop_local_user_update_trigger)
   87         migrate_engine.execute(drop_nonlocal_user_insert_trigger)
   88         migrate_engine.execute(drop_nonlocal_user_update_trigger)
   89 
   90 
   91 def _get_fk_name(inspector, table, fk_column):
   92     for fk in inspector.get_foreign_keys(table):
   93         if fk_column in fk['constrained_columns']:
   94             return fk['name']