@@ -0,0 +1,2977 @@
+#!/usr/bin/env python%0A# encoding: utf-8%0A%0Aimport logging%0Aimport datetime%0A%0Afrom modularodm import storage, Q%0A%0Afrom framework.mongo import database%0Afrom framework.transactions.context import TokuTransaction%0A%0Afrom website.models import Node%0Afrom website.addons.osffiles.model import NodeFile%0Afrom website.addons.osfstorage.model import OsfStorageFileRecord%0A%0Afrom scripts import utils as script_utils%0A%0A%0ANodeFile.set_storage(storage.MongoStorage(database, 'nodefile'))%0A%0Alogger = logging.getLogger(__name__)%0Ascript_utils.add_file_logger(logger, __file__)%0Alogging.basicConfig(level=logging.INFO)%0A%0A%0Adef migrate_version(idx, node_file, record):%0A    version = record.versions%5Bidx%5D%0A    version.date_modified = node_file.date_modified%0A    version.save()%0A%0A%0Adef migrate_node(node, dry_run=True):%0A    node_settings = node.get_addon('osfstorage')%0A    for _, versions in node.files_versions.iteritems():%0A        for idx, version in enumerate(versions):%0A            try:%0A                node_file = NodeFile.load(version)%0A                record = OsfStorageFileRecord.find_by_path(node_file.path, node_settings)%0A                assert len(record.versions) == len(versions)%0A                migrate_version(idx, node_file, record)%0A            except Exception as error:%0A                logger.error('Could not migrate object %7B0%7D on node %7B1%7D'.format(version, node._id))%0A                logger.exception(error)%0A                break%0A%0A%0Adef get_nodes():%0A    return Node.find(Q('files_versions', 'ne', None))%0A%0A%0Adef main(dry_run=True):%0A    nodes = get_nodes()%0A    logger.info('Migrating files on %7B0%7D %60Node%60 records'.format(len(nodes)))%0A    for node in nodes:%0A        try:%0A            with TokuTransaction():%0A                migrate_node(node, dry_run=dry_run)%0A        except Exception as error:%0A            logger.error('Could not migrate node %7B0%7D'.format(node._id))%0A            logger.exception(error)%0A%0A%0Afrom nose.tools import *  # noqa%0A%0Afrom tests.base import OsfTestCase%0Afrom tests.factories import ProjectFactory%0A%0Afrom website.addons.osfstorage.tests.factories import FileVersionFactory%0A%0A%0Aclass TestMigrateDates(OsfTestCase):%0A%0A    def setUp(self):%0A        super(TestMigrateDates, self).setUp()%0A        self.path = 'old-pizza'%0A        self.project = ProjectFactory()%0A        self.node_settings = self.project.get_addon('osfstorage')%0A        self.node_file = NodeFile(path=self.path)%0A        self.node_file.save()%0A        self.date = self.node_file.date_modified%0A        self.project.files_versions%5B'old_pizza'%5D = %5Bself.node_file._id%5D%0A        self.project.save()%0A        self.version = FileVersionFactory(date_modified=datetime.datetime.now())%0A        self.record = OsfStorageFileRecord.get_or_create(self.node_file.path, self.node_settings)%0A        self.record.versions = %5Bself.version%5D%0A        self.record.save()%0A%0A    def test_migrate_dates(self):%0A        assert_not_equal(self.version.date_modified, self.date)%0A        main(dry_run=False)%0A        assert_equal(self.version.date_modified, self.date)%0A

Add migration script to restore file modification dates.