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 = '';

#[Index('idx_status_date', columns: ['status', 'created_at'], renamedFrom: 'idx_status', renamedSince: '1.4.0')]

#[Absent(index: ['idx_legacy_sku', 'idx_legacy_isbn'], since: '1.4.0')]
#[Absent(column: 'po_id', since: '2.0.0')]

#[Unmanaged(index: 'idx_dba_status_covering')]

$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 …");
});

$schema = TableSchema::fromClass(SlotSpace::class)->extendedWith(
    columns: ['dim_loc' => new ColumnDefinition(name: 'dim_loc', /* … */)],
    indexes: ['idx_dim_loc' => ['active', 'dim_loc']],
);

$migrator->plan([Order::class, Supplier::class, $schema]);

#[Table(name: 'app_slotspace')]
final class SlotSpace extends Record implements PartiallyDeclared {}

$fp = $migrator->fingerprint($classes);   // == $plan->fingerprint

#[Table(name: 'myapp_schema_runs')]
final class MyRunRecord extends SchemaRunRecord {}

$migrator = new SchemaMigrator($connection, runRecordClass: MyRunRecord::class);