1 | from sqlalchemy import * |
---|
2 | from sqlalchemy.orm import * |
---|
3 | from sqlalchemy.exc import * |
---|
4 | from migrate import * |
---|
5 | from migrate.changeset import * |
---|
6 | import datetime |
---|
7 | now = datetime.datetime.utcnow |
---|
8 | import sys, logging |
---|
9 | # Need our custom types, but don't import anything else from model |
---|
10 | from galaxy.model.custom_types import * |
---|
11 | |
---|
12 | log = logging.getLogger( __name__ ) |
---|
13 | log.setLevel(logging.DEBUG) |
---|
14 | handler = logging.StreamHandler( sys.stdout ) |
---|
15 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
16 | formatter = logging.Formatter( format ) |
---|
17 | handler.setFormatter( formatter ) |
---|
18 | log.addHandler( handler ) |
---|
19 | |
---|
20 | metadata = MetaData( migrate_engine ) |
---|
21 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
22 | |
---|
23 | def display_migration_details(): |
---|
24 | print "========================================" |
---|
25 | print """This script creates a request.folder_id column which is a foreign |
---|
26 | key to the library_folder table. This also adds a 'type' and 'layout' column |
---|
27 | to the form_definition table.""" |
---|
28 | print "========================================" |
---|
29 | |
---|
30 | def upgrade(): |
---|
31 | display_migration_details() |
---|
32 | # Load existing tables |
---|
33 | metadata.reflect() |
---|
34 | # Create the folder_id column |
---|
35 | try: |
---|
36 | Request_table = Table( "request", metadata, autoload=True ) |
---|
37 | except NoSuchTableError: |
---|
38 | Request_table = None |
---|
39 | log.debug( "Failed loading table request" ) |
---|
40 | if Request_table: |
---|
41 | try: |
---|
42 | col = Column( "folder_id", Integer, index=True ) |
---|
43 | col.create( Request_table ) |
---|
44 | assert col is Request_table.c.folder_id |
---|
45 | except Exception, e: |
---|
46 | log.debug( "Adding column 'folder_id' to request table failed: %s" % ( str( e ) ) ) |
---|
47 | try: |
---|
48 | LibraryFolder_table = Table( "library_folder", metadata, autoload=True ) |
---|
49 | except NoSuchTableError: |
---|
50 | LibraryFolder_table = None |
---|
51 | log.debug( "Failed loading table library_folder" ) |
---|
52 | # Add 1 foreign key constraint to the library_folder table |
---|
53 | if Request_table and LibraryFolder_table: |
---|
54 | try: |
---|
55 | cons = ForeignKeyConstraint( [Request_table.c.folder_id], |
---|
56 | [LibraryFolder_table.c.id], |
---|
57 | name='request_folder_id_fk' ) |
---|
58 | # Create the constraint |
---|
59 | cons.create() |
---|
60 | except Exception, e: |
---|
61 | log.debug( "Adding foreign key constraint 'request_folder_id_fk' to table 'library_folder' failed: %s" % ( str( e ) ) ) |
---|
62 | # Create the type column in form_definition |
---|
63 | try: |
---|
64 | FormDefinition_table = Table( "form_definition", metadata, autoload=True ) |
---|
65 | except NoSuchTableError: |
---|
66 | FormDefinition_table = None |
---|
67 | log.debug( "Failed loading table form_definition" ) |
---|
68 | if FormDefinition_table: |
---|
69 | try: |
---|
70 | col = Column( "type", TrimmedString( 255 ), index=True ) |
---|
71 | col.create( FormDefinition_table ) |
---|
72 | assert col is FormDefinition_table.c.type |
---|
73 | except Exception, e: |
---|
74 | log.debug( "Adding column 'type' to form_definition table failed: %s" % ( str( e ) ) ) |
---|
75 | try: |
---|
76 | col = Column( "layout", JSONType()) |
---|
77 | col.create( FormDefinition_table ) |
---|
78 | assert col is FormDefinition_table.c.layout |
---|
79 | except Exception, e: |
---|
80 | log.debug( "Adding column 'layout' to form_definition table failed: %s" % ( str( e ) ) ) |
---|
81 | |
---|
82 | |
---|
83 | def downgrade(): |
---|
84 | pass |
---|