Automattic\WooCommerce\Database\Migrations

MetaToCustomTableMigrator::get_already_existing_records()protectedWC 1.0

Fetch id mappings for records that are already inserted in the destination table.

Метод класса: MetaToCustomTableMigrator{}

Хуков нет.

Возвращает

Массив. Already migrated entities, would be of the form array(

'$source_id1' => array(
	'source_id' => $source_id1,
	'destination_id' => $destination_id1
	'modified' => 0 if it can be determined that the row doesn't need update, 1 otherwise
),
...

)

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->get_already_existing_records( $entity_ids ): array;
$entity_ids(массив) (обязательный)
List of entity IDs to verify.

Код MetaToCustomTableMigrator::get_already_existing_records() WC 7.5.1

protected function get_already_existing_records( array $entity_ids ): array {
	global $wpdb;

	$source_table                   = $this->schema_config['source']['entity']['table_name'];
	$source_destination_join_column = $this->schema_config['source']['entity']['destination_rel_column'];
	$source_primary_key_column      = $this->schema_config['source']['entity']['primary_key'];

	$destination_table              = $this->schema_config['destination']['table_name'];
	$destination_source_join_column = $this->schema_config['destination']['source_rel_column'];
	$destination_primary_key_column = $this->schema_config['destination']['primary_key'];

	$entity_id_placeholder = implode( ',', array_fill( 0, count( $entity_ids ), '%d' ) );

	// Additional SQL to check if the row needs update according to the column mapping.
	// The IFNULL and CHAR(0) "hack" is needed because NULLs can't be directly compared in SQL.
	$modified_selector   = array();
	$core_column_mapping = array_filter(
		$this->core_column_mapping,
		function( $mapping ) {
			return ! isset( $mapping['select_clause'] );
		}
	);
	foreach ( $core_column_mapping as $column_name => $mapping ) {
		if ( $column_name === $source_primary_key_column ) {
			continue;
		}
		$modified_selector[] =
			"IFNULL(source.$column_name,CHAR(0)) != IFNULL(destination.{$mapping['destination']},CHAR(0))"
			. ( 'string' === $mapping['type'] ? ' COLLATE ' . $wpdb->collate : '' );
	}

	if ( empty( $modified_selector ) ) {
		$modified_selector = ', 1 AS modified';
	} else {
		$modified_selector = trim( implode( ' OR ', $modified_selector ) );
		$modified_selector = ", if( $modified_selector, 1, 0 ) AS modified";
	}

	$additional_where = $this->get_additional_where_clause_for_get_data_to_insert_or_update( $entity_ids );

	$already_migrated_entity_ids = $this->db_get_results(
		$wpdb->prepare(
		// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- All columns and table names are hardcoded.
			"
SELECT source.`$source_primary_key_column` as source_id, destination.`$destination_primary_key_column` as destination_id $modified_selector
FROM `$destination_table` destination
JOIN `$source_table` source ON source.`$source_destination_join_column` = destination.`$destination_source_join_column`
WHERE source.`$source_primary_key_column` IN ( $entity_id_placeholder ) $additional_where
",
			$entity_ids
		)
	// phpcs:enable
	);

	return array_column( $already_migrated_entity_ids, null, 'source_id' );
}