root/galaxy-central/lib/galaxy/model/migrate/versions/0059_sample_dataset_file_path.py

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

import galaxy-central

行番号 
1"""
2Migration script to modify the 'file_path' field type in 'sample_dataset' table
3to 'TEXT' so that it can support large file paths exceeding 255 characters
4"""
5
6from sqlalchemy import *
7from sqlalchemy.orm import *
8from migrate import *
9from migrate.changeset import *
10from sqlalchemy.exc import *
11
12from galaxy.model.custom_types import *
13from galaxy.util.json import from_json_string, to_json_string
14
15import datetime
16now = datetime.datetime.utcnow
17
18import logging
19log = logging.getLogger( __name__ )
20
21metadata = MetaData( migrate_engine )
22db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) )
23
24
25def upgrade():
26    print __doc__
27    metadata.reflect()
28    try:
29        SampleDataset_table = Table( "sample_dataset", metadata, autoload=True )
30    except NoSuchTableError, e:
31        SampleDataset_table = None
32        log.debug( "Failed loading table 'sample_dataset'" )
33
34    if SampleDataset_table:
35        cmd = "SELECT id, file_path FROM sample_dataset"
36        result = db_session.execute( cmd )
37        filepath_dict = {}
38        for r in result:
39            id = int(r[0])
40            filepath_dict[id] = r[1]
41        # remove the 'file_path' column
42        try:
43            SampleDataset_table.c.file_path.drop()
44        except Exception, e:
45            log.debug( "Deleting column 'file_path' from the 'sample_dataset' table failed: %s" % ( str( e ) ) )   
46        # create the column again
47        try:
48            col = Column( "file_path", TEXT )
49            col.create( SampleDataset_table )
50            assert col is SampleDataset_table.c.file_path
51        except Exception, e:
52            log.debug( "Creating column 'file_path' in the 'sample_dataset' table failed: %s" % ( str( e ) ) )   
53
54        for id, file_path in filepath_dict.items():
55            cmd = "update sample_dataset set file_path='%s' where id=%i" % (file_path, id)
56            db_session.execute( cmd )           
57
58def downgrade():
59    pass
Note: リポジトリブラウザについてのヘルプは TracBrowser を参照してください。