Closed (fixed)
Project:
Migrate
Version:
7.x-2.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
7 May 2013 at 12:51 UTC
Updated:
8 Jul 2013 at 09:28 UTC
I want migrate files first and then use migrated files in node file/image fields. All files (AttachmentsMigration) are migrated and registered to drupal file_managed table without problem. But if i use rollback for my nodes (ArticlesMigration) my files are always deleted from disk and file_managed database.
I try use
$this->addFieldMapping('preserve_files')
->defaultValue(TRUE);
but without success. I try debug in file.inc and for some reason there is no entry in file_usage table.
Here is relevant part of migration class:
class AttachmentsMigration extends HNMigration {
public function __construct($arguments) {
parent::__construct($arguments);
$this->description = t('Migration Attachments from mongodb to Drupal');
$this->dependencies = array('PrepareAttachment', 'PrepareImage');
// We instantiate the MigrateMap
$this->map = new MigrateSQLMap(
$this->machineName,
array(
'old_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'ID z ceskeho API.',
),
),
MigrateDestinationFile::getKeySchema()
);
$query = db_select('hn_migrate_attachments', 'ma')
->fields('ma')
->orderBy('old_id', 'ASC');
$this->source = new MigrateSourceSQL($query);
$this->destination = new MigrateDestinationFile('file');
$this->addFieldMapping('value', 'data')
->callbacks(array($this, 'convertDataToURL')); // create url from MongoDB grid_fs ObjectId
$this->addFieldMapping('destination_dir', 'old_id')
->callbacks(array($this, 'getSubDir')); // create subdir
$this->addFieldMapping('destination_file', 'filename')
->callbacks(array($this, 'cleanupFileName')); // transliterate filename
$this->addFieldMapping('file_replace')
->defaultValue(MigrateFile::FILE_EXISTS_REUSE); // reuse files
$this->addFieldMapping('preserve_files')
->defaultValue(TRUE); // preserve files
}
/**
* Prepare entity before import begin.
*
* @param type $entity
* @param type $row
*/
public function prepare($entity, $row) {
// nastavime povodne meno suboru
$entity->filename = $row->filename;
// is file image type?
if ($image_info = @getimagesize($entity->value)) {
$entity->type = 'image';
$entity->filemime = $image_info['mime'];
}
else {
// use file_entity file_get_type() function for determine type of file
$entity->type = file_get_type($entity);
$entity->filemime = $row->mime_type;
}
}
}
Thanks for advice.
Comments
Comment #1
havran commentedI still have no success. I go to through code in plugins/destinations/file.inc and if i try comment out line 661 (in import() method from MigrateDestinationFile class) here:
Then migrate module create entry in file_usage table (as i expected).
I try debug rollback() mehod from MigrateDestinationFile and this seems value from $mappings['preserve_files'] is not taken from my field mapping: $this->addFieldMapping('preserve_files')->defaultValue(TRUE);
Comment #2
mikeryanIt's rolling back the node migration, not the file migration, that's deleting the files, right? What does the file field mapping look like in the node migration?
Comment #3
mikeryanOK, I hacked around with the wine.inc example to reproduce this - it is the node rollback that removes the file, because the only file_usage it sees is the one for the field.
The reason we try to avoid adding file_usage is that post-migration, file_usage rows held by migrate prevent manual deletion of files. In this case, though, the only way to prevent the implicit file deletion caused by the field deletion is to add a file_usage row. So, how can we make sure files don't get deleted as a side-effect of migration, but allow manual deletion? This needs a bit more thought...
Comment #4
havran commentedThanks for answer. Yes, it's about node migration - rollback articles delete files migrated by AttachmentsMigration. Here is relevant code from ArticlesMigration class:
I try preserve_files here but files are still deleted.
Comment #5
mikeryanTagging for Migrate 2.6.
Comment #6
mikeryanAddressed simply by adding preserve_files support for MigrateFileFid. We're already adding file_usage rows for MigrateFileUri, so this isn't introducing anything really new there.
Comment #7
havran commentedThank you!
Comment #9
marcus178 commentedIs there a solution to this without using 2.6 as I'm migrating from Ubercart as well and I don't want to have to go through the pain barrier of figuring that out to work with 2.6
Comment #10
criznach commentedI got this working in 2.5 (so far) by extending MigrateFileFid with my own class
I'm not sure if that's the normal method, but it seems to work...
Now I can do a rollback and import of both files and nodes, followed by rollback and import of just the nodes. All image fields are re-populated correctly.
Comment #11
marcus178 commented@criznach you're a life saver, that's fixed it for me.