Skip to content

Commit 5fc6f4b

Browse files
authored
Merge pull request #35 from john-shaffer/34-duplicate-indexes
Create options index ourselves instead of using dbDelta. Fixes #34.
2 parents bb2427b + 86b0d7f commit 5fc6f4b

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/Controller.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ public static function createOptionsTable() : void {
271271

272272
$sql = "CREATE TABLE $table_name (
273273
id mediumint(9) NOT NULL AUTO_INCREMENT,
274-
name VARCHAR(255) NOT NULL UNIQUE,
274+
name VARCHAR(255) NOT NULL,
275275
value VARCHAR(255) NOT NULL,
276276
label VARCHAR(255) NULL,
277277
description VARCHAR(255) NULL,
@@ -280,6 +280,15 @@ public static function createOptionsTable() : void {
280280

281281
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
282282
dbDelta( $sql );
283+
284+
// dbDelta doesn't handle unique indexes well.
285+
$indexes = $wpdb->query( "SHOW INDEX FROM $table_name WHERE key_name = 'name'" );
286+
if ( 0 === $indexes ) {
287+
$result = $wpdb->query( "CREATE UNIQUE INDEX name ON $table_name (name)" );
288+
if ( false === $result ) {
289+
\WP2Static\WsLog::l( "Failed to create 'name' index on $table_name." );
290+
}
291+
}
283292
}
284293

285294
public static function activate_for_single_site(): void {

0 commit comments

Comments
 (0)