root/galaxy-central/lib/galaxy/model/migrate/versions/0037_samples_library.py

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

import galaxy-central

行番号 
1"""
2This migration script removes the library_id & folder_id fields in the 'request' table and
3adds the same to the 'sample' table. This also adds a 'datatx' column to request_type table
4to store the sequencer login information. Finally, this adds a 'dataset_files' column to
5the sample table.
6"""
7from sqlalchemy import *
8from sqlalchemy.orm import *
9from migrate import *
10from migrate.changeset import *
11import sys, logging
12from galaxy.model.custom_types import *
13from sqlalchemy.exc import *
14import datetime
15now = datetime.datetime.utcnow
16
17log = logging.getLogger( __name__ )
18log.setLevel(logging.DEBUG)
19handler = logging.StreamHandler( sys.stdout )
20format = "%(name)s %(levelname)s %(asctime)s %(message)s"
21formatter = logging.Formatter( format )
22handler.setFormatter( formatter )
23log.addHandler( handler )
24
25metadata = MetaData( migrate_engine )
26db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
27
28def upgrade():
29    print __doc__
30    # Load existing tables
31    metadata.reflect()
32    # retuest_type table
33    try:
34        RequestType_table = Table( "request_type", metadata, autoload=True )
35    except NoSuchTableError:
36        RequestType_table = None
37        log.debug( "Failed loading table request_type" )
38    if RequestType_table:
39        # Add the datatx_info column in 'request_type' table
40        try:
41            col = Column( "datatx_info", JSONType() )
42            col.create( RequestType_table )
43            assert col is RequestType_table.c.datatx_info
44        except Exception, e:
45            log.debug( "Adding column 'datatx_info' to request_type table failed: %s" % ( str( e ) ) )   
46    # request table
47    try:
48        Request_table = Table( "request", metadata, autoload=True )
49    except NoSuchTableError:
50        Request_table = None
51        log.debug( "Failed loading table request" )
52    if Request_table:
53        # Delete library_id & folder_id columns in the table 'request'.
54        # if Galaxy is running on sqlite, the delete/recreate the table
55        # otherwise remove the specific columns
56        if migrate_engine.name == 'sqlite':
57            # create a temporary table
58            RequestTemp_table = Table( 'request_temp', metadata,
59                                        Column( "id", Integer, primary_key=True),
60                                        Column( "create_time", DateTime, default=now ),
61                                        Column( "update_time", DateTime, default=now, onupdate=now ),
62                                        Column( "name", TrimmedString( 255 ), nullable=False ),
63                                        Column( "desc", TEXT ),
64                                        Column( "form_values_id", Integer, ForeignKey( "form_values.id" ), index=True ),
65                                        Column( "request_type_id", Integer, ForeignKey( "request_type.id" ), index=True ),
66                                        Column( "user_id", Integer, ForeignKey( "galaxy_user.id" ), index=True ),
67                                        Column( "deleted", Boolean, index=True, default=False ) )
68            try:
69                RequestTemp_table.create()
70            except Exception, e:
71                log.debug( "Creating request_temp table failed: %s" % str( e ) ) 
72            # insert all the rows from the request table to the request_temp table
73            cmd = \
74                "INSERT INTO request_temp " + \
75                "SELECT id," + \
76                    "create_time," + \
77                    "update_time," + \
78                    "name," + \
79                    "desc," + \
80                    "form_values_id," + \
81                    "request_type_id," + \
82                    "user_id," + \
83                    "deleted " + \
84                "FROM request;"
85            db_session.execute( cmd )
86            # delete the 'request' table
87            try:
88                Request_table.drop()
89            except Exception, e:
90                log.debug( "Dropping request table failed: %s" % str( e ) )
91            # rename table request_temp to request
92            cmd = "ALTER TABLE request_temp RENAME TO request"
93            db_session.execute( cmd )
94        else:
95            # Delete the library_id column in 'request' table
96            try:
97                Request_table.c.library_id.drop()
98            except Exception, e:
99                log.debug( "Deleting column 'library_id' to request table failed: %s" % ( str( e ) ) )   
100            # Delete the folder_id column in 'request' table
101            try:
102                Request_table.c.folder_id.drop()
103            except Exception, e:
104                log.debug( "Deleting column 'folder_id' to request table failed: %s" % ( str( e ) ) )   
105    # sample table
106    try:
107        Sample_table = Table( "sample", metadata, autoload=True )
108    except NoSuchTableError:
109        Sample_table = None
110        log.debug( "Failed loading table sample" )
111    if Sample_table:
112        # Add the dataset_files column in 'sample' table
113        try:
114            col = Column( "dataset_files", JSONType() )
115            col.create( Sample_table )
116            assert col is Sample_table.c.dataset_files
117        except Exception, e:
118            log.debug( "Adding column 'dataset_files' to sample table failed: %s" % ( str( e ) ) )
119        # library table
120        try:
121            Library_table = Table( "library", metadata, autoload=True )
122        except NoSuchTableError:
123            Library_table = None
124            log.debug( "Failed loading table library" )
125        if Library_table:
126            # Add the library_id column in 'sample' table
127            try:
128                col = Column( "library_id", Integer, ForeignKey( "library.id" ), index=True )
129                col.create( Sample_table )
130                assert col is Sample_table.c.library_id
131            except Exception, e:
132                log.debug( "Adding column 'library_id' to sample table failed: %s" % ( str( e ) ) )
133        # library_folder table
134        try:
135            LibraryFolder_table = Table( "library_folder", metadata, autoload=True )
136        except NoSuchTableError:
137            LibraryFolder_table = None
138            log.debug( "Failed loading table library_folder" )
139        if LibraryFolder_table:
140            # Add the library_id column in 'sample' table
141            try:
142                col = Column( "folder_id", Integer, ForeignKey( "library_folder.id" ), index=True )
143                col.create( Sample_table )
144                assert col is Sample_table.c.folder_id
145            except Exception, e:
146                log.debug( "Adding column 'folder_id' to sample table failed: %s" % ( str( e ) ) )
147
148def downgrade():
149    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。