PageRenderTime 26ms CodeModel.GetById 11ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0078_add_columns_for_disk_usage_accounting.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 88 lines | 87 code | 0 blank | 1 comment | 0 complexity | a1f7f3a64f603db4aca4be1b1642f14e MD5 | raw file
 1"""
 2Migration script to add 'total_size' column to the dataset table, 'purged'
 3column to the HDA table, and 'disk_usage' column to the User and GalaxySession
 4tables.
 5"""
 6
 7from sqlalchemy import *
 8from sqlalchemy.orm import *
 9from migrate import *
10from migrate.changeset import *
11
12import logging
13log = logging.getLogger( __name__ )
14
15metadata = MetaData()
16
17def upgrade(migrate_engine):
18    metadata.bind = migrate_engine
19    print __doc__
20    metadata.reflect()
21
22    try:
23        Dataset_table = Table( "dataset", metadata, autoload=True )
24        c = Column( 'total_size', Numeric( 15, 0 ) )
25        c.create( Dataset_table )
26        assert c is Dataset_table.c.total_size
27    except Exception, e:
28        print "Adding total_size column to dataset table failed: %s" % str( e )
29        log.debug( "Adding total_size column to dataset table failed: %s" % str( e ) )
30
31    try:
32        HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
33        c = Column( "purged", Boolean, index=True, default=False )
34        c.create( HistoryDatasetAssociation_table, index_name="ix_history_dataset_association_purged")
35        assert c is HistoryDatasetAssociation_table.c.purged
36        migrate_engine.execute(HistoryDatasetAssociation_table.update().values(purged=False))
37    except Exception, e:
38        print "Adding purged column to history_dataset_association table failed: %s" % str( e )
39        log.debug( "Adding purged column to history_dataset_association table failed: %s" % str( e ) )
40
41    try:
42        User_table = Table( "galaxy_user", metadata, autoload=True )
43        c = Column( 'disk_usage', Numeric( 15, 0 ), index=True )
44        c.create( User_table, index_name="ix_galaxy_user_disk_usage")
45        assert c is User_table.c.disk_usage
46    except Exception, e:
47        print "Adding disk_usage column to galaxy_user table failed: %s" % str( e )
48        log.debug( "Adding disk_usage column to galaxy_user table failed: %s" % str( e ) )
49
50    try:
51        GalaxySession_table = Table( "galaxy_session", metadata, autoload=True )
52        c = Column( 'disk_usage', Numeric( 15, 0 ), index=True )
53        c.create( GalaxySession_table, index_name="ix_galaxy_session_disk_usage")
54        assert c is GalaxySession_table.c.disk_usage
55    except Exception, e:
56        print "Adding disk_usage column to galaxy_session table failed: %s" % str( e )
57        log.debug( "Adding disk_usage column to galaxy_session table failed: %s" % str( e ) )
58
59def downgrade(migrate_engine):
60    metadata.bind = migrate_engine
61    metadata.reflect()
62    try:
63        Dataset_table = Table( "dataset", metadata, autoload=True )
64        Dataset_table.c.total_size.drop()
65    except Exception, e:
66        print "Dropping total_size column from dataset table failed: %s" % str( e )
67        log.debug( "Dropping total_size column from dataset table failed: %s" % str( e ) )
68
69    try:
70        HistoryDatasetAssociation_table = Table( "history_dataset_association", metadata, autoload=True )
71        HistoryDatasetAssociation_table.c.purged.drop()
72    except Exception, e:
73        print "Dropping purged column from history_dataset_association table failed: %s" % str( e )
74        log.debug( "Dropping purged column from history_dataset_association table failed: %s" % str( e ) )
75
76    try:
77        User_table = Table( "galaxy_user", metadata, autoload=True )
78        User_table.c.disk_usage.drop()
79    except Exception, e:
80        print "Dropping disk_usage column from galaxy_user table failed: %s" % str( e )
81        log.debug( "Dropping disk_usage column from galaxy_user table failed: %s" % str( e ) )
82
83    try:
84        GalaxySession_table = Table( "galaxy_session", metadata, autoload=True )
85        GalaxySession_table.c.disk_usage.drop()
86    except Exception, e:
87        print "Dropping disk_usage column from galaxy_session table failed: %s" % str( e )
88        log.debug( "Dropping disk_usage column from galaxy_session table failed: %s" % str( e ) )