Unverified Commit d9286729 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2833060 by claudiu.cristea, matroskeen, heddn, dww, benjifisher,...

Issue #2833060 by claudiu.cristea, matroskeen, heddn, dww, benjifisher, alexpott, xurizaemon, liam morland, quietone, mikeryan, mikelutz, pfrenssen: SqlBase::prepareQuery() should be called also on count
parent c9212079
Loading
Loading
Loading
Loading
Loading
+8 −3
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ public static function create(ContainerInterface $container, array $configuratio
   *   The query string.
   */
  public function __toString() {
    return (string) $this->query();
    return (string) $this->prepareQuery();
  }

  /**
@@ -383,8 +383,13 @@ public function rewind(): void {
  }

  /**
   * Prepares query object to retrieve data from the source database.
   *
   * This method should not be called directly. It is called automatically from
   * SqlBase::prepareQuery().
   *
   * @return \Drupal\Core\Database\Query\SelectInterface
   *   The query object.
   *   A Select query object with the source data.
   */
  abstract public function query();

@@ -392,7 +397,7 @@ abstract public function query();
   * Gets the source count using countQuery().
   */
  protected function doCount() {
    return (int) $this->query()->countQuery()->execute()->fetchField();
    return (int) $this->prepareQuery()->countQuery()->execute()->fetchField();
  }

  /**
+5 −0
Original line number Diff line number Diff line
name: 'Migrate SQL prepare query test'
type: module
description: 'Provides a source plugin to test prepare query method.'
package: Testing
version: VERSION
+48 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\migrate_sql_prepare_query_test\Plugin\migrate\source;

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

/**
 * Source plugin for prepare query test.
 *
 * @MigrateSource(
 *   id = "test_sql_prepare_query"
 * )
 */
class TestSqlPrepareQuery extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {
    return $this->select('migrate_source_test')->fields('migrate_source_test');
  }

  /**
   * {@inheritdoc}
   */
  protected function prepareQuery() {
    $this->query = parent::prepareQuery();
    $this->query->condition('name', 'foo', '!=');
    return $this->query;
  }

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

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return ['id' => 'ID', 'name' => 'Name'];
  }

}
+48 −0
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@
use Drupal\Core\Database\StatementInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\Core\Database\Database;
use Drupal\migrate\MigrateExecutable;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Plugin\MigrationInterface;

@@ -198,6 +200,52 @@ public static function highWaterDataProvider() {
    ];
  }

  /**
   * Tests prepare query method.
   */
  public function testPrepareQuery(): void {
    $this->prepareSourceData();
    $this->enableModules(['migrate_sql_prepare_query_test', 'entity_test']);

    /** @var \Drupal\migrate\Plugin\MigrationInterface $migration */
    $migration = $this->container->get('plugin.manager.migration')
      ->createStubMigration([
        'source' => ['plugin' => 'test_sql_prepare_query'],
        'process' => ['id' => 'id', 'name' => 'name'],
        'destination' => ['plugin' => 'entity:entity_test'],
      ]);

    // One item is excluded by the condition defined in the source plugin.
    // @see \Drupal\migrate_sql_prepare_query_test\Plugin\migrate\source\TestSqlPrepareQuery
    $count = $migration->getSourcePlugin()->count();
    $this->assertEquals(2, $count);

    // Run the migration and verify that the number of migrated items matches
    // the initial source count.
    (new MigrateExecutable($migration, new MigrateMessage()))->import();
    $this->assertEquals(2, $migration->getIdMap()->processedCount());
  }

  /**
   * Creates a custom source table and some sample data.
   */
  protected function prepareSourceData(): void {
    $this->sourceDatabase->schema()->createTable('migrate_source_test', [
      'fields' => [
        'id' => ['type' => 'int'],
        'name' => ['type' => 'varchar', 'length' => 32],
      ],
    ]);

    // Add some data in the table.
    $this->sourceDatabase->insert('migrate_source_test')
      ->fields(['id', 'name'])
      ->values(['id' => 1, 'name' => 'foo'])
      ->values(['id' => 2, 'name' => 'bar'])
      ->values(['id' => 3, 'name' => 'baz'])
      ->execute();
  }

}

/**