MetaToMetaTableMigrator
extends TableMigrator
in package
Base class for implementing migrations from the standard WordPress meta table to custom meta (key-value pairs) tables.
Table of Contents
- $errors : array<string|int, mixed>
- An array of cummulated error messages.
- $schema_config : array<string|int, mixed>
- Schema config, see __construct for more details.
- __construct() : mixed
- MetaToMetaTableMigrator constructor.
- fetch_data_for_migration_for_ids() : array<string|int, array<string|int, mixed>>
- Fetch data for migration.
- fetch_sanitized_migration_data() : array<string|int, array<string|int, mixed>>
- Return data to be migrated for a batch of entities.
- process_migration_batch_for_ids() : array<string|int, mixed>
- Migrate a batch of orders, logging any database error that could arise and the exception thrown if any.
- process_migration_data() : array<string|int, mixed>
- Process migration data for a batch of entities.
- add_error() : void
- Add an error message to the errors list unless it's there already.
- clear_errors() : void
- Clear the error messages list.
- db_get_results() : mixed
- Run $wpdb->get_results and add the error, if any, to the errors list.
- db_query() : mixed
- Run $wpdb->query and add the error, if any, to the errors list.
- get_errors() : array<string|int, mixed>
- Get the list of error messages added.
- get_meta_config() : array<string|int, mixed>
- Returns config for the migration.
- maybe_add_insert_or_update_error() : void
- Check if the amount of processed database rows matches the amount of orders to process, and log an error if not.
- process_migration_batch_for_ids_core() : void
- Migrate a batch of entities from the posts table to the corresponding table.
- build_meta_table_query() : string
- Helper method to build query used to fetch data from source meta table.
- classify_update_insert_records() : array<string|int, array<string|int, mixed>>
- Classify each record on whether to migrate or update.
- generate_insert_sql_for_batch() : string
- Generate insert sql queries for batches.
- generate_update_sql_for_batch() : string
- Generate update SQL for given batch.
- get_already_migrated_records() : array<string|int, mixed>
- Helper method to get already migrated records. Will be used to find prevent migration of already migrated records.
Properties
$errors
An array of cummulated error messages.
private
array<string|int, mixed>
$errors
$schema_config
Schema config, see __construct for more details.
private
array<string|int, mixed>
$schema_config
Methods
__construct()
MetaToMetaTableMigrator constructor.
public
__construct() : mixed
Return values
mixed —fetch_data_for_migration_for_ids()
Fetch data for migration.
public
fetch_data_for_migration_for_ids(array<string|int, mixed> $entity_ids) : array<string|int, array<string|int, mixed>>
Parameters
- $entity_ids : array<string|int, mixed>
-
Array of IDs to fetch data for.
Return values
array<string|int, array<string|int, mixed>> — Data, will of the form: array( 'id_1' => array( 'column1' => array( value1_1, value1_2...), 'column2' => array(value2_1, value2_2...), ...), ..., )fetch_sanitized_migration_data()
Return data to be migrated for a batch of entities.
public
fetch_sanitized_migration_data(array<string|int, mixed> $entity_ids) : array<string|int, array<string|int, mixed>>
Parameters
- $entity_ids : array<string|int, mixed>
-
Ids of entities to migrate.
Return values
array<string|int, array<string|int, mixed>> — Data to be migrated. Would be of the form: array( 'data' => array( ... ), 'errors' => array( ... ) ).process_migration_batch_for_ids()
Migrate a batch of orders, logging any database error that could arise and the exception thrown if any.
public
process_migration_batch_for_ids(array<string|int, mixed> $entity_ids) : array<string|int, mixed>
Parameters
- $entity_ids : array<string|int, mixed>
-
Order ids to migrate.
Tags
Return values
array<string|int, mixed> — An array containing the keys 'errors' (array of strings) and 'exception' (exception object or null).process_migration_data()
Process migration data for a batch of entities.
public
process_migration_data(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
- $data : array<string|int, mixed>
-
Data to be migrated. Should be of the form: array( 'data' => array( ... ) ) as returned by the
fetch_sanitized_migration_data
method.
Return values
array<string|int, mixed> — Array of errors and exception if any.add_error()
Add an error message to the errors list unless it's there already.
protected
add_error(string $error) : void
Parameters
- $error : string
-
The error message to add.
Return values
void —clear_errors()
Clear the error messages list.
protected
clear_errors() : void
Return values
void —db_get_results()
Run $wpdb->get_results and add the error, if any, to the errors list.
protected
db_get_results([string|null $query = null ][, string $output = OBJECT ]) : mixed
Parameters
- $query : string|null = null
-
The SQL query to run.
- $output : string = OBJECT
-
Any of ARRAY_A | ARRAY_N | OBJECT | OBJECT_K constants.
Return values
mixed — Whatever $wpdb->get_results returns.db_query()
Run $wpdb->query and add the error, if any, to the errors list.
protected
db_query(string $query) : mixed
Parameters
- $query : string
-
The SQL query to run.
Return values
mixed — Whatever $wpdb->query returns.get_errors()
Get the list of error messages added.
protected
get_errors() : array<string|int, mixed>
Return values
array<string|int, mixed> —get_meta_config()
Returns config for the migration.
protected
abstract get_meta_config() : array<string|int, mixed>
Return values
array<string|int, mixed> — Meta config, must be in following format: array( 'source' => array( 'meta' => array( 'table_name' => source_meta_table_name, 'entity_id_column' => entity_id column name in source meta table, 'meta_key_column' => meta_key column', 'meta_value_column' => meta_value column', ), 'entity' => array( 'table_name' => entity table name for the meta table, 'source_id_column' => column name in entity table which maps to meta table, 'id_column' => id column in entity table, ), 'excluded_keys' => array of keys to exclude, ), 'destination' => array( 'meta' => array( 'table_name' => destination meta table name, 'entity_id_column' => entity_id column in meta table, 'meta_key_column' => meta key column, 'meta_value_column' => meta_value column, 'entity_id_type' => data type of entity id, 'meta_id_column' => id column in meta table, ), ), )maybe_add_insert_or_update_error()
Check if the amount of processed database rows matches the amount of orders to process, and log an error if not.
protected
maybe_add_insert_or_update_error(string $operation, array<string|int, mixed>|bool $received_rows_count) : void
Parameters
- $operation : string
-
Operation performed, 'insert' or 'update'.
- $received_rows_count : array<string|int, mixed>|bool
-
Value returned by @wpdb after executing the query.
Return values
void —process_migration_batch_for_ids_core()
Migrate a batch of entities from the posts table to the corresponding table.
protected
process_migration_batch_for_ids_core(array<string|int, mixed> $entity_ids) : void
Parameters
- $entity_ids : array<string|int, mixed>
-
Ids of entities ro migrate.
Return values
void —build_meta_table_query()
Helper method to build query used to fetch data from source meta table.
private
build_meta_table_query(array<string|int, mixed> $entity_ids) : string
Parameters
- $entity_ids : array<string|int, mixed>
-
List of entity IDs to build meta query for.
Return values
string — Query that can be used to fetch data.classify_update_insert_records()
Classify each record on whether to migrate or update.
private
classify_update_insert_records(array<string|int, mixed> $to_migrate, array<string|int, mixed> $already_migrated) : array<string|int, array<string|int, mixed>>
Parameters
- $to_migrate : array<string|int, mixed>
-
Records to migrate.
- $already_migrated : array<string|int, mixed>
-
Records already migrated.
Return values
array<string|int, array<string|int, mixed>> — Returns two arrays, first for records to migrate, and second for records to upgrade.generate_insert_sql_for_batch()
Generate insert sql queries for batches.
private
generate_insert_sql_for_batch(array<string|int, mixed> $batch) : string
Parameters
- $batch : array<string|int, mixed>
-
Data to generate queries for.
Return values
string — Insert SQL query.generate_update_sql_for_batch()
Generate update SQL for given batch.
private
generate_update_sql_for_batch(array<string|int, mixed> $batch) : string
Parameters
- $batch : array<string|int, mixed>
-
List of data to generate update SQL for. Should be in same format as output of $this->fetch_data_for_migration_for_ids.
Return values
string — Query to update batch records.get_already_migrated_records()
Helper method to get already migrated records. Will be used to find prevent migration of already migrated records.
private
get_already_migrated_records(array<string|int, mixed> $entity_ids) : array<string|int, mixed>
Parameters
- $entity_ids : array<string|int, mixed>
-
List of entity ids to check for.