dc18f5482af4b54b699f79b0d21a82e29aefdf15
[moon.git] /
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 sqlalchemy as sql
14
15 _SP_TABLE_NAME = 'service_provider'
16
17
18 def _update_null_columns(migrate_engine, sp_table):
19     stmt = (sp_table.update().
20             where(sp_table.c.auth_url.is_(None)).
21             values(auth_url=''))
22     migrate_engine.execute(stmt)
23
24     stmt = (sp_table.update().
25             where(sp_table.c.sp_url.is_(None)).
26             values(sp_url=''))
27     migrate_engine.execute(stmt)
28
29
30 def upgrade(migrate_engine):
31     meta = sql.MetaData()
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)
38
39     sp_table.c.auth_url.alter(nullable=False)
40     sp_table.c.sp_url.alter(nullable=False)