JezK
Edit File: Migrate_6_2_1.php
<?php namespace GeminiLabs\SiteReviews\Migrations; use GeminiLabs\SiteReviews\Contracts\MigrateContract; use GeminiLabs\SiteReviews\Database; use GeminiLabs\SiteReviews\Database\Query; use GeminiLabs\SiteReviews\Database\Tables; use GeminiLabs\SiteReviews\Database\Tables\TableAssignedPosts; use GeminiLabs\SiteReviews\Database\Tables\TableAssignedTerms; use GeminiLabs\SiteReviews\Database\Tables\TableAssignedUsers; class Migrate_6_2_1 implements MigrateContract { /** * Run migration. */ public function run(): bool { $this->migrateDatabaseIndexes(); $this->removeDuplicateCustomFields(); return true; } /** * Fixes the PRIMARY indexes of the assigned tables. * The method used in the 6_0_0 migration did not work with MariaDB. */ protected function migrateDatabaseIndexes(): void { $tables = [ TableAssignedPosts::class => 'post_id', TableAssignedTerms::class => 'term_id', TableAssignedUsers::class => 'user_id', ]; foreach ($tables as $table => $columnName) { $table = glsr($table); if (!glsr(Tables::class)->isInnodb($table->name)) { continue; } $constraints = glsr(Database::class)->dbGetCol(" SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = '{$table->database}' AND TABLE_NAME = '{$table->tablename}' "); if (!in_array('PRIMARY', $constraints)) { // 1. Drop the foreign constraints. This is only done when the primary key // is about to be rebuilt, because that is the only reason to do it: the // unique index below cannot be dropped while a foreign key is using it. // Dropping and re-adding them on a table that needs no repair is a full // rebuild of every constraint for nothing, and ADD CONSTRAINT validates // the whole table. $table->dropForeignConstraints(); // 2. Drop unique index $uniqueIndex = sprintf('%s%s_rating_id_%s_unique', glsr()->prefix, $table->name, $columnName); if (in_array($uniqueIndex, $constraints)) { glsr(Database::class)->dbQuery(" ALTER TABLE {$table->tablename} DROP INDEX {$uniqueIndex} "); } // 3. Add primary key (replaces the unique index) glsr(Database::class)->dbQuery(" ALTER TABLE {$table->tablename} ADD PRIMARY KEY (rating_id,{$columnName}) "); } // 4. Add the foreign constraints. Unconditional, and stays that way: each one // is guarded by foreignConstraintExists(), so this adds back the ones just // dropped, and back-fills any that an older install never got. $table->addForeignConstraints(); } } /** * Removes duplicate custom field values (keeps the last duplicate record). */ protected function removeDuplicateCustomFields(): void { $sql = " DELETE pm FROM table|postmeta AS pm INNER JOIN table|posts AS p ON (p.ID = pm.post_id) WHERE p.post_type = %s AND pm.meta_key LIKE '_custom_%%' AND pm.meta_id NOT IN ( SELECT * FROM ( SELECT MAX(meta_id) FROM table|postmeta WHERE meta_key LIKE '_custom_%%' GROUP BY post_id, meta_key ) AS x ) "; glsr(Database::class)->dbQuery( glsr(Query::class)->sql($sql, glsr()->post_type) ); } }