Skip to content

bskade/phinx-migrations-generator

 
 

Repository files navigation

Phinx migrations generator

Generate a migration by comparing your current database to your mapping information.

Latest Version on Packagist Software License Build Status Code Coverage Quality Score Total Downloads

Phinx cannot automatically generate migrations. Phinx creates "only" a class with empty up, down or change functions. You still have to write the migration manually.

In reality, you should rarely have to write migrations manually because the migration library should automatically generate migration classes by comparing your schema mapping information (i.e. how your database should look like) with your current database structure.

Features

  • Framework independent
  • DBMS: MySQL 5.7+ (only)
  • Initial schema and schema diff
  • Database: character set, collation
  • Tables: create, update, remove, engine, comment, character set, collation
  • Columns: create, update, remove
  • Indexes: create, remove
  • Foreign keys: create, remove, constraint name

Install

Via Composer

$ composer require odan/phinx-migrations-generator --dev

Usage

Generating migrations

On the first run, an inital schema and a migration class is generated. The schema.php file contains the previous database schema and is getting compared with the current schema. Based on the difference, a Phinx migration class is generated.

$ vendor/bin/phinx-migrations generate

By executing the generate command again, only the difference to the last schema is generated.

Parameters

Parameter Values Default Description
--name string The class name.
--overwrite bool Overwrite schema.php file.
--path string (from phinx) Specify the path in which to generate this migration.
--environment or -e string (from phinx) The target environment.

Running migrations

The Phinx migrate command runs all of the available migrations.

$ vendor/bin/phinx migrate

Configuration

The phinx-migrations-generator uses the configuration of phinx.

Migration configuration

Parameter Values Default Description
foreign_keys bool false Enable or disable foreign key migrations.

Example configuration

Filename: phinx.php (in your project root directory)

<?php

// Framework bootstrap code here
require_once __DIR__ . '/config/bootstrap.php';

// Get PDO object
$pdo = new PDO(
    'mysql:host=127.0.0.1;dbname=test;charset=utf8', 'root', '',
    array(
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_PERSISTENT => false,
        PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8 COLLATE utf8_unicode_ci',
    )
);

// Get migration path for phinx classes
$migrationPath = __DIR__ . '/resources/migrations';

return [
    'paths' => [
        'migrations' => $migrationPath,
    ],
    'foreign_keys' => false,
    'environments' => [
        'default_database' => 'local',
        'local' => [
            // Database name
            'name' => $pdo->query('select database()')->fetchColumn(),
            'connection' => $pdo,
        ]
    ]
];

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

License

The MIT License (MIT). Please see License File for more information.

About

Migration Code Generator for Phinx

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%