|
- <?php
-
- declare(strict_types=1);
-
- use Core\Database;
- use Core\Migration;
-
- return new class extends Migration
- {
- public function up(Database $database): void
- {
- $tableExists = $database->first(
- "SELECT 1 AS tbl FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'campaign_audit'"
- );
-
- if ($tableExists) {
- return;
- }
-
- // No foreign key on id: audit records must survive deletion of the
- // campaign row they reference (that deletion is itself audited as 'D').
- $database->execute(
- "CREATE TABLE campaign_audit (
- audit_id INT IDENTITY(1,1) NOT NULL,
- id INT NOT NULL,
- action CHAR(1) NOT NULL,
- fields NVARCHAR(MAX) NOT NULL,
- username NVARCHAR(255) NOT NULL DEFAULT 'system',
- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
- CONSTRAINT PK_campaign_audit PRIMARY KEY (audit_id),
- CONSTRAINT CHK_campaign_audit_action CHECK (action IN ('I','U','D','R'))
- )"
- );
-
- $database->execute(
- 'CREATE INDEX IX_campaign_audit_id ON campaign_audit (id)'
- );
- }
-
- public function down(Database $database): void
- {
- $database->execute('DROP TABLE IF EXISTS campaign_audit');
- }
- };
|