PageRenderTime 39ms CodeModel.GetById 34ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/galaxy/model/migrate/versions/0062_user_openid_table.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 60 lines | 42 code | 11 blank | 7 comment | 1 complexity | fbf6d38fc18a236f1cea8832e257abaf MD5 | raw file
 1"""
 2Migration script to create table for associating sessions and users with
 3OpenIDs.
 4"""
 5
 6from sqlalchemy import *
 7from sqlalchemy.orm import *
 8from migrate import *
 9from migrate.changeset import *
10
11import datetime
12now = datetime.datetime.utcnow
13
14import logging
15log = logging.getLogger( __name__ )
16
17metadata = MetaData()
18
19# Table to add
20
21UserOpenID_table = Table( "galaxy_user_openid", metadata,
22    Column( "id", Integer, primary_key=True ),
23    Column( "create_time", DateTime, default=now ),
24    Column( "update_time", DateTime, index=True, default=now, onupdate=now ),
25    Column( "session_id", Integer, ForeignKey( "galaxy_session.id" ), index=True ),
26    Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
27    Column( "openid", TEXT ),
28    )
29
30def upgrade(migrate_engine):
31    metadata.bind = migrate_engine
32    print __doc__
33    metadata.reflect()
34
35    # Create galaxy_user_openid table
36    try:
37        UserOpenID_table.create()
38    except Exception, e:
39        log.debug( "Creating galaxy_user_openid table failed: %s" % str( e ) )
40
41    ix_name = 'ix_galaxy_user_openid_openid'
42    if migrate_engine.name == 'mysql':
43        i = "ALTER TABLE galaxy_user_openid ADD UNIQUE INDEX ( openid( 255 ) )"
44        migrate_engine.execute( i )
45    else:
46        i = Index( ix_name, UserOpenID_table.c.openid, unique=True )
47        try:
48            i.create()
49        except Exception, e:
50            log.debug( "Adding index '%s' failed: %s" % ( ix_name, str( e ) ) )
51
52def downgrade(migrate_engine):
53    metadata.bind = migrate_engine
54    metadata.reflect()
55
56    # Drop galaxy_user_openid table
57    try:
58        UserOpenID_table.drop()
59    except Exception, e:
60        log.debug( "Dropping galaxy_user_openid table failed: %s" % str( e ) )