PageRenderTime 69ms CodeModel.GetById 57ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0025_user_info.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 63 lines | 53 code | 3 blank | 7 comment | 7 complexity | 9b508a5536a100581df260194550655f MD5 | raw file
 1"""
 2This script adds a foreign key to the form_values table in the galaxy_user table
 3"""
 4from sqlalchemy import *
 5from sqlalchemy.orm import *
 6from migrate import *
 7from migrate.changeset import *
 8import datetime
 9now = datetime.datetime.utcnow
10import sys, logging
11# Need our custom types, but don't import anything else from model
12from galaxy.model.custom_types import *
13
14log = logging.getLogger( __name__ )
15log.setLevel(logging.DEBUG)
16handler = logging.StreamHandler( sys.stdout )
17format = "%(name)s %(levelname)s %(asctime)s %(message)s"
18formatter = logging.Formatter( format )
19handler.setFormatter( formatter )
20log.addHandler( handler )
21
22metadata = MetaData()
23
24def display_migration_details():
25    print "========================================"
26    print "This script adds a foreign key to the form_values table in the galaxy_user table"
27    print "========================================"
28def upgrade(migrate_engine):
29    metadata.bind = migrate_engine
30    display_migration_details()
31    # Load existing tables
32    metadata.reflect()
33    try:
34        User_table = Table( "galaxy_user", metadata, autoload=True )
35    except NoSuchTableError:
36        User_table = None
37        log.debug( "Failed loading table galaxy_user" )
38    if User_table is not None:
39        try:
40            col = Column( "form_values_id", Integer, index=True )
41            col.create( User_table, index_name='ix_user_form_values_id')
42            assert col is User_table.c.form_values_id
43        except Exception, e:
44            log.debug( "Adding column 'form_values_id' to galaxy_user table failed: %s" % ( str( e ) ) )
45        try:
46            FormValues_table = Table( "form_values", metadata, autoload=True )
47        except NoSuchTableError:
48            FormValues_table = None
49            log.debug( "Failed loading table form_values" )
50        if migrate_engine.name != 'sqlite':
51            # Add 1 foreign key constraint to the form_values table
52            if User_table is not None and FormValues_table is not None:
53                try:
54                    cons = ForeignKeyConstraint( [User_table.c.form_values_id],
55                                                 [FormValues_table.c.id],
56                                                 name='user_form_values_id_fk' )
57                    # Create the constraint
58                    cons.create()
59                except Exception, e:
60                    log.debug( "Adding foreign key constraint 'user_form_values_id_fk' to table 'galaxy_user' failed: %s" % ( str( e ) ) )
61def downgrade(migrate_engine):
62    metadata.bind = migrate_engine
63    pass