Integrate database migration into module activation

Module Activation Integration:
- Added automatic migration to module init() method
- Migration runs when module is activated/reactivated
- Checks if table exists and migration is needed
- Safe migration with proper error handling

Migration Features:
- Drops old unique constraint (entity, ca3_line)
- Adds new constraint (entity, ca3_line, account_code)
- Allows multiple accounts per CA-3 line
- Adds performance index on account_code
- Updates table comments for new CA-3 structure

Debug Improvements:
- Simplified debug output in setup page
- Shows if table structure is correct
- Indicates if migration is needed
- Clear instructions for user

No manual database access needed - migration runs automatically!
This commit is contained in:
Frank Cools 2025-10-02 17:32:59 +02:00
parent c74e09993f
commit 2aebb1ca67
2 changed files with 73 additions and 13 deletions

View File

@ -71,25 +71,21 @@ $mappings_by_line = $config->getAccountMappingsByLine();
$accounts = $config->getAccountingAccounts(); $accounts = $config->getAccountingAccounts();
$ca3_definitions = $config->getCA3LineDefinitions(); $ca3_definitions = $config->getCA3LineDefinitions();
// Debug: Check if table exists and show structure // Debug: Check if table exists and show structure (simplified)
$table_name = MAIN_DB_PREFIX . "declarationtva_account_mappings"; $table_name = MAIN_DB_PREFIX . "declarationtva_account_mappings";
$check_table_sql = "SHOW TABLES LIKE '" . $table_name . "'"; $check_table_sql = "SHOW TABLES LIKE '" . $table_name . "'";
$table_exists = $db->query($check_table_sql); $table_exists = $db->query($check_table_sql);
if ($table_exists && $db->num_rows($table_exists) > 0) { if ($table_exists && $db->num_rows($table_exists) > 0) {
setEventMessages("Debug: Table $table_name exists", null, 'mesgs'); // Table exists - check if migration is needed
$constraint_check = "SHOW INDEX FROM " . $table_name . " WHERE Key_name = 'uk_mapping_entity_line_account'";
// Show table structure $constraint_result = $db->query($constraint_check);
$structure_sql = "DESCRIBE " . $table_name; if ($constraint_result && $db->num_rows($constraint_result) > 0) {
$structure_result = $db->query($structure_sql); setEventMessages("Debug: Table structure is correct (supports multiple accounts per line)", null, 'mesgs');
if ($structure_result) { } else {
$structure_info = array(); setEventMessages("Debug: Table exists but needs migration - please deactivate and reactivate the module", null, 'warnings');
while ($row = $db->fetch_array($structure_result)) {
$structure_info[] = $row['Field'] . ' (' . $row['Type'] . ')';
}
setEventMessages("Debug: Table structure: " . implode(', ', $structure_info), null, 'mesgs');
} }
} else { } else {
setEventMessages("Debug: Table $table_name does NOT exist - need to run database setup", null, 'errors'); setEventMessages("Debug: Table $table_name does NOT exist - please activate the module first", null, 'errors');
} }
$section_headers = $config->getCA3SectionHeaders(); $section_headers = $config->getCA3SectionHeaders();

View File

@ -477,6 +477,13 @@ class modDeclarationTVA extends DolibarrModules
if ($result < 0) { if ($result < 0) {
return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default') return -1; // Do not activate module if error 'not allowed' returned when loading module SQL queries (the _load_table run sql with run_sql with the error allowed parameter set to 'default')
} }
// Run migration to fix account mappings table structure
$migration_result = $this->_run_migration_fix_account_mappings();
if ($migration_result < 0) {
// Log the error but don't fail module activation
dol_syslog("DeclarationTVA: Migration failed but continuing module activation", LOG_WARNING);
}
// Create extrafields during init // Create extrafields during init
//include_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; //include_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
@ -540,4 +547,61 @@ class modDeclarationTVA extends DolibarrModules
$sql = array(); $sql = array();
return $this->_remove($sql, $options); return $this->_remove($sql, $options);
} }
/**
* Run migration to fix account mappings table structure
* Allows multiple accounts per CA-3 line
*
* @return int 1 if OK, -1 if KO
*/
private function _run_migration_fix_account_mappings()
{
global $conf;
$table_name = MAIN_DB_PREFIX . "declarationtva_account_mappings";
// Check if table exists
$sql_check = "SHOW TABLES LIKE '" . $table_name . "'";
$result = $this->db->query($sql_check);
if (!$result || $this->db->num_rows($result) == 0) {
dol_syslog("DeclarationTVA: Table $table_name does not exist, skipping migration", LOG_INFO);
return 1; // Not an error, table will be created by _load_tables
}
// Check if the old unique constraint exists
$sql_check_constraint = "SHOW INDEX FROM " . $table_name . " WHERE Key_name = 'uk_mapping_entity_line'";
$result_constraint = $this->db->query($sql_check_constraint);
if (!$result_constraint || $this->db->num_rows($result_constraint) == 0) {
dol_syslog("DeclarationTVA: Old constraint not found, migration not needed", LOG_INFO);
return 1; // Migration not needed
}
dol_syslog("DeclarationTVA: Running migration to fix account mappings table", LOG_INFO);
// Migration steps
$migration_sql = array(
// Drop the old unique constraint that prevents multiple accounts per line
"ALTER TABLE `" . $table_name . "` DROP INDEX `uk_mapping_entity_line`",
// Add new unique constraint that allows multiple accounts per line
"ALTER TABLE `" . $table_name . "` ADD UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`)",
// Add index on account_code for better performance
"ALTER TABLE `" . $table_name . "` ADD INDEX `idx_account_code` (`account_code`)",
// Update the comment to reflect the new structure
"ALTER TABLE `" . $table_name . "` MODIFY COLUMN `ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29'"
);
foreach ($migration_sql as $sql) {
$result = $this->db->query($sql);
if (!$result) {
dol_syslog("DeclarationTVA: Migration SQL failed: " . $sql . " - Error: " . $this->db->lasterror(), LOG_ERR);
return -1;
}
}
dol_syslog("DeclarationTVA: Migration completed successfully", LOG_INFO);
return 1;
}
} }