Fix attribute dropdown delete (#4176)

This commit is contained in:
jekkos
2025-02-13 00:13:56 +01:00
committed by jekkos
parent eeaa693ede
commit cf73ffa825
4 changed files with 77 additions and 44 deletions

View File

@@ -231,14 +231,15 @@ class Attributes extends Secure_Controller
}
/**
* AJAX called function to delete an attribute value. This is never called in the code. Perhaps it was boiler plate code that just isn't needed?
* @param int $attribute_id
* AJAX called function to delete an attribute value. This is called when a dropdown item is removed.
*
* @param string $attribute_value
* @return bool
* @noinspection PhpUnused
*/
public function delete_value(int $attribute_id): bool //TODO: This function appears to never be used in the codebase. Is it needed?
public function delete_value(string $attribute_value): bool
{
return $this->attribute->delete_value($attribute_id, NO_DEFINITION_ID);
return $this->attribute->delete_value($attribute_value, NO_DEFINITION_ID);
}
/**

View File

@@ -18,7 +18,15 @@ class fix_duplicate_attributes extends Migration
helper('migration');
$this->drop_foreign_key_constraints();
$foreignKeys = [
'ospos_attribute_links_ibfk_1',
'ospos_attribute_links_ibfk_2',
'ospos_attribute_links_ibfk_3',
'ospos_attribute_links_ibfk_4',
'ospos_attribute_links_ibfk_5'
];
drop_foreign_key_constraints($foreignKeys, 'ospos_attribute_links');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.0_attribute_links_unique_constraint.sql');
}
@@ -57,44 +65,6 @@ class fix_duplicate_attributes extends Migration
}
}
/**
* Drops the foreign key constraints from the attribute_links table.
* This is required to successfully create the generated unique constraint.
*
* @return void
*/
private function drop_foreign_key_constraints(): void
{
$foreignKeys = [
'ospos_attribute_links_ibfk_1',
'ospos_attribute_links_ibfk_2',
'ospos_attribute_links_ibfk_3',
'ospos_attribute_links_ibfk_4',
'ospos_attribute_links_ibfk_5'
];
$current_prefix = $this->db->getPrefix();
$this->db->setPrefix('');
$database_name = $this->db->database;
foreach ($foreignKeys as $fk)
{
$builder = $this->db->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS');
$builder->select('CONSTRAINT_NAME');
$builder->where('TABLE_SCHEMA', $database_name);
$builder->where('TABLE_NAME', 'ospos_attribute_links');
$builder->where('CONSTRAINT_TYPE', 'FOREIGN KEY');
$builder->where('CONSTRAINT_NAME', $fk);
$query = $builder->get();
if($query->getNumRows() > 0)
{
$this->db->query("ALTER TABLE `ospos_attribute_links` DROP FOREIGN KEY `$fk`");
}
}
$this->db->setPrefix($current_prefix);
}
/**
* Revert a migration step.

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use App\Models\Attribute;
use CodeIgniter\Database\ResultInterface;
class Migration_Attributes_fix_cascading_delete extends Migration
{
/**
* Perform a migration step.
*/
public function up(): void
{
helper('migration');
drop_foreign_key_constraints(['ospos_attribute_links_ibfk_1', 'ospos_attribute_links_ibfk_2'], 'ospos_attribute_links');
$this->db->query("ALTER TABLE `ospos_attribute_links` ADD CONSTRAINT `ospos_attribute_links_ibfk_1` FOREIGN KEY (`definition_id`) REFERENCES `ospos_attribute_definitions` (`definition_id`) ON DELETE CASCADE;");
$this->db->query("ALTER TABLE `ospos_attribute_links` ADD CONSTRAINT `ospos_attribute_links_ibfk_2` FOREIGN KEY (`attribute_id`) REFERENCES `ospos_attribute_values` (`attribute_id`) ON DELETE CASCADE;");
}
/**
* Revert a migration step.
*/
public function down(): void
{
}
}

View File

@@ -30,4 +30,37 @@ function execute_script(string $path): void
}
error_log("Migrated to $version");
}
}
/**
* Drops the foreign key constraints from the attribute_links table.
* This is required to successfully create the generated unique constraint.
*
* @return void
*/
function drop_foreign_key_constraints(array $foreignKeys, string $table): void
{
$db = Database::connect();
$current_prefix = $db->getPrefix();
$db->setPrefix('');
$database_name = $db->database;
foreach ($foreignKeys as $fk)
{
$builder = $db->table('INFORMATION_SCHEMA.TABLE_CONSTRAINTS');
$builder->select('CONSTRAINT_NAME');
$builder->where('TABLE_SCHEMA', $database_name);
$builder->where('TABLE_NAME', $table);
$builder->where('CONSTRAINT_TYPE', 'FOREIGN KEY');
$builder->where('CONSTRAINT_NAME', $fk);
$query = $builder->get();
if($query->getNumRows() > 0)
{
$db->query("ALTER TABLE `$table` DROP FOREIGN KEY `$fk`");
}
}
$db->setPrefix($current_prefix);
}