PageRenderTime 27ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/galaxy/model/migrate/versions/0060_history_archive_import.py

https://bitbucket.org/cistrome/cistrome-harvard/
Python | 73 lines | 48 code | 14 blank | 11 comment | 3 complexity | aab7c98ef72a52e790eb8b383fc20615 MD5 | raw file
 1"""
 2Migration script to create column and table for importing histories from
 3file archives.
 4"""
 5
 6from sqlalchemy import *
 7from sqlalchemy.orm import *
 8from migrate import *
 9from migrate.changeset import *
10
11import logging
12log = logging.getLogger( __name__ )
13
14metadata = MetaData()
15
16# Columns to add.
17
18importing_col = Column( "importing", Boolean, index=True, default=False )
19ldda_parent_col = Column( "ldda_parent_id", Integer, ForeignKey( "library_dataset_dataset_association.id" ), index=True )
20
21# Table to add.
22
23JobImportHistoryArchive_table = Table( "job_import_history_archive", metadata,
24    Column( "id", Integer, primary_key=True ),
25    Column( "job_id", Integer, ForeignKey( "job.id" ), index=True ),
26    Column( "history_id", Integer, ForeignKey( "history.id" ), index=True ),
27    Column( "archive_dir", TEXT )
28    )
29
30def upgrade(migrate_engine):
31    metadata.bind = migrate_engine
32    print __doc__
33    metadata.reflect()
34
35    # Add column to history table and initialize.
36    try:
37        History_table = Table( "history", metadata, autoload=True )
38        importing_col.create( History_table, index_name="ix_history_importing")
39        assert importing_col is History_table.c.importing
40
41        # Initialize column to false.
42        if migrate_engine.name == 'mysql' or migrate_engine.name == 'sqlite':
43            default_false = "0"
44        elif migrate_engine.name in ['postgres', 'postgresql']:
45            default_false = "false"
46        migrate_engine.execute( "UPDATE history SET importing=%s" % default_false )
47    except Exception, e:
48        print str(e)
49        log.debug( "Adding column 'importing' to history table failed: %s" % str( e ) )
50
51    # Create job_import_history_archive table.
52    try:
53        JobImportHistoryArchive_table.create()
54    except Exception, e:
55        log.debug( "Creating job_import_history_archive table failed: %s" % str( e ) )
56
57def downgrade(migrate_engine):
58    metadata.bind = migrate_engine
59    metadata.reflect()
60
61    # Drop 'importing' column from history table.
62    try:
63        History_table = Table( "history", metadata, autoload=True )
64        importing_col = History_table.c.importing
65        importing_col.drop()
66    except Exception, e:
67        log.debug( "Dropping column 'importing' from history table failed: %s" % ( str( e ) ) )
68
69    # Drop job_import_history_archive table.
70    try:
71        JobImportHistoryArchive_table.drop()
72    except Exception, e:
73        log.debug( "Dropping job_import_history_archive table failed: %s" % str( e ) )