2

I have created a simple migration class that extends SqlBase. When I run it with drush and dump the query in query(), I can see that it is working. When I dump a 'hello' in getIds() that fires. However, the migration never calls prepareRow() and never processes anything. I will paste all the relevant files below and the dumps. Hopefully I am missing something fairly simple.

Class:

use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;

/**
 * Locations table.
 *
 * @MigrateSource(
 *   id = "locations",
 *   source_module = "eso_migrate",
 * )
 */
class Locations extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    // Source data is queried from 'locations' table.
    $query = $this->select('locations', 'l')
      ->fields('l', [
        'id',
        'type',
        'name',
        'undaunted_npc',
      ]);
    drush_print(var_dump($query->execute()->fetchAssoc()));
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'id' => $this->t('id' ),
      'type' => $this->t('type' ),
      'name'   => $this->t('name' ),
      'undaunted_npc'    => $this->t('undaunted_npc'),
    ];
    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    drush_print('id');
    return [
      'id' => [
        'type' => 'integer',
        'alias' => 'l',
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    drush_print($row);
    // set type
    $type = $row->getSourceProperty('type');
    $term = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
      ->loadByProperties(['name' => $type]);
    $system_type = $term['id'];
    $row->setSourceProperty('type', $system_type);

    // set undaunted
    $type = $row->getSourceProperty('undaunted_npc');
    $tid = null;

    switch ($type) {
      case 1:
        $tid = 44;
        break;
      case 2:
        $tid = 45;
        break;
      case 3:
        $tid = 46;
        break;
    }

    $row->setSourceProperty('undaunted_npc', $tid);

    return parent::prepareRow($row);
  }
}

YML:

id: locations
label: 'Locations'
source:
  plugin: locations
  key: migrate
process:
  name: name
  field_type: type
  field_undaunted_npc: undaunted_npc
destination:
  plugin: entity:taxonomy_term
  default_bundle: location

drush_print(var_dump($query->execute()->fetchAssoc())) in the query method returns:

array(4) {
  ["id"]=>
  string(1) "1"
  ["type"]=>
  string(3) "pvp"
  ["name"]=>
  string(13) "Imperial City"
  ["undaunted_npc"]=>
  NULL
}

but drush_print($row) at top of prepare row is never called.

$ drush ms shows a total of 62 rows (which is correct) with 0 imported and 0 Unprocessed.

$ drush migrate:import locations produces:

[notice] Processed 0 items (0 created, 0 updated, 0 failed, 0 ignored) - done with 'locations'

Any help is greatly appreciated.

----- edit ----- adding more information

I emptied the migrate_map_locations and the migrate_message_locations tables and ran the migration again, which gave me a little more information. I am getting the following error on each row:

[error]  Drupal\Core\Database\IntegrityConstraintViolationException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null: INSERT INTO {taxonomy_term_field_data} (tid, vid, langcode, status, name, description__value, description__format, weight, changed, default_langcode) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array (
    [:db_insert_placeholder_0] => 202
    [:db_insert_placeholder_1] => location
    [:db_insert_placeholder_2] => en
    [:db_insert_placeholder_3] => 1
    [:db_insert_placeholder_4] => 
    [:db_insert_placeholder_5] => 
    [:db_insert_placeholder_6] => 
    [:db_insert_placeholder_7] => 0
    [:db_insert_placeholder_8] => 1551292760
    [:db_insert_placeholder_9] => 1 )

I can now dump the row data in prepareRow when I dump

drush_print($row->getSourceProperty('type'));
drush_print($row->getSourceProperty('name'));
drush_print($row->getSourceProperty('undaunted_npc'));

the values are present and correct. I am not sure why getSourceProperty returns the expected value for each of those (especially 'name') but none of those values make it to the query that creates the taxonomy term.

  • Let check database have a table migrate_map_locations? – Jonh Feb 27 at 17:20
  • @Jonh It does, and that table has 62 records (the same number of items I expect to be imported). 'destid1' column is null for everything. 'source_row_status' is 3 for everything. – bletch Feb 27 at 18:19
  • @Jonh - I emptied those tables and tried again which provided better error messages. I have updated the description of the issue to include the new information. – bletch Feb 27 at 18:46
  • 1
    Le go admin UI to check your process migrate is same with file YML. If not, you need reimport. – Jonh Feb 28 at 5:19
  • @Jonh - that was it! I forgot to re-import after changing 'title' to 'name' in my yml. If you want to write your comment as an answer, I can mark this answered. Thank you!! – bletch Feb 28 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.