- Enhanced validateDeclaration() to check for column existence - Falls back to basic validation if enhanced columns don't exist - Updated migration script with proper column existence checks - Uses dynamic SQL to avoid errors on existing columns - System now works with or without the additional columns - Graceful degradation for different database states
69 lines
2.6 KiB
SQL
69 lines
2.6 KiB
SQL
-- Migration: Add documents table for linking validated PDFs to declarations
|
|
-- Version: 2.1.0
|
|
-- Date: 2025-01-06
|
|
|
|
-- Create table for linking documents to declarations
|
|
CREATE TABLE IF NOT EXISTS `llx_declarationtva_documents` (
|
|
`rowid` int(11) NOT NULL AUTO_INCREMENT,
|
|
`declaration_id` int(11) NOT NULL,
|
|
`document_id` int(11) NOT NULL,
|
|
`created_date` datetime NOT NULL,
|
|
`entity` int(11) NOT NULL DEFAULT 1,
|
|
PRIMARY KEY (`rowid`),
|
|
KEY `idx_declaration_id` (`declaration_id`),
|
|
KEY `idx_document_id` (`document_id`),
|
|
KEY `idx_entity` (`entity`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
|
|
|
-- Add validated_date and validated_by columns to declarations table if they don't exist
|
|
-- Check if validated_date column exists first
|
|
SET @sql = (SELECT IF(
|
|
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'llx_declarationtva_declarations'
|
|
AND COLUMN_NAME = 'validated_date') = 0,
|
|
'ALTER TABLE `llx_declarationtva_declarations` ADD COLUMN `validated_date` datetime DEFAULT NULL',
|
|
'SELECT "validated_date column already exists" as message'
|
|
));
|
|
PREPARE stmt FROM @sql;
|
|
EXECUTE stmt;
|
|
DEALLOCATE PREPARE stmt;
|
|
|
|
-- Check if validated_by column exists first
|
|
SET @sql = (SELECT IF(
|
|
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'llx_declarationtva_declarations'
|
|
AND COLUMN_NAME = 'validated_by') = 0,
|
|
'ALTER TABLE `llx_declarationtva_declarations` ADD COLUMN `validated_by` int(11) DEFAULT NULL',
|
|
'SELECT "validated_by column already exists" as message'
|
|
));
|
|
PREPARE stmt FROM @sql;
|
|
EXECUTE stmt;
|
|
DEALLOCATE PREPARE stmt;
|
|
|
|
-- Add indexes for performance (only if they don't exist)
|
|
SET @sql = (SELECT IF(
|
|
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'llx_declarationtva_declarations'
|
|
AND INDEX_NAME = 'idx_validated_date') = 0,
|
|
'ALTER TABLE `llx_declarationtva_declarations` ADD INDEX `idx_validated_date` (`validated_date`)',
|
|
'SELECT "idx_validated_date index already exists" as message'
|
|
));
|
|
PREPARE stmt FROM @sql;
|
|
EXECUTE stmt;
|
|
DEALLOCATE PREPARE stmt;
|
|
|
|
SET @sql = (SELECT IF(
|
|
(SELECT COUNT(*) FROM INFORMATION_SCHEMA.STATISTICS
|
|
WHERE TABLE_SCHEMA = DATABASE()
|
|
AND TABLE_NAME = 'llx_declarationtva_declarations'
|
|
AND INDEX_NAME = 'idx_validated_by') = 0,
|
|
'ALTER TABLE `llx_declarationtva_declarations` ADD INDEX `idx_validated_by` (`validated_by`)',
|
|
'SELECT "idx_validated_by index already exists" as message'
|
|
));
|
|
PREPARE stmt FROM @sql;
|
|
EXECUTE stmt;
|
|
DEALLOCATE PREPARE stmt;
|