Add check to migration to prevent errors (#4032)

* Add check to migration

- Only drop the constraint if it exists.

Signed-off-by: objecttothis <objecttothis@gmail.com>

* Automatic bump of package-lock.json

Signed-off-by: objecttothis <objecttothis@gmail.com>

---------

Signed-off-by: objecttothis <objecttothis@gmail.com>
This commit is contained in:
objecttothis
2024-07-27 00:08:49 +04:00
committed by GitHub
parent 05538570ec
commit e1f8b73005
2 changed files with 18 additions and 4 deletions

View File

@@ -11,7 +11,14 @@ class Migration_fix_keys_for_db_upgrade extends Migration
*/
public function up(): void
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP FOREIGN KEY ospos_sales_items_taxes_ibfk_1');
$checkSql = "SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = '" . $this->db->prefixTable('sales_items_taxes') . "' AND CONSTRAINT_NAME = 'ospos_sales_items_taxes_ibfk_1'";
$foreignKeyExists = $this->db->query($checkSql)->getRow();
if($foreignKeyExists)
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP FOREIGN KEY ospos_sales_items_taxes_ibfk_1');
}
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes')
. ' ADD CONSTRAINT ospos_sales_items_taxes_ibfk_1 FOREIGN KEY (sale_id, item_id, line) '
. ' REFERENCES ' . $this->db->prefixTable('sales_items') . ' (sale_id, item_id, line)');
@@ -22,7 +29,14 @@ class Migration_fix_keys_for_db_upgrade extends Migration
*/
public function down(): void
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP CONSTRAINT ospos_sales_items_taxes_ibfk_1');
$checkSql = "SELECT CONSTRAINT_NAME FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = '" . $this->db->prefixTable('sales_items_taxes') . "' AND CONSTRAINT_NAME = 'ospos_sales_items_taxes_ibfk_1'";
$foreignKeyExists = $this->db->query($checkSql)->getRow();
if($foreignKeyExists)
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP CONSTRAINT ospos_sales_items_taxes_ibfk_1');
}
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes')
. ' ADD CONSTRAINT ospos_sales_items_taxes_ibfk_1 FOREIGN KEY (sale_id) '
. ' REFERENCES ' . $this->db->prefixTable('sales_items') . ' (sale_id)');

4
package-lock.json generated
View File

@@ -18,9 +18,9 @@
"bootstrap-tagsinput-2021": "^0.8.6",
"bootstrap-toggle": "^2.2.2",
"bootstrap3-dialog": "github:nakupanda/bootstrap3-dialog#master",
"bootstrap5": "npm:bootstrap@^5.3.2",
"bootstrap5": "npm:bootstrap@^5.3.3",
"bootswatch": "^3.4.1",
"bootswatch5": "npm:bootswatch@^5.3.2",
"bootswatch5": "npm:bootswatch@^5.3.3",
"chartist": "^0.11.4",
"chartist-plugin-axistitle": "^0.0.7",
"chartist-plugin-barlabels": "^0.0.5",