ActionScheduler_LoggerSchema.php
102 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_LoggerSchema |
| 5 | * |
| 6 | * @codeCoverageIgnore |
| 7 | * |
| 8 | * Creates a custom table for storing action logs |
| 9 | */ |
| 10 | class ActionScheduler_LoggerSchema extends ActionScheduler_Abstract_Schema { |
| 11 | const LOG_TABLE = 'actionscheduler_logs'; |
| 12 | |
| 13 | /** |
| 14 | * Schema version. |
| 15 | * |
| 16 | * Increment this value to trigger a schema update. |
| 17 | * |
| 18 | * @var int |
| 19 | */ |
| 20 | protected $schema_version = 3; |
| 21 | |
| 22 | /** |
| 23 | * Construct. |
| 24 | */ |
| 25 | public function __construct() { |
| 26 | $this->tables = array( |
| 27 | self::LOG_TABLE, |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Performs additional setup work required to support this schema. |
| 33 | */ |
| 34 | public function init() { |
| 35 | add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_3_0' ), 10, 2 ); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Get table definition. |
| 40 | * |
| 41 | * @param string $table Table name. |
| 42 | */ |
| 43 | protected function get_table_definition( $table ) { |
| 44 | global $wpdb; |
| 45 | $table_name = $wpdb->$table; |
| 46 | $charset_collate = $wpdb->get_charset_collate(); |
| 47 | switch ( $table ) { |
| 48 | |
| 49 | case self::LOG_TABLE: |
| 50 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
| 51 | return "CREATE TABLE $table_name ( |
| 52 | log_id bigint(20) unsigned NOT NULL auto_increment, |
| 53 | action_id bigint(20) unsigned NOT NULL, |
| 54 | message text NOT NULL, |
| 55 | log_date_gmt datetime NULL default '{$default_date}', |
| 56 | log_date_local datetime NULL default '{$default_date}', |
| 57 | PRIMARY KEY (log_id), |
| 58 | KEY action_id (action_id), |
| 59 | KEY log_date_gmt (log_date_gmt) |
| 60 | ) $charset_collate"; |
| 61 | |
| 62 | default: |
| 63 | return ''; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Update the logs table schema, allowing datetime fields to be NULL. |
| 69 | * |
| 70 | * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
| 71 | * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
| 72 | * |
| 73 | * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
| 74 | * that method relies on dbDelta() and this change is not possible when using that function. |
| 75 | * |
| 76 | * @param string $table Name of table being updated. |
| 77 | * @param string $db_version The existing schema version of the table. |
| 78 | */ |
| 79 | public function update_schema_3_0( $table, $db_version ) { |
| 80 | global $wpdb; |
| 81 | |
| 82 | if ( 'actionscheduler_logs' !== $table || version_compare( $db_version, '3', '>=' ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 87 | $table_name = $wpdb->prefix . 'actionscheduler_logs'; |
| 88 | $table_list = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ); |
| 89 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
| 90 | |
| 91 | if ( ! empty( $table_list ) ) { |
| 92 | $query = " |
| 93 | ALTER TABLE {$table_name} |
| 94 | MODIFY COLUMN log_date_gmt datetime NULL default '{$default_date}', |
| 95 | MODIFY COLUMN log_date_local datetime NULL default '{$default_date}' |
| 96 | "; |
| 97 | $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 98 | } |
| 99 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 100 | } |
| 101 | } |
| 102 |