1 | """ |
---|
2 | This migration script adds the following new tables for supporting Galaxy forms: |
---|
3 | 1) form_definition_current |
---|
4 | 2) form_definition |
---|
5 | 3) form_values |
---|
6 | 4) request_type |
---|
7 | 5) request |
---|
8 | 6) sample |
---|
9 | 7) sample_state |
---|
10 | 8) sample_event |
---|
11 | """ |
---|
12 | from sqlalchemy import * |
---|
13 | from sqlalchemy.orm import * |
---|
14 | from sqlalchemy.exc import * |
---|
15 | from migrate import * |
---|
16 | from migrate.changeset import * |
---|
17 | |
---|
18 | import datetime |
---|
19 | now = datetime.datetime.utcnow |
---|
20 | |
---|
21 | import sys, logging |
---|
22 | log = logging.getLogger( __name__ ) |
---|
23 | log.setLevel(logging.DEBUG) |
---|
24 | handler = logging.StreamHandler( sys.stdout ) |
---|
25 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
26 | formatter = logging.Formatter( format ) |
---|
27 | handler.setFormatter( formatter ) |
---|
28 | log.addHandler( handler ) |
---|
29 | |
---|
30 | # Need our custom types, but don't import anything else from model |
---|
31 | from galaxy.model.custom_types import * |
---|
32 | |
---|
33 | metadata = MetaData( migrate_engine ) |
---|
34 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
35 | |
---|
36 | def display_migration_details(): |
---|
37 | print "========================================" |
---|
38 | print "This migration script adds the following new tables for supporting Galaxy forms:" |
---|
39 | print "1) form_definition_current" |
---|
40 | print "2) form_definition" |
---|
41 | print "3) form_values" |
---|
42 | print "4) request_type" |
---|
43 | print "5) request" |
---|
44 | print "6) sample" |
---|
45 | print "7) sample_state" |
---|
46 | print "8) sample_event" |
---|
47 | print "========================================" |
---|
48 | |
---|
49 | FormDefinitionCurrent_table = Table('form_definition_current', metadata, |
---|
50 | Column( "id", Integer, primary_key=True), |
---|
51 | Column( "create_time", DateTime, default=now ), |
---|
52 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
53 | Column( "latest_form_id", Integer, index=True ), |
---|
54 | Column( "deleted", Boolean, index=True, default=False )) |
---|
55 | |
---|
56 | FormDefinition_table = Table('form_definition', metadata, |
---|
57 | Column( "id", Integer, primary_key=True), |
---|
58 | Column( "create_time", DateTime, default=now ), |
---|
59 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
60 | Column( "name", TrimmedString( 255 ), nullable=False ), |
---|
61 | Column( "desc", TEXT ), |
---|
62 | Column( "form_definition_current_id", Integer, ForeignKey( "form_definition_current.id" ), index=True, nullable=False ), |
---|
63 | Column( "fields", JSONType()) ) |
---|
64 | |
---|
65 | FormValues_table = Table('form_values', metadata, |
---|
66 | Column( "id", Integer, primary_key=True), |
---|
67 | Column( "create_time", DateTime, default=now ), |
---|
68 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
69 | Column( "form_definition_id", Integer, ForeignKey( "form_definition.id" ), index=True ), |
---|
70 | Column( "content", JSONType()) ) |
---|
71 | |
---|
72 | RequestType_table = Table('request_type', metadata, |
---|
73 | Column( "id", Integer, primary_key=True), |
---|
74 | Column( "create_time", DateTime, default=now ), |
---|
75 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
76 | Column( "name", TrimmedString( 255 ), nullable=False ), |
---|
77 | Column( "desc", TEXT ), |
---|
78 | Column( "request_form_id", Integer, ForeignKey( "form_definition.id" ), index=True ), |
---|
79 | Column( "sample_form_id", Integer, ForeignKey( "form_definition.id" ), index=True ) ) |
---|
80 | |
---|
81 | Request_table = Table('request', metadata, |
---|
82 | Column( "id", Integer, primary_key=True), |
---|
83 | Column( "create_time", DateTime, default=now ), |
---|
84 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
85 | Column( "name", TrimmedString( 255 ), nullable=False ), |
---|
86 | Column( "desc", TEXT ), |
---|
87 | Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ), |
---|
88 | Column( "request_type_id", Integer, ForeignKey( "request_type.id" ), index=True ), |
---|
89 | Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ), |
---|
90 | Column( "library_id", Integer, ForeignKey( "library.id" ), index=True ), |
---|
91 | Column( "deleted", Boolean, index=True, default=False ) ) |
---|
92 | |
---|
93 | Sample_table = Table('sample', metadata, |
---|
94 | Column( "id", Integer, primary_key=True), |
---|
95 | Column( "create_time", DateTime, default=now ), |
---|
96 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
97 | Column( "name", TrimmedString( 255 ), nullable=False ), |
---|
98 | Column( "desc", TEXT ), |
---|
99 | Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ), |
---|
100 | Column( "request_id", Integer, ForeignKey( "request.id" ), index=True ), |
---|
101 | Column( "deleted", Boolean, index=True, default=False ) ) |
---|
102 | |
---|
103 | SampleState_table = Table('sample_state', metadata, |
---|
104 | Column( "id", Integer, primary_key=True), |
---|
105 | Column( "create_time", DateTime, default=now ), |
---|
106 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
107 | Column( "name", TrimmedString( 255 ), nullable=False ), |
---|
108 | Column( "desc", TEXT ), |
---|
109 | Column( "request_type_id", Integer, ForeignKey( "request_type.id" ), index=True ) ) |
---|
110 | |
---|
111 | SampleEvent_table = Table('sample_event', metadata, |
---|
112 | Column( "id", Integer, primary_key=True), |
---|
113 | Column( "create_time", DateTime, default=now ), |
---|
114 | Column( "update_time", DateTime, default=now, onupdate=now ), |
---|
115 | Column( "sample_id", Integer, ForeignKey( "sample.id" ), index=True ), |
---|
116 | Column( "sample_state_id", Integer, ForeignKey( "sample_state.id" ), index=True ), |
---|
117 | Column( "comment", TEXT ) ) |
---|
118 | |
---|
119 | def upgrade(): |
---|
120 | display_migration_details() |
---|
121 | # Load existing tables |
---|
122 | metadata.reflect() |
---|
123 | # Add all of the new tables above |
---|
124 | # metadata.create_all() |
---|
125 | try: |
---|
126 | FormDefinitionCurrent_table.create() |
---|
127 | except Exception, e: |
---|
128 | log.debug( "Creating form_definition_current table failed: %s" % str( e ) ) |
---|
129 | try: |
---|
130 | FormDefinition_table.create() |
---|
131 | except Exception, e: |
---|
132 | log.debug( "Creating form_definition table failed: %s" % str( e ) ) |
---|
133 | # Add 1 foreign key constraint to the form_definition_current table |
---|
134 | if FormDefinitionCurrent_table and FormDefinition_table: |
---|
135 | try: |
---|
136 | cons = ForeignKeyConstraint( [FormDefinitionCurrent_table.c.latest_form_id], |
---|
137 | [FormDefinition_table.c.id], |
---|
138 | name='form_definition_current_latest_form_id_fk' ) |
---|
139 | # Create the constraint |
---|
140 | cons.create() |
---|
141 | except Exception, e: |
---|
142 | log.debug( "Adding foreign key constraint 'form_definition_current_latest_form_id_fk' to table 'form_definition_current' failed: %s" % ( str( e ) ) ) |
---|
143 | try: |
---|
144 | FormValues_table.create() |
---|
145 | except Exception, e: |
---|
146 | log.debug( "Creating form_values table failed: %s" % str( e ) ) |
---|
147 | try: |
---|
148 | RequestType_table.create() |
---|
149 | except Exception, e: |
---|
150 | log.debug( "Creating request_type table failed: %s" % str( e ) ) |
---|
151 | try: |
---|
152 | Request_table.create() |
---|
153 | except Exception, e: |
---|
154 | log.debug( "Creating request table failed: %s" % str( e ) ) |
---|
155 | try: |
---|
156 | Sample_table.create() |
---|
157 | except Exception, e: |
---|
158 | log.debug( "Creating sample table failed: %s" % str( e ) ) |
---|
159 | try: |
---|
160 | SampleState_table.create() |
---|
161 | except Exception, e: |
---|
162 | log.debug( "Creating sample_state table failed: %s" % str( e ) ) |
---|
163 | try: |
---|
164 | SampleEvent_table.create() |
---|
165 | except Exception, e: |
---|
166 | log.debug( "Creating sample_event table failed: %s" % str( e ) ) |
---|
167 | |
---|
168 | def downgrade(): |
---|
169 | # Load existing tables |
---|
170 | metadata.reflect() |
---|
171 | try: |
---|
172 | FormDefinition_table.drop() |
---|
173 | except Exception, e: |
---|
174 | log.debug( "Dropping form_definition table failed: %s" % str( e ) ) |
---|
175 | try: |
---|
176 | FormDefinitionCurrent_table.drop() |
---|
177 | except Exception, e: |
---|
178 | log.debug( "Dropping form_definition_current table failed: %s" % str( e ) ) |
---|
179 | try: |
---|
180 | FormValues_table.drop() |
---|
181 | except Exception, e: |
---|
182 | log.debug( "Dropping form_values table failed: %s" % str( e ) ) |
---|
183 | try: |
---|
184 | Request_table.drop() |
---|
185 | except Exception, e: |
---|
186 | log.debug( "Dropping request table failed: %s" % str( e ) ) |
---|
187 | try: |
---|
188 | RequestType_table.drop() |
---|
189 | except Exception, e: |
---|
190 | log.debug( "Dropping request_type table failed: %s" % str( e ) ) |
---|
191 | try: |
---|
192 | Sample_table.drop() |
---|
193 | except Exception, e: |
---|
194 | log.debug( "Dropping sample table failed: %s" % str( e ) ) |
---|
195 | try: |
---|
196 | SampleState_table.drop() |
---|
197 | except Exception, e: |
---|
198 | log.debug( "Dropping sample_state table failed: %s" % str( e ) ) |
---|
199 | try: |
---|
200 | SampleEvent_table.drop() |
---|
201 | except Exception, e: |
---|
202 | log.debug( "Dropping sample_event table failed: %s" % str( e ) ) |
---|