PHP code example of mongoose-studio / phobos-framework-database-migrations

1. Go to this page and download the library: Download mongoose-studio/phobos-framework-database-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/ */

    

mongoose-studio / phobos-framework-database-migrations example snippets



use PhobosFramework\Migrations\Migration;
use PhobosFramework\Migrations\Schema\Schema;
use PhobosFramework\Migrations\Schema\Blueprint;

return new class extends Migration {
    public function up(): void
    {
        Schema::create('cuentas', function (Blueprint $t) {
            $t->uuidv7('id')->primary();          // calza con keyStrategy = "uuidv7"
            $t->string('codigo', 32)->unique();
            $t->string('nombre', 160);
            $t->uuid('cuenta_padre_id')->nullable();
            $t->boolean('imputable')->default(true);
            $t->json('meta')->nullable();          // jsonb · json · TEXT según el motor
            $t->auditColumns();                     // convención Phobos, en una línea
            $t->foreign('cuenta_padre_id')->references('id')->on('cuentas')->nullOnDelete();
            $t->index('nombre');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('cuentas');
    }
};

$t->json('meta')->rawType('pgsql', 'jsonb');      // tipo crudo por motor
Schema::raw("CREATE INDEX ix_meta ON cuentas USING gin (meta)", 'pgsql'); // no-op en otros

use PhobosFramework\Migrations\Schema\Harmonia;

// DSL → Harmonia (JSON)
$json = Schema::toJson('cuentas', function (Blueprint $t) {
    $t->uuidv7('id')->primary();
    $t->string('codigo', 32)->unique();
    $t->json('meta')->nullable();
});

// Harmonia (JSON) → aplicar (ESTRICTO por defecto)
Schema::fromJson($json);

// A bajo nivel:
$bp = Harmonia::decode($json);        // estricto
$json = Harmonia::encode($bp);

Schema::fromJson($jsonDeConfianza, strict: false);   // permite rawType

use PhobosFramework\Migrations\Seeder;

class PermisosSeeder extends Seeder {
    public function run(): void {
        $this->insertOrIgnore('permissions', [
            ['code' => 'finco.cuentas.view'],
            ['code' => 'finco.cuentas.manage'],
        ]);

        $this->upsert('plans',
            [['code' => 'starter', 'rate_limit' => 500]],
            uniqueBy: ['code'],
        );
    }
}


PhobosFramework\Database\Drivers\Postgres\PostgresDriver;

dbConfig(
    connections: ['main' => [ /* ... driver, host, database, ... */ ]],
    drivers: ['pgsql' => new PostgresDriver()],
    default: 'main',
);

return [
    'path'       => __DIR__ . '/database/migrations',
    'connection' => null,          // null = conexión por defecto
    'seeders'    => __DIR__ . '/database/seeders',
];

use PhobosFramework\Migrations\Introspection\Introspector;

$intro = Introspector::for();            // motor de la conexión activa
$intro->tables();                         // ['cuentas', ...]
$json = $intro->toHarmonia('cuentas');    // DB → Harmonia (JSON)
$bp   = $intro->table('cuentas');         // DB → Blueprint

$bp = Introspector::for('mysql_src')->table('cuentas');
$pg = Schema::pretendFor('pgsql', fn() => Schema::build($bp)); // MySQL → Postgres

use PhobosFramework\Migrations\Schema\Diff\SchemaDiff;
use PhobosFramework\Migrations\Schema\Diff\SchemaSnapshot;

// Dos fotos del esquema (introspectando conexiones vivas)
$diff = SchemaDiff::connections('prod', 'dev');   // o SchemaDiff::between($snapA, $snapB)

echo $diff->report();          // reporte legible (+/-/~ por tabla y columna)
$diff->toSql('pgsql');         // el delta aplicable como SQL
$diff->toHarmonia();           // el delta aplicable como Harmonia (JSON)
$diff->notes();                // lo que hay que revisar a mano (ver abajo)

use PhobosFramework\Migrations\Migrator;

foreach ($tenantConnections as $name) {
    (new Migrator(__DIR__ . '/database/migrations', $name))->run();
}