Sql Migrate – working example

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 Drupaltest_migrate_erPluginmigratesource;

use DrupalmigratePluginmigratesourceSqlBase;
use DrupalmigrateRow;

/**
 * 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

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Sql Migrate – working example

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.