Run your migration quickly with Quick Migration!
PHP >= 8.1
- MySql
- PostgreSql
via Composer:
composer require alirezasalehizadeh/quick-migration
Create a xMigration
class like this that must extends from \Alirezasalehizadeh\QuickMigration\Migration
class:
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...
});
}
}
Next, create a object from xMigration
class and run migrate
method:
// index.php
$connection = PDO connection object;
(new xMigration($connection))->migrate();
php index.php
// index.php
$connection = PDO connection object;
(new xMigration($connection))->drop('table name');
// OR
(new xMigration($connection))->dropIfExists('table name');
php index.php
$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);
*NOTE: See the Structure Test file for examples
$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
Sometimes it happens that you need a specific type of column that is not available in Type
enum and you have to create it manually. QuickMigration
has provided you with a quick and easy way to create a specific type of column!
To create a column, it is enough to set the method name
equal to the column type
and write the column name
in the first argument
, like this:
// 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);
*NOTE: See the Command Test file for examples
You can get the sql`s by call the migration class object as string:
$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, ...)
**/
You can use export
method, for get your sql`s in a file:
$obj = new xMigration($connection);
$obj->dropIfExists('bar');
$obj->migrate();
$obj->export(string $fileName);
// Create a file named fileName.sql
A quick way to create a foreignkey is this that the name of the method
must to beforeign + {foreignColumnName}
:
$structure = new StructureBuilder('table name');
$structure->foreign('bar_id')->reference('id')->on('bar');
// OR
$structure->foreignBarId()->reference('id')->on('bar');
// ...
Now, for modify your tables can use change
method on Structure
:
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...
});
}
}
// index.php
$connection = PDO connection object;
(new xMigration($connection))->alterTable();
php index.php
*NOTE: See the Table Alter Test file for examples
Send pull request or open issue for contributing.
MIT.