Files
opensourcepos/app/Database/Migrations/20210422000001_remove_duplicate_links.php
Joe Williams bcddf482fe [Feature] Add logging to migrations (#4327)
* `execute_script()` now returns a boolean for error handling.

* Added transaction to `Migration_MissingConfigKeys.up()`.

* Added logging to various migrations.

* Added transaction to `Migration_MissingConfigKeys.up()`.

* Added logging to various migrations.

* Formatting and function call fixes

Fixed a minor formatting issue in the migration helper.
Replaced a few remaining error_log() calls.
Updated executeScriptWithTransaction() to use log_message()

* Function call fix

Replaced the last error_log() calls with log_message().

---------

Co-authored-by: Joe Williams <hey-there-joe@outlook.com>
2025-10-19 22:10:28 -07:00

65 lines
1.9 KiB
PHP

<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use App\Models\Attribute;
class Migration_remove_duplicate_links extends Migration
{
/**
* Perform a migration step.
*/
public function up(): void
{
log_message('info', 'Removing duplicate links.');
$this->migrate_duplicate_attribute_links();
log_message('info', 'Duplicate links removed.');
}
/**
* Given the type of attribute, deletes any duplicates it finds in the attribute_values table and reassigns those
*
* @property attribute $attribute
*/
private function migrate_duplicate_attribute_links(): void
{
$attribute = model(Attribute::class);
// Remove duplicate attribute links
$this->db->transStart();
$builder = $this->db->table('attribute_links');
$builder->select('item_id, definition_id, attribute_id, COUNT(*) as count');
$builder->where('sale_id', null);
$builder->where('receiving_id', null);
$builder->where('item_id IS NOT NULL');
$builder->groupBy('item_id');
$builder->groupBy('definition_id');
$builder->groupBy('attribute_id');
$builder->having('count > 1');
$duplicated_links = $builder->get();
$builder = $this->db->table('attribute_links');
foreach ($duplicated_links->getResultArray() as $duplicated_link) {
$builder->where('sale_id', null);
$builder->where('receiving_id', null);
$builder->where('item_id', $duplicated_link['item_id']);
$builder->where('definition_id', $duplicated_link['definition_id']);
$builder->delete();
$attribute->saveAttributeLink($duplicated_link['item_id'], $duplicated_link['definition_id'], $duplicated_link['attribute_id']);
}
$this->db->transComplete();
}
/**
* Revert a migration step.
*/
public function down(): void {}
}