From 2aebb1ca67e6e809587d43bbd578745763575f63 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 17:32:59 +0200 Subject: [PATCH] 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! --- admin/setup_mvp.php | 22 ++++---- core/modules/modDeclarationTVA.class.php | 64 ++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 13 deletions(-) diff --git a/admin/setup_mvp.php b/admin/setup_mvp.php index 2cde947..34f3114 100644 --- a/admin/setup_mvp.php +++ b/admin/setup_mvp.php @@ -71,25 +71,21 @@ $mappings_by_line = $config->getAccountMappingsByLine(); $accounts = $config->getAccountingAccounts(); $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"; $check_table_sql = "SHOW TABLES LIKE '" . $table_name . "'"; $table_exists = $db->query($check_table_sql); if ($table_exists && $db->num_rows($table_exists) > 0) { - setEventMessages("Debug: Table $table_name exists", null, 'mesgs'); - - // Show table structure - $structure_sql = "DESCRIBE " . $table_name; - $structure_result = $db->query($structure_sql); - if ($structure_result) { - $structure_info = array(); - while ($row = $db->fetch_array($structure_result)) { - $structure_info[] = $row['Field'] . ' (' . $row['Type'] . ')'; - } - setEventMessages("Debug: Table structure: " . implode(', ', $structure_info), null, 'mesgs'); + // Table exists - check if migration is needed + $constraint_check = "SHOW INDEX FROM " . $table_name . " WHERE Key_name = 'uk_mapping_entity_line_account'"; + $constraint_result = $db->query($constraint_check); + if ($constraint_result && $db->num_rows($constraint_result) > 0) { + setEventMessages("Debug: Table structure is correct (supports multiple accounts per line)", null, 'mesgs'); + } else { + setEventMessages("Debug: Table exists but needs migration - please deactivate and reactivate the module", null, 'warnings'); } } 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(); diff --git a/core/modules/modDeclarationTVA.class.php b/core/modules/modDeclarationTVA.class.php index 32aa436..53dfcc4 100644 --- a/core/modules/modDeclarationTVA.class.php +++ b/core/modules/modDeclarationTVA.class.php @@ -477,6 +477,13 @@ class modDeclarationTVA extends DolibarrModules 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') } + + // 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 //include_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; @@ -540,4 +547,61 @@ class modDeclarationTVA extends DolibarrModules $sql = array(); 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; + } }