ActionScheduler_StoreSchema::update_schema_5_0()publicWC 1.0

Update the actions table schema, allowing datetime fields to be NULL.

This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created.

Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however that method relies on dbDelta() and this change is not possible when using that function.

Method of the class: ActionScheduler_StoreSchema{}

No Hooks.

Return

null. Nothing (null).

Usage

$ActionScheduler_StoreSchema = new ActionScheduler_StoreSchema();
$ActionScheduler_StoreSchema->update_schema_5_0( $table, $db_version );
$table(string) (required)
Name of table being updated.
$db_version(string) (required)
The existing schema version of the table.

ActionScheduler_StoreSchema::update_schema_5_0() code WC 8.7.0

public function update_schema_5_0( $table, $db_version ) {
	global $wpdb;

	if ( 'actionscheduler_actions' !== $table || version_compare( $db_version, '5', '>=' ) ) {
		return;
	}

	// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
	$table_name   = $wpdb->prefix . 'actionscheduler_actions';
	$table_list   = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" );
	$default_date = self::DEFAULT_DATE;

	if ( ! empty( $table_list ) ) {
		$query = "
			ALTER TABLE {$table_name}
			MODIFY COLUMN scheduled_date_gmt datetime NULL default '{$default_date}',
			MODIFY COLUMN scheduled_date_local datetime NULL default '{$default_date}',
			MODIFY COLUMN last_attempt_gmt datetime NULL default '{$default_date}',
			MODIFY COLUMN last_attempt_local datetime NULL default '{$default_date}'
	";
		$wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}
	// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
}