3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Create a dedicated migration for Quantities.

This commit is contained in:
Michael Stenta 2021-02-04 11:53:50 -05:00
parent 99b3a15a6a
commit 6010d3af8f
7 changed files with 141 additions and 152 deletions

View file

@ -31,6 +31,7 @@ Follow the steps below to migrate your farmOS 1.x data to farmOS 2.x:
drush migrate:import --group=farm_migrate_taxonomy
drush migrate:import --group=farm_migrate_asset
drush migrate:import --group=farm_migrate_area
drush migrate:import --group=farm_migrate_quantity
drush migrate:import --group=farm_migrate_log
7. Confirm that all the above migrations were successful before running the

View file

@ -0,0 +1,37 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_migrate
id: farm_migrate_quantity
label: 'Quantities'
migration_group: farm_migrate_quantity
migration_tags:
- 'Drupal 7'
- 'farmOS 1.x'
class: Drupal\migrate\Plugin\Migration
field_plugin_method: null
cck_plugin_method: null
source:
plugin: d7_farm_quantity
destination:
plugin: 'entity:quantity'
process:
measure: measure
value/numerator: value_numerator
value/denominator: value_denominator
units:
plugin: migration_lookup
migration: farm_migrate_taxonomy_unit
source: units
label: label
uid:
plugin: migration_lookup
migration: farm_migrate_user
source: uid
migration_dependencies:
required:
- farm_migrate_user
- farm_migrate_taxonomy_unit
optional: { }

View file

@ -136,41 +136,16 @@ shared_configuration:
title: title
width: width
height: height
# Create a quantity for each quantity field collection.
quantity:
plugin: sub_process
source: log_quantities
process:
# Save the uid as a pseudo value.
_uid:
plugin: migration_lookup
migration: farm_migrate_user
source: uid
no_stub: true
# Save the units term as a pseudo value.
_units:
plugin: migration_lookup
migration: farm_migrate_taxonomy_unit
source: units
no_stub: true
# Create a quantity and save the ID.
target_id:
plugin: create_quantity
values:
uid: '@_uid'
created: created
changed: changed
units: '@_units'
label: label
measure: measure
value/numerator: value_numerator
value/denominator: value_denominator
plugin: migration_lookup
migration: farm_migrate_quantity
source: field_farm_quantity
migration_dependencies:
required:
- farm_migrate_file
- farm_migrate_file_private
- farm_migrate_user
- farm_migrate_taxonomy_log_category
- farm_migrate_taxonomy_unit
- farm_migrate_quantity
optional:
- farm_migrate_asset_equipment

View file

@ -0,0 +1,12 @@
langcode: en
status: true
dependencies:
enforced:
module:
- farm_migrate
id: farm_migrate_quantity
label: 'farmOS 1.x Quantity Migration'
description: 'Migrates quantity measurements from farmOS 1.x to farmOS 2.x'
source_type: 'farmOS 1.x'
module: null
shared_configuration : null

View file

@ -141,60 +141,19 @@ class FarmLog extends Log {
// Set the "is_movement" property for use in migrations.
$row->setSourceProperty('is_movement', $is_movement);
// Get values from the log that will be inherited to created quantities.
$log_uid = $row->getSourceProperty('uid');
$log_created = $row->getSourceProperty('created');
$log_changed = $row->getSourceProperty('changed');
// Get quantity log field value.
$quantity_values = $this->getFieldvalues('log', 'field_farm_quantity', $id);
// Iterate through quantity field values to collect field collection IDs.
$quantity_field_collection_item_ids = [];
$quantity_ids = [];
foreach ($quantity_values as $quantity_value) {
if (!empty($quantity_value['value'])) {
$quantity_field_collection_item_ids[] = $quantity_value['value'];
$quantity_ids[] = $quantity_value['value'];
}
}
// Iterate through the field collection IDs and load values.
$log_quantities = [];
foreach ($quantity_field_collection_item_ids as $item_id) {
$query = $this->select('field_collection_item', 'fci')
->condition('fci.item_id', $item_id)
->condition('fci.field_name', 'field_farm_quantity');
// Join the quantity label field.
$query->leftJoin('field_data_field_farm_quantity_label', 'fdffql', 'fdffql.entity_id = fci.item_id AND fdffql.deleted = 0');
$query->addField('fdffql', 'field_farm_quantity_label_value', 'label');
// Join the quantity measure field.
$query->leftJoin('field_data_field_farm_quantity_measure', 'fdffqm', 'fdffqm.entity_id = fci.item_id AND fdffqm.deleted = 0');
$query->addField('fdffqm', 'field_farm_quantity_measure_value', 'measure');
// Join the quantity units field.
$query->leftJoin('field_data_field_farm_quantity_units', 'fdffqu', 'fdffqu.entity_id = fci.item_id AND fdffqu.deleted = 0');
$query->addField('fdffqu', 'field_farm_quantity_units_tid', 'units');
// Join the quantity value field.
$query->leftJoin('field_data_field_farm_quantity_value', 'fdffqv', 'fdffqv.entity_id = fci.item_id AND fdffqv.deleted = 0');
$query->addField('fdffqv', 'field_farm_quantity_value_numerator', 'value_numerator');
$query->addField('fdffqv', 'field_farm_quantity_value_denominator', 'value_denominator');
// Execute the query.
$quantity_data = $query->execute()->fetchAssoc();
// Add values to inherit from the log.
$quantity_data['uid'] = $log_uid;
$quantity_data['created'] = $log_created;
$quantity_data['changed'] = $log_changed;
// Save the quantity to be created later.
$log_quantities[] = $quantity_data;
}
// Add the quantity logs to the row for future processing.
$row->setSourceProperty('log_quantities', $log_quantities);
// Add the quantity IDs to the row for future processing.
$row->setSourceProperty('log_quantities', $quantity_ids);
return parent::prepareRow($row);
}

