7a75f7b193f841529e317048cd921e72fc84964c
[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
16 def upgrade(migrate_engine):
17     meta = sql.MetaData()
18     meta.bind = migrate_engine
19
20     # You can specify primary keys when creating tables, however adding
21     # auto-increment integer primary keys for existing tables is not
22     # cross-engine compatibility supported. Thus, the approach is to:
23     # (1) create a new revocation_event table with an int pkey,
24     # (2) migrate data from the old table to the new table,
25     # (3) delete the old revocation_event table
26     # (4) rename the new revocation_event table
27     revocation_table = sql.Table('revocation_event', meta, autoload=True)
28
29     revocation_table_new = sql.Table(
30         'revocation_event_new',
31         meta,
32         sql.Column('id', sql.Integer, primary_key=True),
33         sql.Column('domain_id', sql.String(64)),
34         sql.Column('project_id', sql.String(64)),
35         sql.Column('user_id', sql.String(64)),
36         sql.Column('role_id', sql.String(64)),
37         sql.Column('trust_id', sql.String(64)),
38         sql.Column('consumer_id', sql.String(64)),
39         sql.Column('access_token_id', sql.String(64)),
40         sql.Column('issued_before', sql.DateTime(), nullable=False),
41         sql.Column('expires_at', sql.DateTime()),
42         sql.Column('revoked_at', sql.DateTime(), index=True, nullable=False),
43         sql.Column('audit_id', sql.String(32), nullable=True),
44         sql.Column('audit_chain_id', sql.String(32), nullable=True))
45     revocation_table_new.create(migrate_engine, checkfirst=True)
46
47     revocation_table_new.insert().from_select(['domain_id',
48                                                'project_id',
49                                                'user_id',
50                                                'role_id',
51                                                'trust_id',
52                                                'consumer_id',
53                                                'access_token_id',
54                                                'issued_before',
55                                                'expires_at',
56                                                'revoked_at',
57                                                'audit_id',
58                                                'audit_chain_id'],
59                                               revocation_table.select())
60
61     revocation_table.drop()
62     revocation_table_new.rename('revocation_event')