PHP code example of alirezasalehizadeh / quick-migration

1. Go to this page and download the library: Download alirezasalehizadeh/quick-migration 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/ */

    

alirezasalehizadeh / quick-migration example snippets


// index.php

$connection = PDO connection object;

(new xMigration($connection))->migrate();

// index.php

$connection = PDO connection object;

(new xMigration($connection))->drop('table name');
// OR
(new xMigration($connection))->dropIfExists('table name');

$structure->id(string $name);
$structure->uuid(string $name, int $length);
$structure->ulid(string $name, int $length);
$structure->string(string $name, int $length);
$structure->number(string $name);
$structure->text(string $name);
$structure->timestamp(string $name);
$structure->timestamps();
$structure->json(string $name);
$structure->enum(string $name, array $enums);
$structure->array(string $name, array $values);
$structure->foreign(string $column)->reference(string $column)->on(string $table);

$structure->number('test')
->primary()                 // Set this as primary key
->nullable()                // Set this nullable or not
->unique()                  // Set this unique
->default(1)                // Set default value
->autoIncrement()           // Set this auto increment
->index()                   // Index this column
->unsigned()                // Set unsigned attribute
->after('column')           // Set this column after specific column
->check('test >= 0')        // Check a expression
->comment('this is test column')  // Set a comment

// TINYTEXT type not defined in `Type` enum

$structure = new StructureBuilder('table name');

$structure->tinyText('foo');
// ...

migrate();
dropIfExists(string $table);
drop(string $table);
createIndex(string $name, string $table, array $columns);    // It is used to index several columns together
dropIndex(string $name, string $table);
alterTable();
dropCheck(string $table, string $name);

$obj = new xMigration($connection);
$obj->dropIfExists('bar');
$obj->migrate();
echo $obj;

/**
DROP TABLE IF EXISTS `foo`.`bar`
CREATE TABLE `foo`.`bar` (`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, ...)
**/


$obj = new xMigration($connection);
$obj->dropIfExists('bar');
$obj->migrate();
$obj->export(string $fileName);

// Create a file named fileName.sql


$structure = new StructureBuilder('table name');

$structure->foreign('bar_id')->reference('id')->on('bar');
// OR
$structure->foreignBarId()->reference('id')->on('bar');
// ...

// index.php

$connection = PDO connection object;

(new xMigration($connection))->alterTable();
 php

use Alirezasalehizadeh\QuickMigration\Migration;
use Alirezasalehizadeh\QuickMigration\Structure\Structure;
use Alirezasalehizadeh\QuickMigration\Structure\StructureBuilder;

class xMigration extends Migration
{

    protected $database = "database name";

    protected $translator = "set database translator name from available translators array (default MySql)";

    public function set(): array
    {
        return Structure::create('table_name', function (StructureBuilder $structure) {
            // Write your structure here...
        });
    }

}

php index.php

php index.php
 php

use Alirezasalehizadeh\QuickMigration\Migration;
use Alirezasalehizadeh\QuickMigration\Structure\Structure;
use Alirezasalehizadeh\QuickMigration\Structure\TableAlter;

class xMigration extends Migration
{

    protected $database = "database name";

    protected $translator = "set database translator name from available translators array (default MySql)";

    public function set(): array
    {
        return Structure::change('table_name', function (TableAlter $alter) {
            // Write your commands for modify table...
        });
    }

}

php index.php