Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent duplicate entries using unique index #128

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion inc/class-job.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,13 @@ public function save() {
$result = $wpdb->update( $this->get_table(), $data, $where, $this->row_format( $data ), $this->row_format( $where ) );
} else {
$result = $wpdb->insert( $this->get_table(), $data, $this->row_format( $data ) );
$this->id = $wpdb->insert_id;

// Swallow duplicate insert errors.
if ( ! $result && strpos( $wpdb->last_error, '[1062]' ) !== false ) {
$result = true;
} else {
$this->id = $wpdb->insert_id;
}
}

self::flush_query_cache();
Expand Down
6 changes: 6 additions & 0 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,14 @@ function create_tables() {
`interval` int unsigned DEFAULT NULL,
`status` varchar(255) NOT NULL DEFAULT 'waiting',
`schedule` varchar(255) DEFAULT NULL,
`hash` binary(32) GENERATED ALWAYS AS (
UNHEX(SHA2(CONCAT_WS(
'-', `site`, `hook`, `args`, `nextrun`, `status`, `schedule`
), 256))
) STORED,

PRIMARY KEY (`id`),
UNIQUE INDEX `uniq` (`hash`),
KEY `status` (`status`),
KEY `site` (`site`),
KEY `hook` (`hook`)
Expand Down
49 changes: 49 additions & 0 deletions inc/upgrade/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use const HM\Cavalcade\Plugin\DATABASE_VERSION;
use HM\Cavalcade\Plugin as Cavalcade;
use HM\Cavalcade\Plugin\Job;
use WP_CLI;

/**
* Update the Cavalcade database version if required.
Expand Down Expand Up @@ -37,6 +38,10 @@ function upgrade_database() {
upgrade_database_4();
}

if ( $database_version < 5 ) {
upgrade_database_5();
}

update_site_option( 'cavalcade_db_version', DATABASE_VERSION );

Job::flush_query_cache();
Expand Down Expand Up @@ -100,3 +105,47 @@ function upgrade_database_4() {

$wpdb->query( $query );
}

/**
* Upgrade Cavalcade database tables to version 5.
*
* Add unique index to prevent duplicate entries being created.
*/
function upgrade_database_5() {
global $wpdb;

$queries = [
// Add generated stored hash column.
"ALTER TABLE `{$wpdb->base_prefix}cavalcade_jobs`
ADD COLUMN `hash` BINARY(32) GENERATED ALWAYS AS (
UNHEX(SHA2(CONCAT_WS(
'-', `site`, `hook`, `args`, `nextrun`, `status`, `schedule`
), 256))
) STORED;",
// Remove full group by mode requirement if set.
"SET @sql_mode_tmp = (SELECT @@sql_mode);",
"SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));",
// Remove any current duplicates.
"DELETE t1 FROM `{$wpdb->base_prefix}cavalcade_jobs` t1
INNER JOIN (
SELECT MIN(`id`) as `id`, `hash`
FROM `{$wpdb->base_prefix}cavalcade_jobs`
GROUP BY `hash`
HAVING COUNT(*) > 1
) t2
ON t1.`hash` = t2.`hash`
AND t1.id != t2.id;",
// Add a unique index on the hash column.
"ALTER TABLE `{$wpdb->base_prefix}cavalcade_jobs` ADD UNIQUE INDEX `uniq` (`hash`);",
// Restore the SQL mode.
"SET sql_mode = (SELECT @sql_mode_tmp);",
];

foreach ( $queries as $query ) {
$wpdb->query( $query );

if ( defined( 'WP_CLI' ) && WP_CLI && ! empty( $wpdb->last_error ) ) {
WP_CLI::error( $wpdb->last_error, false );
}
}
}
2 changes: 1 addition & 1 deletion plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace HM\Cavalcade\Plugin;

const DATE_FORMAT = 'Y-m-d H:i:s';
const DATABASE_VERSION = 4;
const DATABASE_VERSION = 5;

require __DIR__ . '/inc/namespace.php';
require __DIR__ . '/inc/class-job.php';
Expand Down