[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>
This commit is contained in:
Joe Williams
2025-10-19 22:10:28 -07:00
committed by GitHub
parent 759356288b
commit bcddf482fe
29 changed files with 47 additions and 72 deletions

View File

@@ -34,7 +34,7 @@ class Migration_Sales_Tax_Data extends Migration
public function up(): void
{
$number_of_unmigrated = $this->get_count_of_unmigrated();
error_log("Migrating sales tax history. The number of sales that will be migrated is $number_of_unmigrated");
log_message('info', "Migrating sales tax history. The number of sales that will be migrated is $number_of_unmigrated");
if ($number_of_unmigrated > 0) {
$unmigrated_invoices = $this->get_unmigrated($number_of_unmigrated)->getResultArray();
@@ -44,7 +44,7 @@ class Migration_Sales_Tax_Data extends Migration
}
}
error_log('Migrating sales tax history. The number of sales that will be migrated is finished.');
log_message('info', 'Migrating sales tax history. The number of sales that will be migrated is finished.');
}
/**
@@ -146,7 +146,7 @@ class Migration_Sales_Tax_Data extends Migration
. ' ORDER BY SIT.sale_id) as US')->getResultArray();
if (!$result) {
error_log('Database error in 20170502221506_sales_tax_data.php related to sales_taxes or sales_items_taxes.');
log_message('info', 'Database error in 20170502221506_sales_tax_data.php related to sales_taxes or sales_items_taxes.');
return 0;
}

View File

@@ -19,8 +19,6 @@ class Migration_IndiaGST extends Migration
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.3.0_indiagst.sql');
error_log('Migrating tax configuration');
$count_of_tax_codes = $this->get_count_of_tax_code_entries();
if ($count_of_tax_codes > 0) {
@@ -42,8 +40,6 @@ class Migration_IndiaGST extends Migration
}
$this->drop_backups();
error_log('Migrating tax configuration completed');
}
/**

View File

@@ -13,10 +13,6 @@ class Migration_IndiaGST1 extends Migration
{
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.3.0_indiagst1.sql');
error_log('Fix definition of Supplier.Tax Id');
error_log('Definition of Supplier.Tax Id corrected');
}
/**

View File

@@ -11,6 +11,8 @@ class Migration_fix_empty_reports extends Migration
*/
public function up(): void
{
log_message('info', 'Starting migration: Fix empty reports.');
$builder = $this->db->table('stock_locations');
$builder->select('location_name');
$builder->where('location_id', 1);
@@ -23,6 +25,8 @@ class Migration_fix_empty_reports extends Migration
$builder->where('permission_id', 'receivings_' . $location_name);
$builder->orWhere('permission_id', 'sales_' . $location_name);
$builder->update();
log_message('info', 'Finished migration: Fix empty reports.');
}
/**

View File

@@ -11,6 +11,7 @@ class Migration_receipttaxindicator extends Migration
*/
public function up(): void
{
log_message('info', 'Migrating receipt tax indicator.');
$this->db->query('INSERT INTO ' . $this->db->prefixTable('app_config') . ' (`key`, `value`)
VALUES (\'receipt_show_tax_ind\', \'0\')');
}

View File

@@ -41,7 +41,7 @@ class Migration_TaxAmount extends Migration
$tax_decimals = $this->appconfig->get_value('tax_decimals', 2);
$number_of_unmigrated = $this->get_count_of_unmigrated();
error_log('Migrating sales tax fixing. The number of sales that will be migrated is ' . $number_of_unmigrated);
log_message('info', 'Migrating sales tax fixing. The number of sales that will be migrated is ' . $number_of_unmigrated);
if ($number_of_unmigrated > 0) {
$unmigrated_invoices = $this->get_unmigrated($number_of_unmigrated)->getResultArray();
@@ -54,7 +54,7 @@ class Migration_TaxAmount extends Migration
$this->db->query('DROP TABLE ' . $this->db->prefixTable('sales_taxes_backup'));
}
error_log('Migrating sales tax fixing. The number of sales that will be migrated is finished.');
log_message('info', 'Migrating sales tax fixing. The number of sales that will be migrated is finished.');
}
}
@@ -126,7 +126,7 @@ class Migration_TaxAmount extends Migration
. ' ORDER BY SIT.sale_id) as US')->getResultArray();
if (!$result) {
error_log('Database error in 20200202000000_taxamount.php related to sales_taxes or sales_items_taxes.');
log_message('info', 'Database error in 20200202000000_taxamount.php related to sales_taxes or sales_items_taxes.');
return 0;
}

View File

@@ -11,6 +11,7 @@ class Migration_taxgroupconstraint extends Migration
*/
public function up(): void
{
log_message('info', 'Migrating tax group constraints.');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('tax_jurisdictions') . ' ADD CONSTRAINT tax_jurisdictions_uq1 UNIQUE (tax_group)');
}

View File

