Move queries to new migration script (#4012)

Iterate over empty array if no query result
Switch compose back to master
Only remove index if no pk
Remove drop indices
Only person_id changes in this migration
Do not name primary key
This commit is contained in:
jekkos
2024-08-04 00:30:56 +02:00
committed by jekkos
parent 1c95d35a74
commit 2e56cf766f
5 changed files with 39 additions and 32 deletions

View File

@@ -55,6 +55,7 @@ class Migration_database_optimizations extends Migration
if($attribute_links)
{
$builder = $this->db->table('attribute_links');
$attribute_links = $attribute_links->getResultArray() ?: [];
foreach($attribute_links->getResultArray() as $attribute_link)
{
@@ -83,27 +84,11 @@ class Migration_database_optimizations extends Migration
}
$this->db->transComplete();
$this->delete_index('customers', 'person_id');
$this->delete_index('employees', 'person_id');
$this->delete_index('suppliers', 'person_id');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql');
error_log('Migrating database_optimizations completed');
}
private function delete_index(string $table, string $index): void
{
$result = $this->db->query('SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = \'' . $this->db->getPrefix() . "$table' AND index_name = '$index'");
$index_exists = $result->getRowArray()['COUNT(*)'] > 0;
if($index_exists)
{
$forge = Database::forge();
$forge->dropKey($table, $index, false);
}
}
/**
* Given the type of attribute, deletes any duplicates it finds in the attribute_values table and reassigns those
*/

View File

@@ -5,17 +5,25 @@ namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use Config\Database;
class Migration_fix_keys_for_db_upgrade extends Migration
{
class Migration_fix_keys_for_db_upgrade extends Migration {
/**
* Perform a migration step.
*/
public function up(): void
{
$this->db->query("ALTER TABLE `ospos_tax_codes` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;");
if (!$this->index_exists("ospos_customers', 'company_name"))
{
$this->db->query("ALTER TABLE `ospos_customers` ADD INDEX(`company_name`)");
}
$this->delete_index("items", 'ospos_items_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)
if ($foreignKeyExists)
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP FOREIGN KEY ospos_sales_items_taxes_ibfk_1');
}
@@ -24,9 +32,10 @@ class Migration_fix_keys_for_db_upgrade extends Migration
. ' 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)');
$this->delete_index('customers', 'person_id');
$this->delete_index('employees', 'person_id');
$this->delete_index('suppliers', 'person_id');
$this->create_primary_key('customers', 'person_id');
$this->create_primary_key('employees', 'person_id');
$this->create_primary_key('suppliers', 'person_id');
}
/**
@@ -37,7 +46,7 @@ class Migration_fix_keys_for_db_upgrade extends Migration
$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)
if ($foreignKeyExists)
{
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_items_taxes') . ' DROP CONSTRAINT ospos_sales_items_taxes_ibfk_1');
}
@@ -47,12 +56,30 @@ class Migration_fix_keys_for_db_upgrade extends Migration
. ' REFERENCES ' . $this->db->prefixTable('sales_items') . ' (sale_id)');
}
private function delete_index(string $table, string $index): void
private function create_primary_key(string $table, string $index): void
{
$result = $this->db->query('SELECT 1 FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name= \'' . $this->db->getPrefix() . "$table' AND column_key = '$index'");
if (!$result->getRowArray())
{
$this->delete_index($table, $index);
$forge = Database::forge();
$forge->addPrimaryKey($table, '');
}
}
private function index_exists(string $table, string $index): bool
{
$result = $this->db->query('SELECT COUNT(*) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name = \'' . $this->db->getPrefix() . "$table' AND index_name = '$index'");
$index_exists = $result->getRowArray()['COUNT(*)'] > 0;
$row_array = $result->getRowArray();
return $row_array && $row_array['COUNT(*)'] > 0;
}
if($index_exists)
private function delete_index(string $table, string $index): void
{
if($this->index_exists($table, $index))
{
$forge = Database::forge();
$forge->dropKey($table, $index, false);

View File

@@ -18,7 +18,6 @@ ALTER TABLE `ospos_customers` DROP FOREIGN KEY `ospos_customers_ibfk_1`;
ALTER TABLE `ospos_customers_points` DROP FOREIGN KEY `ospos_customers_points_ibfk_1`;
ALTER TABLE `ospos_sales` DROP FOREIGN KEY `ospos_sales_ibfk_2`;
DROP INDEX `person_id` ON `ospos_customers`;
ALTER TABLE `ospos_customers` MODIFY `taxable` tinyint(1) DEFAULT 1 NOT NULL;
ALTER TABLE `ospos_customers` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `ospos_customers` MODIFY `discount_type` tinyint(1) DEFAULT 0 NOT NULL;
@@ -45,7 +44,6 @@ ALTER TABLE `ospos_employees` DROP FOREIGN KEY `ospos_employees_ibfk_1`;
ALTER TABLE `ospos_cash_up` DROP FOREIGN KEY `ospos_cash_up_ibfk_1`;
ALTER TABLE `ospos_cash_up` DROP FOREIGN KEY `ospos_cash_up_ibfk_2`;
DROP INDEX `person_id` ON `ospos_employees`;
ALTER TABLE `ospos_employees` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `ospos_employees` MODIFY `hash_version` tinyint(1) DEFAULT 2 NOT NULL;
@@ -72,7 +70,6 @@ ALTER TABLE `ospos_expense_categories` ADD INDEX(`category_description`);
ALTER TABLE `ospos_giftcards` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;
#ospos_items table
ALTER TABLE `ospos_items` DROP FOREIGN KEY `ospos_items_ibfk_1`;
ALTER TABLE `ospos_items` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `ospos_items` MODIFY `stock_type` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `ospos_items` MODIFY `item_type` tinyint(1) DEFAULT 0 NOT NULL;
@@ -122,7 +119,6 @@ ALTER TABLE `ospos_items` DROP FOREIGN KEY `ospos_items_ibfk_1`;
ALTER TABLE `ospos_receivings` DROP FOREIGN KEY `ospos_receivings_ibfk_2`;
ALTER TABLE `ospos_suppliers` DROP FOREIGN KEY `ospos_suppliers_ibfk_1`;
DROP INDEX `person_id` ON `ospos_suppliers`;
ALTER TABLE `ospos_suppliers` MODIFY `deleted` tinyint(1) DEFAULT 0 NOT NULL;
ALTER TABLE `ospos_suppliers` MODIFY `category` tinyint(1) NOT NULL;
ALTER TABLE `ospos_suppliers` ADD INDEX(`category`);

View File

@@ -1,4 +1,3 @@
version: '2.2'
volumes:
uploads:

View File

@@ -3,7 +3,7 @@ include:
services:
sqlscript:
image: jekkos/opensourcepos:sqlscript
image: jekkos/opensourcepos:sql-master
command: /bin/sh -c 'exit 0'
ospos:
image: jekkos/opensourcepos:master