Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Address fatal error in Action Scheduler when identity field was not correctly set on migration
<?php
/*
Plugin Name: Fix Action Scheduler Action IDs
Version: 0.2
Author: Automattic
Author URI: https://automattic.com/
Description: This is a one time use plugin designed to address a fatal error during AS migration where the identity field in the actionscheduler_actions table did not get set before actions were migrated.
License: GNU General Public License v3.0 (or later)
License URI: http://www.opensource.org/licenses/gpl-license.php
*/
add_action( 'init', function() {
global $wpdb;
// Assign table names with AS inactive.
$action_table = $wpdb->prefix . 'actionscheduler_actions';
$log_table = $wpdb->prefix . 'actionscheduler_logs';
// Get the relevant option.
$demarkation_id = get_option( 'action_scheduler_hybrid_store_demarkation' );
// Or get the maximum action ID in the posts table.
if ( $demarkation_id < 3 ) {
$demarkation_id = $wpdb->get_var( $wpdb->prepare( "SELECT MAX(ID) FROM {$wpdb->posts} WHERE post_type = %s", 'scheduled-action' ) );
$demarkation_id++;
}
// If there isn't a demarkation ID then there is nothing to do.
if ( $demarkation_id < 3 ) {
return;
}
// Check for a lower action ID in the actions table.
$min_action_id = $wpdb->get_var( "SELECT MIN(action_id) FROM {$action_table}" );
if ( $min_action_id < $demarkation_id ) {
$max_action_id = $wpdb->get_var( "SELECT MAX(action_id) FROM {$action_table}" );
// Update the action IDs to a range that doesn't currently exist.
$increase = max( $max_action_id + 1, $demarkation_id + 1 );
$wpdb->query( $wpdb->prepare( "UPDATE {$action_table} SET action_id = action_id + %d", $increase ) );
$wpdb->query( $wpdb->prepare( "UPDATE {$log_table} SET action_id = action_id + %d", $increase ) );
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment