root/galaxy-central/lib/galaxy/model/migrate/versions/0025_user_info.py

リビジョン 2, 2.4 KB (コミッタ: hatakeyama, 14 年 前)

import galaxy-central

行番号 
1"""
2This script adds a foreign key to the form_values table in the galaxy_user table
3"""
4from sqlalchemy import *
5from sqlalchemy.orm import *
6from sqlalchemy.exceptions import *
7from migrate import *
8from migrate.changeset import *
9import datetime
10now = datetime.datetime.utcnow
11import sys, logging
12# Need our custom types, but don't import anything else from model
13from galaxy.model.custom_types import *
14
15log = logging.getLogger( __name__ )
16log.setLevel(logging.DEBUG)
17handler = logging.StreamHandler( sys.stdout )
18format = "%(name)s %(levelname)s %(asctime)s %(message)s"
19formatter = logging.Formatter( format )
20handler.setFormatter( formatter )
21log.addHandler( handler )
22
23metadata = MetaData( migrate_engine )
24db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
25
26def 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 "========================================"
30def 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 ) ) )
61def downgrade():
62    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。