1 | """ |
---|
2 | This script adds a foreign key to the form_values table in the galaxy_user table |
---|
3 | """ |
---|
4 | from sqlalchemy import * |
---|
5 | from sqlalchemy.orm import * |
---|
6 | from sqlalchemy.exceptions import * |
---|
7 | from migrate import * |
---|
8 | from migrate.changeset import * |
---|
9 | import datetime |
---|
10 | now = datetime.datetime.utcnow |
---|
11 | import sys, logging |
---|
12 | # Need our custom types, but don't import anything else from model |
---|
13 | from galaxy.model.custom_types import * |
---|
14 | |
---|
15 | log = logging.getLogger( __name__ ) |
---|
16 | log.setLevel(logging.DEBUG) |
---|
17 | handler = logging.StreamHandler( sys.stdout ) |
---|
18 | format = "%(name)s %(levelname)s %(asctime)s %(message)s" |
---|
19 | formatter = logging.Formatter( format ) |
---|
20 | handler.setFormatter( formatter ) |
---|
21 | log.addHandler( handler ) |
---|
22 | |
---|
23 | metadata = MetaData( migrate_engine ) |
---|
24 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
25 | |
---|
26 | def display_migration_details(): |
---|
27 | print "========================================" |
---|
28 | print "This script adds a foreign key to the form_values table in the galaxy_user table" |
---|
29 | print "========================================" |
---|
30 | def upgrade(): |
---|
31 | display_migration_details() |
---|
32 | # Load existing tables |
---|
33 | metadata.reflect() |
---|
34 | try: |
---|
35 | User_table = Table( "galaxy_user", metadata, autoload=True ) |
---|
36 | except NoSuchTableError: |
---|
37 | User_table = None |
---|
38 | log.debug( "Failed loading table galaxy_user" ) |
---|
39 | if User_table: |
---|
40 | try: |
---|
41 | col = Column( "form_values_id", Integer, index=True ) |
---|
42 | col.create( User_table ) |
---|
43 | assert col is User_table.c.form_values_id |
---|
44 | except Exception, e: |
---|
45 | log.debug( "Adding column 'form_values_id' to galaxy_user table failed: %s" % ( str( e ) ) ) |
---|
46 | try: |
---|
47 | FormValues_table = Table( "form_values", metadata, autoload=True ) |
---|
48 | except NoSuchTableError: |
---|
49 | FormValues_table = None |
---|
50 | log.debug( "Failed loading table form_values" ) |
---|
51 | # Add 1 foreign key constraint to the form_values table |
---|
52 | if User_table and FormValues_table: |
---|
53 | try: |
---|
54 | cons = ForeignKeyConstraint( [User_table.c.form_values_id], |
---|
55 | [FormValues_table.c.id], |
---|
56 | name='user_form_values_id_fk' ) |
---|
57 | # Create the constraint |
---|
58 | cons.create() |
---|
59 | except Exception, e: |
---|
60 | log.debug( "Adding foreign key constraint 'user_form_values_id_fk' to table 'galaxy_user' failed: %s" % ( str( e ) ) ) |
---|
61 | def downgrade(): |
---|
62 | pass |
---|