2017 © Pedro Peláez
 

library silex_migrations_service_provider

Silex Migrations Service Provider

image

mac2000/silex_migrations_service_provider

Silex Migrations Service Provider

  • Friday, September 6, 2013
  • by mac2000
  • Repository
  • 1 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Silex Migrations Service Provider

This service provider will give you ability to easily migrate your schema directly from browser., (*1)

Installation

Add to your composer.json:, (*2)

"minimum-stability": "dev",
"repositories": [
    {
        "type": "package",
        "package": {
            "name": "mac2000/silex_migrations_service_provider",
            "version": "dev-master",
            "source": {
                "type": "git",
                "url": "git://github.com/mac2000/silex_migrations_service_provider.git",
                "reference": "master"
            },
            "autoload": {
                "psr-0": {
                    "": "src"
                }
            }
        }
    }
],
"require": {
    "silex/silex": "1.*",
    "mac2000/silex_migrations_service_provider": "dev-master"
},

Register Service Provider

$app->register(new MigrationsServiceProvider(), array(
    'migration.table' => 'version', // Optional argument - table name where migrations log will be stored, will be created automatically, default value is: doctrine_migration_versions
    'migration.namespace' => 'Acme\\Migration', // Namespace where your migration classes can be found, do not forget about slash escaping and do not add last slash
    'migration.directory' => 'src/Acme/Migration' // Directory where your migration classes can be found
));

Usage examples

$versions = $app['migration']->getSql();
$versions = $app['migration']->migrate();

Migrations Trait

use MigrationsTrait;
...
$app->migration()->getSql();
$app->migration()->migrate();

Run tests

vendor/bin/phpunit
vendor/bin/phpunit --coverage-html ./report

Demo Application

There is demo application in source where you can find fully functional application example., (*3)

The only thing you need to do is run:, (*4)

mysql -uroot -proot -e "CREATE DATABASE silex_migrations_service_provider_example"

Migration examples can be found here: demo/Acme/Migrations/Version*.php, (*5)

Version class examples

Using Raw SQL

Can be found in demo/Acme/Migrations/Version*.php, (*6)

Doctrine

class Version1 extends AbstractMigration
{
    public function up(Schema $schema)
    {
        $people = $schema->createTable('people');
        $people->addColumn('id', 'integer', array('unsigned' => true, 'autoincrement' => true));
        $people->addColumn('first_name', 'string', array('length' => 128));
        $people->addColumn('last_name', 'string', array('length' => 128));
        $people->setPrimaryKey(array('id'));
    }

    public function postUp(Schema $schema)
    {
        $this->connection->insert('people', array(
            'first_name' => 'Alexandr',
            'last_name' => 'Marchenko'
        ));

        $this->connection->insert('people', array(
            'first_name' => 'Maria',
            'last_name' => 'Marchenko'
        ));
    }

    public function down(Schema $schema)
    {
        $schema->dropTable('people');
    }
}

class Version2 extends AbstractMigration {

    public function up(Schema $schema)
    {
        $people = $schema->getTable('people');
        $people->addColumn('full_name', 'string', array('length' => 256));
    }

    public function postUp(Schema $schema) {
        $this->connection->createQueryBuilder()->update('people')->set('full_name', "CONCAT(first_name, ' ', last_name)")->execute();
    }

    public function down(Schema $schema)
    {
        $people = $schema->getTable('people');
        $people->dropColumn('full_name');
    }
}

class Version3 extends AbstractMigration {

    public function up(Schema $schema)
    {
        $people = $schema->getTable('people');
        $people->dropColumn('first_name');
        $people->dropColumn('last_name');
    }

    public function down(Schema $schema)
    {
        $people = $schema->getTable('people');
        $people->addColumn('first_name', 'string', array('length' => 128));
        $people->addColumn('last_name', 'string', array('length' => 128));
    }

    public function postDown(Schema $schema) {
        $this->connection->createQueryBuilder()->update('people')->set('first_name', "SUBSTRING_INDEX(full_name, ' ', 1)")->set('last_name', "SUBSTRING_INDEX(full_name, ' ', -1)")->execute();
    }
}

What you should notice is that you can not change schema and data at same time., (*7)

The Versions

06/09 2013

dev-master

9999999-dev

Silex Migrations Service Provider

  Sources   Download

The Requires

 

The Development Requires

by Marchenko Alexandr