mac2000/silex_migrations_service_provider

此包已被废弃且不再维护。没有建议的替代包。
关于此包的最新版本(dev-master)没有可用的许可证信息。

Silex 迁移服务提供者

dev-master 2013-09-06 15:08 UTC

This package is not auto-updated.

Last update: 2021-09-13 01:14:28 UTC


README

此服务提供者将为您提供一个直接从浏览器迁移模式的能力。

安装

添加到您的 composer.json

"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"
},

注册服务提供者

$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
));

使用示例

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

迁移特质

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

运行测试

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

演示应用

在源代码中有一个 demo 应用,您可以在其中找到完整功能的示例应用。

您需要做的就是运行

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

迁移示例可以在以下位置找到: demo/Acme/Migrations/Version*.php

版本类示例

使用原始 SQL

可以在 demo/Acme/Migrations/Version*.php 中找到

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();
    }
}

您需要注意的一点是,您不能同时更改模式和数据。