@@ -11,6 +11,7 @@ class Migration_image_upload_defaults extends Migration
*/
public function up(): void
{
log_message('info', 'Migrating image upload defaults.');
$image_values = [
['key' => 'image_allowed_types', 'value' => 'gif|jpg|png'],
['key' => 'image_max_height', 'value' => '480'],

View File

@@ -11,12 +11,8 @@ class Migration_modify_attr_links_constraint extends Migration
*/
public function up(): void
{
error_log('Migrating modify_attr_links_constraint');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.3.2_modify_attr_links_constraint.sql');
error_log('Migrating modify_attr_links_constraint');
}
/**

View File

@@ -11,6 +11,7 @@ class Migration_cashrounding extends Migration
*/
public function up(): void
{
log_message('info', 'Migrating cash rounding.');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('sales_payments') . ' ADD COLUMN `cash_adjustment` tinyint NOT NULL DEFAULT 0 AFTER `cash_refund`');
}

View File

@@ -11,12 +11,8 @@ class Migration_add_item_kit_number extends Migration
*/
public function up(): void
{
error_log('Migrating add_item_kit_number');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.3.3_add_kits_item_number.sql');
error_log('Migrating add_item_kit_number');
}
/**

View File

@@ -11,12 +11,8 @@ class Migration_modify_session_datatype extends Migration
*/
public function up(): void
{
error_log('Migrating modify_session_datatype');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.3.4_modify_session_datatype.sql');
error_log('Migrating modify_session_datatype');
}
/**

View File

@@ -16,7 +16,7 @@ class Migration_database_optimizations extends Migration
*/
public function up(): void
{
error_log('Migrating database_optimizations');
log_message('info', 'Migrating database optimizations.');
$attribute = model(Attribute::class);
@@ -82,7 +82,7 @@ class Migration_database_optimizations extends Migration
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.0_database_optimizations.sql');
error_log('Migrating database_optimizations completed');
log_message('info', 'Finished migrating database optimizations.');
}
/**

View File

@@ -12,11 +12,11 @@ class Migration_remove_duplicate_links extends Migration
*/
public function up(): void
{
error_log('Migrating remove_duplicate_links');
log_message('info', 'Removing duplicate links.');
$this->migrate_duplicate_attribute_links();
error_log('Migrating remove_duplicate_links completed');
log_message('info', 'Duplicate links removed.');
}
/**

View File

@@ -11,11 +11,11 @@ class Migration_move_expenses_categories extends Migration
*/
public function up(): void
{
error_log('Migrating expense categories module');
log_message('info', 'Migrating expense categories module');
$this->db->simpleQuery("UPDATE ospos_grants SET menu_group = 'office' WHERE permission_id = 'expenses_categories'");
error_log('Migrating expense categories module completed');
log_message('info', 'Migrating expense categories module completed');
}
/**

View File

@@ -27,8 +27,6 @@ class Convert_to_ci4 extends Migration
*/
public function up(): void
{
error_log('Migrating database to CodeIgniter4 formats');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.0_ci4_conversion.sql');
@@ -39,8 +37,6 @@ class Convert_to_ci4 extends Migration
}
remove_backup();
error_log('Migrating to CodeIgniter4 formats completed');
}
/**

View File

@@ -11,6 +11,7 @@ class IntToTinyint extends Migration
*/
public function up(): void
{
log_message('info', 'Converting ints to tinyints.');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('customers') . ' MODIFY `consent` tinyint NOT NULL DEFAULT 0');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('cash_up') . ' MODIFY `note` tinyint NOT NULL DEFAULT 0');
}
@@ -20,6 +21,7 @@ class IntToTinyint extends Migration
*/
public function down(): void
{
log_message('info', 'Converting tinyints to ints.');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('customers') . ' MODIFY `consent` int NOT NULL DEFAULT 0');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('cash_up') . ' MODIFY `note` int NOT NULL DEFAULT 0');
}

View File

