2

I am changing the migration code (not written by me) to not create new files on migration but to re-use (or rewrite) the existing files.

The migration code looks like the following:

Source part:

$url = url('filename.json', array('absolute' => TRUE));
$this->source = new MigrateSourceJSON(new MultiListJSON($url), 'name', $fields);
$this->destination = new MigrateDestinationFieldCollection(
  'field_product_documents',
  array('host_entity_type' => 'node')
);
$this->map = new MigrateSQLMap($this->machineName,
  array(
    'name' => array(
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
    ),
  ),
 MigrateDestinationFile::getKeySchema()
);

Destination part:

$this->destination = new MigrateDestinationFile('file', 'MigrateFileBlob');
$this->addFieldMapping('value', 'blob');
$this->addFieldMapping('destination_file', 'name');
$this->addFieldMapping('filename', 'name');
$this->addFieldMapping('destination_dir' ,'dest_dir')
  ->defaultValue('private://');

This migration is only responsible for creating of the file entities from the blob which comes from a third party service.
There is another migration that is executed after this migration and is responsible for associating the file ids of the files created by this migration.

Following the documentation of the MigrateDestinationFile, the following code could be used to replace the file on the directory:

<?php
    $this->addFieldMapping('field_my_image:file_replace')
         ->defaultValue(FILE_EXISTS_REPLACE);
?>

But, during the file migration I am not associating it with any field. Hence, it could not be used. Or I am not sure of the exact way to use it.

My main concern is (let's say) if I have a file ABC.pdf on disk (might not be associated with any entity), the migration should not create a new ABC_0.pdf.

Any pointers would be highly appreciated!

1 Answer 1

1

As a temporary fix I have implemented the following in the prepareRow function.

function prepareRow($row) {
  // Delete the file if already present.
  $file_path = drupal_realpath($row->dest_dir . '/' . $row->name);
  if (file_exists($file_path)) {
    unlink($file_path);
  }
}

However, I am still not sure about the way it is recommended in the Migrate module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.