Automattic\WooCommerce\Database\Migrations

MetaToMetaTableMigrator::get_already_migrated_records()privateWC 1.0

Helper method to get already migrated records. Will be used to find prevent migration of already migrated records.

Method of the class: MetaToMetaTableMigrator{}

No Hooks.

Return

Array. Already migrated records.

Usage

// private - for code of main (parent) class only
$result = $this->get_already_migrated_records( $entity_ids ): array;
$entity_ids(array) (required)
List of entity ids to check for.

MetaToMetaTableMigrator::get_already_migrated_records() code WC 7.1.1

private function get_already_migrated_records( array $entity_ids ): array {
	global $wpdb;

	$destination_table_name        = $this->schema_config['destination']['meta']['table_name'];
	$destination_id_column         = $this->schema_config['destination']['meta']['meta_id_column'];
	$destination_entity_id_column  = $this->schema_config['destination']['meta']['entity_id_column'];
	$destination_meta_key_column   = $this->schema_config['destination']['meta']['meta_key_column'];
	$destination_meta_value_column = $this->schema_config['destination']['meta']['meta_value_column'];

	$entity_id_type_placeholder = MigrationHelper::get_wpdb_placeholder_for_type( $this->schema_config['destination']['meta']['entity_id_type'] );
	$entity_ids_placeholder     = implode( ',', array_fill( 0, count( $entity_ids ), $entity_id_type_placeholder ) );

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
	$data_already_migrated = $this->db_get_results(
		$wpdb->prepare(
			"
SELECT
   $destination_id_column meta_id,
       $destination_entity_id_column entity_id,
       $destination_meta_key_column meta_key,
       $destination_meta_value_column meta_value
FROM $destination_table_name destination
WHERE destination.$destination_entity_id_column in ( $entity_ids_placeholder ) ORDER BY destination.$destination_entity_id_column
",
			$entity_ids
		)
	);
	// phpcs:enable

	$already_migrated = array();

	foreach ( $data_already_migrated as $migrate_row ) {
		if ( ! isset( $already_migrated[ $migrate_row->entity_id ] ) ) {
			$already_migrated[ $migrate_row->entity_id ] = array();
		}

		// phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.SlowDBQuery.slow_db_query_meta_value
		if ( ! isset( $already_migrated[ $migrate_row->entity_id ][ $migrate_row->meta_key ] ) ) {
			$already_migrated[ $migrate_row->entity_id ][ $migrate_row->meta_key ] = array();
		}

		$already_migrated[ $migrate_row->entity_id ][ $migrate_row->meta_key ][] = array(
			'id'         => $migrate_row->meta_id,
			'meta_value' => $migrate_row->meta_value,
		);
		// phpcs:enable
	}

	return $already_migrated;
}