From f5a7521c1c6d252ad994a84a62052ae7a14a5020 Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Thu, 15 Sep 2022 13:33:55 -0400 Subject: [PATCH 1/7] Error saving action: `ActionScheduler_Action::$args` too long Closes #851 PHP Fatal error: Uncaught RuntimeException: Error saving action: ActionScheduler_Action::$args too long --- classes/schema/ActionScheduler_StoreSchema.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index 2506f0180..4d4f9378a 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -56,7 +56,7 @@ protected function get_table_definition( $table ) { last_attempt_gmt datetime NULL default '${default_date}', last_attempt_local datetime NULL default '${default_date}', claim_id bigint(20) unsigned NOT NULL default '0', - extended_args varchar(8000) DEFAULT NULL, + extended_args longtext, PRIMARY KEY (action_id), KEY hook (hook($max_index_length)), KEY status (status), From a22105dc3da53de32ae8a2ebc91fa4e2b87f0c86 Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Thu, 15 Sep 2022 13:41:58 -0400 Subject: [PATCH 2/7] Update ActionScheduler_StoreSchema.php --- classes/schema/ActionScheduler_StoreSchema.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index 4d4f9378a..af50eafde 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -120,7 +120,8 @@ public function update_schema_5_0( $table, $db_version ) { 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}' + MODIFY COLUMN last_attempt_local datetime NULL default '${default_date}', + MODIFY COLUMN extended_args longtext "; $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared } From a2b8470b512276c0aa0f23d719056e3db2170127 Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Tue, 8 Nov 2022 19:39:12 -0500 Subject: [PATCH 3/7] Added new schema to convert `$extended_args` --- .../schema/ActionScheduler_StoreSchema.php | 46 +++++++++++++++++-- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index af50eafde..da968fd5a 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -9,14 +9,14 @@ */ class ActionScheduler_StoreSchema extends ActionScheduler_Abstract_Schema { const ACTIONS_TABLE = 'actionscheduler_actions'; - const CLAIMS_TABLE = 'actionscheduler_claims'; - const GROUPS_TABLE = 'actionscheduler_groups'; - const DEFAULT_DATE = '0000-00-00 00:00:00'; + const CLAIMS_TABLE = 'actionscheduler_claims'; + const GROUPS_TABLE = 'actionscheduler_groups'; + const DEFAULT_DATE = '0000-00-00 00:00:00'; /** * @var int Increment this value to trigger a schema update. */ - protected $schema_version = 6; + protected $schema_version = 7; public function __construct() { $this->tables = [ @@ -31,6 +31,7 @@ public function __construct() { */ public function init() { add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_5_0' ), 10, 2 ); + add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_7_0' ), 10, 2 ); } protected function get_table_definition( $table ) { @@ -115,6 +116,43 @@ public function update_schema_5_0( $table, $db_version ) { $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 + } + + /** + * Update the actions table schema, allowing $args fields to be longtext. + * + * 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. + * + * @param string $table Name of table being updated. + * @param string $db_version The existing schema version of the table. + */ + public function update_schema_7_0( $table, $db_version ) { + global $wpdb; + + if ( 'actionscheduler_actions' !== $table || version_compare( $db_version, '7', '>=' ) ) { + 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 ) ) { + // Keeping _date_ keys, in case the user is migrating from a very old AS version $query = " ALTER TABLE ${table_name} MODIFY COLUMN scheduled_date_gmt datetime NULL default '${default_date}', From 7ee4e83dfc3891c8c75f08af29640819e3718185 Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Tue, 8 Nov 2022 19:43:40 -0500 Subject: [PATCH 4/7] Update ActionScheduler_DBStore.php --- classes/data-stores/ActionScheduler_DBStore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/data-stores/ActionScheduler_DBStore.php b/classes/data-stores/ActionScheduler_DBStore.php index 79cdcf38c..a48359942 100644 --- a/classes/data-stores/ActionScheduler_DBStore.php +++ b/classes/data-stores/ActionScheduler_DBStore.php @@ -20,7 +20,7 @@ class ActionScheduler_DBStore extends ActionScheduler_Store { private $claim_before_date = null; /** @var int */ - protected static $max_args_length = 8000; + protected static $max_args_length = 4294967295; // longtext length (4GB) /** @var int */ protected static $max_index_length = 191; From 97748f6c7cb76a4caf0447e41ece7560cd7c83b6 Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Tue, 8 Nov 2022 19:47:31 -0500 Subject: [PATCH 5/7] Update ActionScheduler_DBStore.php --- classes/data-stores/ActionScheduler_DBStore.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/data-stores/ActionScheduler_DBStore.php b/classes/data-stores/ActionScheduler_DBStore.php index a48359942..78484db13 100644 --- a/classes/data-stores/ActionScheduler_DBStore.php +++ b/classes/data-stores/ActionScheduler_DBStore.php @@ -19,8 +19,13 @@ class ActionScheduler_DBStore extends ActionScheduler_Store { */ private $claim_before_date = null; - /** @var int */ - protected static $max_args_length = 4294967295; // longtext length (4GB) + /** + * The "longtext" field type can store up to 4,294,967,295 (4GB) characters. + * For sanity, allow up to half the length of 4GB (2GB) in the field. + * + * @var int + */ + protected static $max_args_length = 2147483647; // longtext length (4GB) /** @var int */ protected static $max_index_length = 191; From d9eb30fc7a25760e300c9d788d244917131fbd6b Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Tue, 8 Nov 2022 20:04:19 -0500 Subject: [PATCH 6/7] Update ActionScheduler_StoreSchema.php --- .../schema/ActionScheduler_StoreSchema.php | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index da968fd5a..2d7d27ec9 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -129,13 +129,7 @@ public function update_schema_5_0( $table, $db_version ) { } /** - * Update the actions table schema, allowing $args fields to be longtext. - * - * 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. + * Update the actions table schema, changing `$extended_args` fields to be longtext. * * @param string $table Name of table being updated. * @param string $db_version The existing schema version of the table. @@ -147,22 +141,10 @@ public function update_schema_7_0( $table, $db_version ) { 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 ) ) { - // Keeping _date_ keys, in case the user is migrating from a very old AS version - $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}', - MODIFY COLUMN extended_args longtext - "; - $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared + $wpdb->query( "ALTER TABLE ${table_name} MODIFY COLUMN extended_args longtext" ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared } - // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared } } From b17ba69a0d8008f0680862b0de8309468372d11c Mon Sep 17 00:00:00 2001 From: Saad Siddique Date: Tue, 8 Nov 2022 20:04:51 -0500 Subject: [PATCH 7/7] Update ActionScheduler_StoreSchema.php --- classes/schema/ActionScheduler_StoreSchema.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/schema/ActionScheduler_StoreSchema.php b/classes/schema/ActionScheduler_StoreSchema.php index 2d7d27ec9..b4fe817bf 100644 --- a/classes/schema/ActionScheduler_StoreSchema.php +++ b/classes/schema/ActionScheduler_StoreSchema.php @@ -9,9 +9,9 @@ */ class ActionScheduler_StoreSchema extends ActionScheduler_Abstract_Schema { const ACTIONS_TABLE = 'actionscheduler_actions'; - const CLAIMS_TABLE = 'actionscheduler_claims'; - const GROUPS_TABLE = 'actionscheduler_groups'; - const DEFAULT_DATE = '0000-00-00 00:00:00'; + const CLAIMS_TABLE = 'actionscheduler_claims'; + const GROUPS_TABLE = 'actionscheduler_groups'; + const DEFAULT_DATE = '0000-00-00 00:00:00'; /** * @var int Increment this value to trigger a schema update.