Based on your Doctrine entity definition, the original ALTER TABLE statement will not work correctly due to a mismatch in default values. Here's why and how to fix it:
Caution: Ensure existing data in file_link is compatible with VARCHAR(255) (e.g., no values exceed 255 characters). If file_link contained NULLs, the NOT NULL constraint will cause the ALTER to fail.
Correct: Adds two new columns with proper defaults and constraints.
Order: New columns are appended to the table. Doctrine ORM annotations ((DC2Type:...)) are comments only and don’t affect functionality.
4. Potential Issues:
NOT NULL on file_path: If file_link had NULL values, this will fail. Fix by updating NULLs first:
UPDATE invoice_document SET file_link = '' WHERE file_link IS NULL;
ENUM Values: Ensure 'not_required' is a valid value in the ENUM (it is, as listed first).
Backwards Compatibility: If applications rely on file_link or updated_on having old defaults/test data, test thoroughly.
Final Verdict:
✅ The SQL is valid and safe to execute if:
Existing file_link values are ≤ 255 characters and not NULL.
You’ve tested the changes in a non-production environment.
If file_link has NULLs, handle them before running the ALTER. Otherwise, proceed confidently!
Entity Default:
Your entity sets the default value for $emailStatus to InvoiceDocumentEmailStatus::PENDING (which maps to the string 'pending').
Database Default:
Your ALTER TABLE sets the database default to 'not_required'.
Conflict:
When Doctrine creates a new record:
It will insert 'pending' (from the entity's default).
But if you insert a record directly via SQL, it will use 'not_required' (from the database default).
This inconsistency can cause data integrity issues.
Solution:
Update the email_status column definition in your ALTER TABLE to match the entity's default:
ALTER TABLE invoice_document
ADD email_sent_at DATETIME DEFAULT NULL COMMENT '(DC2Type:datetime_immutable)',
ADD email_status ENUM('not_required', 'pending', 'sent', 'failed') DEFAULT 'pending' NOT NULL, -- Changed here
CHANGE updated_on updated_on DATETIME DEFAULT CURRENT_TIMESTAMP COMMENT '(DC2Type:datetime_immutable)',
CHANGE file_link file_path VARCHAR(255) NOT NULL
Key Notes:
ENUM Values:
Ensure the ENUM values in the database match your InvoiceDocumentEmailStatus enum class. If your enum uses:
public const NOT_REQUIRED = 'not_required';
public const PENDING = 'pending';
// etc.
The database ENUM values are correct.
Default Value:
The database default must be the string representation of your enum constant ('pending'), not the PHP constant (InvoiceDocumentEmailStatus::PENDING).
Existing Data:
If the table has existing rows:
New rows will use 'pending' as the default.
Existing rows will retain their current values (if any) or become 'pending' if NULL.
Doctrine Mapping:
The enumType in your entity ensures Doctrine correctly maps the database string to your PHP enum.
Why This Works:
Doctrine will now use the same default value ('pending') whether inserting via the entity or directly via SQL.
The NOT NULL constraint is compatible with your entity's non-nullable property.
The ENUM type ensures only valid enum values are accepted.
After applying this fix, your ALTER TABLE statement will work seamlessly with the Doctrine entity.
The original ALTER TABLE statement will work correctly with this entity. Here's why:
1. Enum Mapping
ADD email_status ENUM('not_required', 'pending', 'sent', 'failed') DEFAULT 'not_required' NOT NULL
✅ Database ENUM values match your PHP enum constants:
✅ Doctrine will correctly map database strings to your PHP enum via enumType
2. Default Value Alignment
✅ Database default: 'not_required'
✅ Entity default: InvoiceDocumentEmailStatus::NOT_REQUIRED (which maps to 'not_required')
✅ Both use the same string representation ('not_required')
3. Nullability
✅ Database: NOT NULL
✅ Entity: Non-nullable property with default value
✅ No conflicts
4. Type Consistency
✅ Database: ENUM with string values
✅ Doctrine: Types::STRING + enumType
✅ Doctrine will handle the enum ↔ string conversion automatically
Key Considerations
Enum Implementation
Ensure your InvoiceDocumentEmailStatus enum matches the database values:
enum InvoiceDocumentEmailStatus: string
{
case NOT_REQUIRED = 'not_required';
case PENDING = 'pending';
case SENT = 'sent';
case FAILED = 'failed';
}
Existing Data
If the table has existing rows:
New rows will use 'not_required' as default
Existing rows will retain their current values (if valid enum values)
Doctrine Behavior
When inserting via Doctrine:
Uses the entity default (InvoiceDocumentEmailStatus::NOT_REQUIRED)
→ Becomes 'not_required' in the database
When inserting via SQL:
Uses the database default ('not_required')
→ Becomes InvoiceDocumentEmailStatus::NOT_REQUIRED in Doctrine
Final Verdict
✅ The ALTER TABLE statement is fully compatible with your Doctrine entity. The enum mapping, default values, and null constraints align perfectly. Proceed with confidence!