PageRenderTime 8ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/galaxy/model/migrate/versions/0064_add_run_and_sample_run_association_tables.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 68 lines | 55 code | 9 blank | 4 comment | 0 complexity | f4a9139b5fab0c9a0d6d450aac30eca1 MD5 | raw file
 1"""
 2Migration script to add the run and sample_run_association tables.
 3"""
 4
 5from sqlalchemy import *
 6from migrate import *
 7from migrate.changeset import *
 8from galaxy.model.custom_types import *
 9
10import datetime
11now = datetime.datetime.utcnow
12
13import logging
14log = logging.getLogger( __name__ )
15
16metadata = MetaData()
17
18Run_table = Table( "run", metadata,
19    Column( "id", Integer, primary_key=True ),
20    Column( "create_time", DateTime, default=now ),
21    Column( "update_time", DateTime, default=now, onupdate=now ),
22    Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ),
23    Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ),
24    Column( "deleted", Boolean, index=True, default=False ) )
25
26RequestTypeRunAssociation_table = Table( "request_type_run_association", metadata,
27    Column( "id", Integer, primary_key=True ),
28    Column( "request_type_id", Integer, ForeignKey( "request_type.id" ), index=True, nullable=False ),
29    Column( "run_id", Integer, ForeignKey( "run.id" ), index=True, nullable=False ) )
30
31SampleRunAssociation_table = Table( "sample_run_association", metadata,
32    Column( "id", Integer, primary_key=True ),
33    Column( "sample_id", Integer, ForeignKey( "sample.id" ), index=True, nullable=False ),
34    Column( "run_id", Integer, ForeignKey( "run.id" ), index=True, nullable=False ) )
35
36def upgrade(migrate_engine):
37    metadata.bind = migrate_engine
38    print __doc__
39    metadata.reflect()
40    try:
41        Run_table.create()
42    except Exception, e:
43        log.debug( "Creating Run_table table failed: %s" % str( e ) )
44    try:
45        RequestTypeRunAssociation_table.create()
46    except Exception, e:
47        log.debug( "Creating RequestTypeRunAssociation table failed: %s" % str( e ) )
48    try:
49        SampleRunAssociation_table.create()
50    except Exception, e:
51        log.debug( "Creating SampleRunAssociation table failed: %s" % str( e ) )
52
53def downgrade(migrate_engine):
54    metadata.bind = migrate_engine
55    # Load existing tables
56    metadata.reflect()
57    try:
58        SampleRunAssociation_table.drop()
59    except Exception, e:
60        log.debug( "Dropping SampleRunAssociation table failed: %s" % str( e ) )
61    try:
62        RequestTypeRunAssociation_table.drop()
63    except Exception, e:
64        log.debug( "Dropping RequestTypeRunAssociation table failed: %s" % str( e ) )
65    try:
66        Run_table.drop()
67    except Exception, e:
68        log.debug( "Dropping Run_table table failed: %s" % str( e ) )