The CMDB includes a full SQL migration system. Each component (plugin, lib, core) can have versioned SQL files that are tracked, checksummed, and never replayed.
File Convention #
plugins/xchange/sql/migrations/001_initial.sql
plugins/xchange/sql/migrations/002_add_trades_table.sql
plugins/xchange/sql/migrations/003_add_rating_index.sql
lib/cmdb/sql/migrations/001_initial.sql
Files must be named with a sortable prefix. They are executed in alphabetical order.
Place a description in the first SQL comment line:
-- Description: Add trades table and buyer/seller indexes
CREATE TABLE IF NOT EXISTS xchange_trades ( ... );
Running Migrations #
// Single component
$result = CMDB::migrate(
'plugin', // component type
'xchange', // component name
'plugins/xchange/sql/migrations', // migrations directory
'1.2.0', // current version (optional)
'admin', // who runs this
'production' // environment tag
);
// $result = ['applied' => 2, 'skipped' => 1, 'failed' => 0, 'errors' => []]
// All components at once (scans lib/*, plugins/*)
$results = CMDB::migrateAll('deploy-server', 'production');
// $results = ['plugin:xchange' => [...], 'lib:cmdb' => [...], ...]
Checking Status #
// What migrations have been applied?
$status = CMDB::getMigrationStatus('plugin', 'xchange');
// Returns: migration_id, version, status, description, statements_count,
// execution_time_ms, applied_by, environment, batch, applied_at
// What's pending?
$pending = CMDB::getPendingMigrations('plugin', 'xchange', 'plugins/xchange/sql/migrations');
// Returns: ['003_add_rating_index.sql']
Integrity Check #
Detects if migration files were modified after being applied (checksum mismatch) or deleted:
$tampered = CMDB::verifyMigrationIntegrity();
foreach ($tampered as $t)
{
echo $t['component'] . ' / ' . $t['file'] . ': ' . $t['issue'];
// "plugin:xchange / 001_initial.sql: checksum_mismatch"
}
Behavior #
| Scenario | Behavior |
|---|
| File already applied | Skipped (checksum verified) |
| New file found | Executed in transaction, recorded |
| Statement fails | Transaction rolled back, status = failed, migration stops |
| File modified after apply | Warning in verifyMigrationIntegrity() |
| File deleted after apply | Reported as file_missing |
Batch Numbers #
All migrations executed in a single migrate() or migrateAll() call share the same batch number. This allows easy rollback identification.
Integration with Deployment #
On devarea.beamreactor.com, after syncing PHP files:
// Auto-run all pending migrations
$results = CMDB::migrateAll('devarea-deploy', 'production');
foreach ($results as $component => $r)
{
if ($r['failed'] > 0)
{
// Alert: migration failed for $component
trigger_error("Migration failed: $component — " . implode(', ', $r['errors']));
}
}