Score:0

Sql Migrate - working example

cn flag

Ok I just spend 8 hours with the migration module. I was not able to find just one working example for the migration of sql data. Even Drupal's own documentation has the state "outdated".

I am trying to import just titles - so that should be pretty straightforward. Unfortunately, I do not even get an error when trying to import the configuration nor by the source plugin.

test_migrate_er.info.yml - (the module is enabled)

name: Test Migrate ER
type: module
description: 'Importer for news nodes via sql ("test" // Typo 3)'
package: test
version: VERSION
core_version_requirement: ^8.8.0 || ^9.0
project: 'test_migrate_er'

dependencies:
  - drupal:migrate
  - drupal:migrate_tools
  - drupal:migrate_plus

migrate_plus.migration_group.test_migrate_content.yml (this is imported via "drush cim")

uuid: 366085cb-4cea-49d1-84a0-534afd31b114
langcode: en
status: true
dependencies: {  }
id: test_migrate_content
label:"TEST IMPORT"
description: null
source_type: null
module: null
shared_configuration: null
source:
  plugin: test_source
  key: migrate

The database connection is set in the local.settings.php (it does use the source key)

$databases['migrate']['default'] = [...]

When importing thr configuration all seems good - no errors ... but when exporting it the source bit including sub-elements (plugin and key) are gone as if they are invalid attributes. That would also explain why the plugin / migration is never shown when testing via drush ms. Seems that the configuration parser is ignoring it.

Source Plugin (/web/modules/custom/test_migrate_er/src/Plugin/migrate/source/News.php. -- the module "test_migrate_er" is enabled)

<?php

namespace Drupal\test_migrate_er\Plugin\migrate\source;

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

/**
 * Minimalistic example for a SqlBase source plugin.
 *
 * @MigrateSource(
 *   id = "test_source",
 *   source_module = "test_migrate_er",
 * )
 */
class News extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    // Source data is queried from 'curling_games' table.
    $query = $this->select('tx_news_domain_model_news_copy', 'n')
      ->fields('n', [
        'uid',
        'tstamp',
        'crdate',
        'datetime',
        'title',
        'teaser',
        'bodytext',
      ]);
    return $query;
  }



  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'uid' => $this->t('uid' ),
      'tstamp' => $this->t('tstamp' ),
      'crdate' => $this->t('crdate' ),
      'datetime' => $this->t('datetime' ),
      'title' => $this->t('title' ),
      'teaser' => $this->t('teaser' ),
      'bodytext' => $this->t('bodytext' ),
    ];
    return $fields;
  }

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

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    // This example shows how source properties can be added in
    // prepareRow(). The source dates are stored as 2017-12-17
    // and times as 16:00. Drupal 8 saves date and time fields
    // in ISO8601 format 2017-01-15T16:00:00 on UTC.
    // We concatenate source date and time and add the seconds.
    // The same result could also be achieved using the 'concat'
    // and 'format_date' process plugins in the migration
    // definition.
//    $date = $row->getSourceProperty('date');
//    $time = $row->getSourceProperty('time');
//    $datetime = $date . 'T' . $time . ':00';
//    $row->setSourceProperty('datetime', $datetime);
    return parent::prepareRow($row);
  }
}

You find a ton "migration" example codes ... non of them target sql and non of them seem to be up to date. I wonder if anybody knows a working example or at least a correct documentation. That would make the next days so much more enjoyable :)

Thanks for help

cn flag
Just to rule it out, is the group YAML in the question verbatim? `label:"TEST IMPORT"` isn't valid (needs a space)
Kevin avatar
in flag
A lot of migration examples target SQL. The migrate and migrate_plus modules have a ton of examples of doing a definition and custom source plugin of various types.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.