PHP code example of safronik / db-migrator

1. Go to this page and download the library: Download safronik/db-migrator 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/ */

    

safronik / db-migrator example snippets


namespace Safronik\DBMigrator;

use Safronik\DBMigrator\Objects\Table;

interface DBMigratorGatewayInterface
{
    public function isTableExists( $table ): bool;
    public function createTable( Table $table ): bool;
    public function getTablesNames(): array;
    public function getTableColumns( string $table ): array;
    public function getTableIndexes( string $table ): array;
    public function getTableConstraints( string $table ): array;
    public function alterTable( $table, array $columns = [], array $indexes = [], array $constraints = [] ): bool;
    public function dropTable( $table ): bool;
}

class DBMigratorGatewayInterfaceImplementation implements \Safronik\DBMigrator\DBMigratorGatewayInterface
{
    // Your implementation of the interface
}

$migrator = new DBMigrator( $migrator_gateway );
$schemas  = $migrator->getCurrentSchemas();

$schemas = new Schemas([
    new Table(
        'example_table',
        
        // Columns 
        [
            new Column([
                'field'   => 'id',      // Required
                'type'    => 'INT(11)', // Required
                'null'    => 'no',  
                'default' => ''   
                'extra'   => 'AUTO INCREMENT',
                'comment' => 'Primary key',
            ]),            
            new Column([
                'field'   => 'value_field', // Required
                'type'    => 'TEXT',        // Required
                'null'    => 'yes',  
                'default' => 'null',   
                'extra'   => '',
                'comment' => 'Desc',
            ]),            
            // ...
        ],
        
        // Indexes (optional)
        [
            new Index([
                'key_name' => 'PRIMARY', // Required
                'columns'  => ['id'],    // Required
                'unique'   => true,
                'type'     => 'BTREE',
                'comment'  => 'Primary key',
            ]),
            // ...    
        ],
        
        // Constraints (optional)
        [
            new Constraint([
                'name'             => 'Example constraint', // Required
                'column'           => 'id',                 // Required
                'reference_table'  => 'examples2',          // Required
                'reference_column' => 'example_id',         // Required
                'on_update'        => '',
                'on_delete'        => '',
            ]),
            // ...    
        ],
    )
]);

$migrator = new DBMigrator( 
    new DBMigratorGatewayInterfaceImplementation() 
);

$migrator
    ->setSchemas( $schemas )
    ->compareWithCurrentStructure()
    ->actualizeSchema();

$migrator = new DBMigrator( 
    new DBMigratorGatewayInterfaceImplementation() 
);

$migrator
    ->setSchemas( $migrator->getCurrentSchemas() )
    ->dropSchema();