PHP code example of nandan108 / attrecord-migrations
1. Go to this page and download the library: Download nandan108/attrecord-migrations library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
nandan108 / attrecord-migrations example snippets
use Nandan108\AttrecordMigrations\SchemaMigrator;
use Nandan108\AttrecordMigrations\Plan\ChangeClass;
$migrator = new SchemaMigrator($connection); // an attrecord Connection
// plan() is PURE: reads the catalog, executes nothing. Always safe to call.
$plan = $migrator->plan([OrderRecord::class, SupplierRecord::class]); // any order — see below
$plan->isEmpty(); // fast path — nothing to do
foreach ($plan->changes as $c) { // inspectable: SQL + classification + reason
printf("[%s] %s %s.%s — %s\n", $c->class->value, $c->kind, $c->table, $c->subject, $c->reason);
}
$migrator->apply($plan); // Safe changes only (default)
$migrator->apply($plan, allow: ChangeClass::Destructive); // opt-in escalation
$migrator->apply($plan, allow: ChangeClass::Assisted); // everything with a statement
#[Column(ColumnType::VarChar, length: 64, renamedFrom: 'sku_code')]
public string $sku = '';
$plan = $migrator->plan($classes)
// MySQL's MODIFY … JSON validates existing values, so wrap them first.
->withStep(
before: 'modify_column orders.payload',
run: fn (DbSession $s) => $s->exec("UPDATE orders SET payload = JSON_OBJECT('data', payload)"),
)
// The other placement: backfill a column that did not exist a statement ago.
->withStep(
after: 'add_column orders.source_ref_type_id',
run: fn (DbSession $s) => $s->exec('UPDATE orders SET source_ref_type_id = 1 WHERE source_id IS NOT NULL'),
);
$migrator->apply($plan);
$migrator->dataStep('2026-07-wrap-payload-json', function ($session): void {
$session->exec("UPDATE orders SET payload = JSON_OBJECT('data', payload) WHERE …");
});
#[Table(name: 'myapp_schema_runs')]
final class MyRunRecord extends SchemaRunRecord {}
$migrator = new SchemaMigrator($connection, runRecordClass: MyRunRecord::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.