Compare commits

...

3 Commits

Author SHA1 Message Date
Joe Williams
43ebe2169a Fixed naming issue:
Nullable tax category ID migration now runs the correct script.
2025-10-15 22:22:15 -07:00
Joe Williams
3f82ac179a Added transaction to Migration_MissingConfigKeys.up(). 2025-09-16 12:14:05 -07:00
Joe Williams
73dab4f347 execute_script() now returns a boolean for error handling. 2025-09-16 12:10:32 -07:00
3 changed files with 35 additions and 13 deletions

View File

@@ -11,12 +11,22 @@ class Migration_MissingConfigKeys extends Migration
*/
public function up(): void
{
error_log('Migrating config table');
error_log('Starting transaction...');
$db = db_connect();
$db->transStart();
helper('migration');
execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_missing_config_keys.sql');
error_log('Migrating config table');
// execute_script returns whether everything executed successfully
if (execute_script(APPPATH . 'Database/Migrations/sqlscripts/3.4.2_missing_config_keys.sql')) {
error_log('Migrated config table.');
}
else {
error_log('Failed to migrate config table.');
}
error_log('Transaction completed.');
$db->transComplete();
}
/**

View File

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

View File

@@ -3,9 +3,11 @@
use Config\Database;
/**
* Migration helper
* Migration helper.
* @param string $path Path to migration script.
* @return bool Whether the migration executed successfully.
*/
function execute_script(string $path): void
function execute_script(string $path): bool
{
$version = preg_replace("/(.*_)?(.*).sql/", "$2", $path);
error_log("Migrating to $version (file: $path)");
@@ -16,17 +18,27 @@ function execute_script(string $path): void
$db = Database::connect();
$success = true; // whether *all* queries succeeded
foreach ($sqls as $statement) {
$statement = "$statement;";
$hadError = !$db->simpleQuery($statement);
if (!$db->simpleQuery($statement)) {
if ($hadError) {
$success = false;
foreach ($db->error() as $error) {
error_log("error: $error");
}
}
}
error_log("Migrated to $version");
if ($success) {
error_log("Successfully migrated to $version");
}
else {
error_log("Could not migrate to $version.");
}
return $success;
}
/**
@@ -228,9 +240,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();