From 2e56cf766f1b30c63f723f74b641fd541a43deb1 Mon Sep 17 00:00:00 2001 From: jekkos Date: Sun, 4 Aug 2024 00:30:56 +0200 Subject: [PATCH] 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 --- .../20210422000000_database_optimizations.php | 17 +------ ...20240630000001_fix_keys_for_db_upgrade.php | 47 +++++++++++++++---- .../3.4.0_database_optimizations.sql | 4 -- docker-compose.dev.yml | 1 - docker-compose.yml | 2 +- 5 files changed, 39 insertions(+), 32 deletions(-) diff --git a/app/Database/Migrations/20210422000000_database_optimizations.php b/app/Database/Migrations/20210422000000_database_optimizations.php index 347499018..33da992d9 100644 --- a/app/Database/Migrations/20210422000000_database_optimizations.php +++ b/app/Database/Migrations/20210422000000_database_optimizations.php @@ -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 */ diff --git a/app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php b/app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php index 5baccc6e3..1306f7f87 100644 --- a/app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php +++ b/app/Database/Migrations/20240630000001_fix_keys_for_db_upgrade.php @@ -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); diff --git a/app/Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql b/app/Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql index 1c7eb9744..38986da88 100644 --- a/app/Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql +++ b/app/Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql @@ -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`); diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index 6d91108f7..4176a4048 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -1,4 +1,3 @@ -version: '2.2' volumes: uploads: diff --git a/docker-compose.yml b/docker-compose.yml index 2f60f5c08..ce97c66f2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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