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
 
   5 #      http://www.apache.org/licenses/LICENSE-2.0
 
   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
 
  13 import sqlalchemy as sql
 
  15 _SP_TABLE_NAME = 'service_provider'
 
  18 def _update_null_columns(migrate_engine, sp_table):
 
  19     stmt = (sp_table.update().
 
  20             where(sp_table.c.auth_url.is_(None)).
 
  22     migrate_engine.execute(stmt)
 
  24     stmt = (sp_table.update().
 
  25             where(sp_table.c.sp_url.is_(None)).
 
  27     migrate_engine.execute(stmt)
 
  30 def upgrade(migrate_engine):
 
  32     meta.bind = migrate_engine
 
  33     sp_table = sql.Table(_SP_TABLE_NAME, meta, autoload=True)
 
  34     # The columns are being changed to non-nullable. To prevent
 
  35     # database errors when both are altered, all the existing
 
  36     # null-records should be filled with not null values.
 
  37     _update_null_columns(migrate_engine, sp_table)
 
  39     sp_table.c.auth_url.alter(nullable=False)
 
  40     sp_table.c.sp_url.alter(nullable=False)