View file

@ -0,0 +1,83 @@
<?php
namespace Drupal\farm_migrate\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
/**
* Asset source from database.
*
* @MigrateSource(
* id = "d7_farm_quantity",
* source_module = "farm_quantity"
* )
*/
class Quantity extends FieldableEntity {
/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('field_collection_item', 'fci');
$query->addField('fci', 'item_id', 'id');
// Join in the measure value.
$query->leftJoin('field_data_field_farm_quantity_measure', 'fdffqm', 'fci.item_id = fdffqm.entity_id');
$query->addField('fdffqm', 'field_farm_quantity_measure_value', 'measure');
// Join in the numerator and denominator values.
$query->leftJoin('field_data_field_farm_quantity_value', 'fdffqv', 'fci.item_id = fdffqv.entity_id');
$query->addField('fdffqv', 'field_farm_quantity_value_numerator', 'value_numerator');
$query->addField('fdffqv', 'field_farm_quantity_value_denominator', 'value_denominator');
// Join in the units value.
$query->leftJoin('field_data_field_farm_quantity_units', 'fdffqu', 'fci.item_id = fdffqu.entity_id');
$query->addField('fdffqu', 'field_farm_quantity_units_tid', 'units');
// Join in the label value.
$query->leftJoin('field_data_field_farm_quantity_label', 'fdffql', 'fci.item_id = fdffql.entity_id');
$query->addField('fdffql', 'field_farm_quantity_label_value', 'label');
// Join in the log table to get the uid.
$query->leftJoin('field_data_field_farm_quantity', 'fdffq', 'fci.item_id = fdffq.field_farm_quantity_value');
$query->leftJoin('log', 'l', "fdffq.entity_type = 'log' AND fdffq.entity_id = l.id");
$query->addField('l', 'uid');
// Ensure we don't include archived/deleted fields.
$query->condition('fci.archived', '0');
$query->condition('fdffq.deleted', '0');
// Distinct items only.
$query->distinct();
// Order by item_id.
$query->orderBy('fci.item_id');
return $query;
}
/**
* {@inheritdoc}
*/
public function fields() {
$fields = [
'id' => $this->t('The quantity item ID.'),
'measure' => $this->t('The quantity measure.'),
'value_numerator' => $this->t('The quantity value numerator.'),
'value_denominator' => $this->t('The quantity value denominator.'),
'units' => $this->t('The quantity units.'),
'label' => $this->t('The quantity label.'),
'uid' => $this->t('The user ID that created the quantity.'),
];
return $fields;
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['id']['type'] = 'integer';
return $ids;
}
}

View file

@ -1,78 +0,0 @@
<?php
namespace Drupal\quantity\Plugin\migrate\process;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Create quantity entities.
*
* This is an alternative to using the entity_generate process plugin which
* requires a "lookup" to happen before creating the entity. Since the quantity
* value field is a Fraction field, it is easier to use our own process plugin.
*
* @MigrateProcessPlugin(
* id = "create_quantity"
* )
*/
class CreateQuantity extends ProcessPluginBase implements ContainerFactoryPluginInterface {
/**
* Quantity entity storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $quantityStorage;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static(
$configuration,
$plugin_id,
$plugin_definition
);
$instance->quantityStorage = $container->get('entity_type.manager')->getStorage('quantity');
return $instance;
}
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
// Start array of entity values.
$entity_values = [];
// Gather any static default values for properties/fields.
if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
foreach ($this->configuration['default_values'] as $key => $value) {
$entity_values[$key] = $value;
}
}
// Gather any additional properties/fields.
if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
foreach ($this->configuration['values'] as $key => $property) {
$source_value = $row->get($property);
NestedArray::setValue($entity_values, explode(Row::PROPERTY_SEPARATOR, $key), $source_value, TRUE);
}
}
// Create the entity.
$entity = $this->quantityStorage->create($entity_values);
// Save the entity so it has an ID.
$entity->save();
// Return the ID.
return $entity->id();
}
}