1 | """ |
---|
2 | Add a state column to the history_dataset_association and library_dataset_dataset_association table. |
---|
3 | """ |
---|
4 | |
---|
5 | from sqlalchemy import * |
---|
6 | from sqlalchemy.orm import * |
---|
7 | from sqlalchemy.exc import * |
---|
8 | from migrate import * |
---|
9 | from migrate.changeset import * |
---|
10 | from galaxy.model.custom_types import * |
---|
11 | |
---|
12 | import datetime |
---|
13 | now = datetime.datetime.utcnow |
---|
14 | |
---|
15 | import sys, logging |
---|
16 | log = logging.getLogger( __name__ ) |
---|
17 | log.setLevel(logging.DEBUG) |
---|
18 | handler = logging.StreamHandler( sys.stdout ) |
---|
19 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
20 | formatter = logging.Formatter( format ) |
---|
21 | handler.setFormatter( formatter ) |
---|
22 | log.addHandler( handler ) |
---|
23 | |
---|
24 | metadata = MetaData( migrate_engine ) |
---|
25 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
26 | |
---|
27 | DATASET_INSTANCE_TABLE_NAMES = [ 'history_dataset_association', 'library_dataset_dataset_association' ] |
---|
28 | |
---|
29 | def upgrade(): |
---|
30 | print __doc__ |
---|
31 | metadata.reflect() |
---|
32 | dataset_instance_tables = [] |
---|
33 | for table_name in DATASET_INSTANCE_TABLE_NAMES: |
---|
34 | try: |
---|
35 | dataset_instance_tables.append( ( table_name, Table( table_name, metadata, autoload=True ) ) ) |
---|
36 | except NoSuchTableError: |
---|
37 | log.debug( "Failed loading table %s" % table_name ) |
---|
38 | if dataset_instance_tables: |
---|
39 | for table_name, dataset_instance_table in dataset_instance_tables: |
---|
40 | try: |
---|
41 | col = Column( "state", TrimmedString( 64 ), index=True, nullable=True ) |
---|
42 | col.create( dataset_instance_table ) |
---|
43 | assert col is dataset_instance_table.c.state |
---|
44 | except Exception, e: |
---|
45 | log.debug( "Adding column 'state' to %s table failed: %s" % ( table_name, str( e ) ) ) |
---|
46 | try: |
---|
47 | i = Index( "ix_%s_state" % table_name, dataset_instance_table.c.state ) |
---|
48 | i.create() |
---|
49 | except Exception, e: |
---|
50 | log.debug( "Adding index 'ix_%s_state' failed: %s" % ( table_name, str( e ) ) ) |
---|
51 | def downgrade(): |
---|
52 | metadata.reflect() |
---|
53 | dataset_instance_tables = [] |
---|
54 | for table_name in DATASET_INSTANCE_TABLE_NAMES: |
---|
55 | try: |
---|
56 | dataset_instance_tables.append( ( table_name, Table( table_name, metadata, autoload=True ) ) ) |
---|
57 | except NoSuchTableError: |
---|
58 | log.debug( "Failed loading table %s" % table_name ) |
---|
59 | if dataset_instance_tables: |
---|
60 | for table_name, dataset_instance_table in dataset_instance_tables: |
---|
61 | try: |
---|
62 | col = dataset_instance_table.c.state |
---|
63 | col.drop() |
---|
64 | except Exception, e: |
---|
65 | log.debug( "Dropping column 'state' from %s table failed: %s" % ( table_name, str( e ) ) ) |
---|