@@ -11,6 +11,7 @@ class Migration_add_missing_config extends Migration
*/
public function up(): void
{
log_message('info', 'Adding missing configs.');
$image_values = [
['key' => 'account_number', 'value' => ''], // This has no current maintenance, but it's used in Sales
['key' => 'category_dropdown', 'value' => ''],

View File

@@ -11,6 +11,7 @@ class Migration_drop_account_number_index extends Migration
*/
public function up(): void
{
log_message('info', 'Dropping account number index.');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('customers') . ' DROP INDEX account_number');
$this->db->query('ALTER TABLE ' . $this->db->prefixTable('customers') . ' ADD INDEX account_number (account_number)');
}

View File

@@ -25,7 +25,7 @@ class Migration_Convert_Barcode_Types extends Migration
*/
public function up(): void
{
log_message('info', 'Converting barcode types.');
$old_barcode_type = $this->config['barcode_type'];
switch ($old_barcode_type) {
@@ -52,6 +52,7 @@ class Migration_Convert_Barcode_Types extends Migration
*/
public function down(): void
{
log_message('info', 'Converting barcode types.');
$new_barcode_type = $this->config['barcode_type'];
switch ($new_barcode_type) {

View File

@@ -12,6 +12,7 @@ class Migration_fix_keys_for_db_upgrade extends Migration
*/
public function up(): void
{
log_message('info', 'Fixing keys for database upgrade.');
helper('migration');
$forge = Database::forge();

View File

@@ -13,6 +13,7 @@ class Migration_Attributes_fix_cascading_delete extends Migration
*/
public function up(): void
{
log_message('info', 'Fixing cascading deletes.');
helper('migration');
$this->db->query("ALTER TABLE `ospos_attribute_links` DROP INDEX `attribute_links_uq3`");

View File

@@ -11,12 +11,8 @@ class Migration_sessions_migration extends Migration
*/
public function up(): void
{
error_log('Migrating sessions table');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.1_migrate_sessions_table.sql');
error_log('Migrating sessions table');
}
/**

View File

@@ -12,7 +12,7 @@ class MigrationOptimizationIndices extends Migration
*/
public function up(): void
{
error_log('Migrating Optimization Indices');
log_message('info', 'Migrating Optimization Indices');
helper('migration');
$forge = Database::forge();
@@ -33,8 +33,6 @@ class MigrationOptimizationIndices extends Migration
$forge->addKey(['trans_items', 'trans_date'], false, false, 'trans_items_trans_date');
$forge->processIndexes('inventory');
}
error_log('Migrating Optimization Indices');
}
/**

View File

@@ -12,7 +12,6 @@ class AttributeLinksUniqueConstraint extends Migration
*/
public function up(): void
{
error_log('Migrating attribute_links unique constraint started');
helper('migration');
$foreignKeys = [
'ospos_attribute_links_ibfk_1',

View File

@@ -11,14 +11,8 @@ class Migration_MissingConfigKeys extends Migration
*/
public function up(): void
{
error_log('Migrating config keys...');
helper('migration');
if (executeScriptWithTransaction(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_missing_config_keys.sql')) {
error_log('Migrated config keys.');
} else {
error_log('Failed to migrate config keys.');
}
executeScriptWithTransaction(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_missing_config_keys.sql');
}
/**

View File

@@ -11,12 +11,8 @@ class Migration_NullableTaxCategoryId extends Migration
*/
public function up(): void
{
error_log('Migrating nullable tax category ID');
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_nullable_tax_category_id.sql');
error_log('Migrated nullable tax category ID');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_missing_config_keys.sql');
}
/**

View File

@@ -10,7 +10,7 @@ use Config\Database;
function execute_script(string $path): bool
{
$version = preg_replace("/(.*_)?(.*).sql/", "$2", $path);
error_log("Migrating to $version (file: $path)");
log_message('info', "Migrating to $version (file: $path)");
$sql = file_get_contents($path);
$sqls = explode(';', $sql);
@@ -18,7 +18,7 @@ function execute_script(string $path): bool
$db = Database::connect();
$success = true;
$success = true; // whether *all* queries succeeded
foreach ($sqls as $statement) {
$statement = "$statement;";
$hadError = !$db->simpleQuery($statement);
@@ -26,15 +26,16 @@ function execute_script(string $path): bool
if ($hadError) {
$success = false;
foreach ($db->error() as $error) {
error_log("error: $error");
log_message('error', "error: $error");
}
}
}
if ($success) {
error_log("Successfully migrated to $version");
} else {
error_log("Could not migrate to $version.");
log_message('info', "Successfully migrated to $version");
}
else {
log_message('info', "Could not migrate to $version.");
}
return $success;
@@ -48,7 +49,7 @@ function execute_script(string $path): bool
function executeScriptWithTransaction(string $path): bool
{
$version = preg_replace("/(.*_)?(.*).sql/", "$2", $path);
error_log("Migrating to $version (file: $path) with transaction");
log_message('info', "Migrating to $version (file: $path) with transaction");
$sql = file_get_contents($path);
$sqls = explode(';', $sql);
@@ -66,20 +67,20 @@ function executeScriptWithTransaction(string $path): bool
if ($hadError) {
$success = false;
foreach ($db->error() as $error) {
error_log("error: $error");
log_message('info', "error: $error");
}
}
}
} catch (Exception $e) {
error_log("Could not migrate to $version: " . $e->getMessage());
log_message('info', "Could not migrate to $version: " . $e->getMessage());
$db->transRollback();
return false;
}
if ($success) {
error_log("Successfully migrated to $version");
log_message('info', "Successfully migrated to $version");
} else {
error_log("Could not migrate to $version.");
log_message('info', "Could not migrate to $version.");
}
$db->transComplete();
@@ -285,9 +286,9 @@ function dropColumnIfExists(string $table, string $column): void
// Check if the column exists in the table
$builder->select('COLUMN_NAME')
->where('TABLE_SCHEMA', $db->database)
->where('TABLE_NAME', $prefix . $table)
->where('COLUMN_NAME', $column);
->where('TABLE_SCHEMA', $db->database)
->where('TABLE_NAME', $prefix . $table)
->where('COLUMN_NAME', $column);
$query = $builder->get();

View File

@@ -68,7 +68,7 @@ class Email_lib
$result = $email->send();
if (!$result) {
error_log($email->printDebugger());
log_message('error', $email->printDebugger());
}
return $result;