[2] | 1 | """ |
---|
| 2 | Migration script to modify the 'file_path' field type in 'sample_dataset' table |
---|
| 3 | to 'TEXT' so that it can support large file paths exceeding 255 characters |
---|
| 4 | """ |
---|
| 5 | |
---|
| 6 | from sqlalchemy import * |
---|
| 7 | from sqlalchemy.orm import * |
---|
| 8 | from migrate import * |
---|
| 9 | from migrate.changeset import * |
---|
| 10 | from sqlalchemy.exc import * |
---|
| 11 | |
---|
| 12 | from galaxy.model.custom_types import * |
---|
| 13 | from galaxy.util.json import from_json_string, to_json_string |
---|
| 14 | |
---|
| 15 | import datetime |
---|
| 16 | now = datetime.datetime.utcnow |
---|
| 17 | |
---|
| 18 | import logging |
---|
| 19 | log = logging.getLogger( __name__ ) |
---|
| 20 | |
---|
| 21 | metadata = MetaData( migrate_engine ) |
---|
| 22 | db_session = scoped_session( sessionmaker( bind=migrate_engine, autoflush=False, autocommit=True ) ) |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | def 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 | |
---|
| 58 | def downgrade(): |
---|
| 59 | pass |
---|