achrafsoltani/migration-service-provider

用于 Silex 的 Doctrine 迁移服务提供程序。基于 knplabs/migration-service-provider

v1.0.5 2015-05-27 14:57 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:31:59 UTC


README

Latest Stable Version Total Downloads License

这是一个简单的 silex 和 doctrine 的家酿模式迁移系统。

安装

安装说明

$ composer require achrafsoltani/migration-service-provider

设置

require_once __DIR__.'/vendor/autoload.php';

use Silex\Application;
use Silex\Provider\DoctrineServiceProvider;
use Gridonic\Provider\ConsoleServiceProvider;
use AchrafSoltani\Provider\MigrationServiceProvider;

$app = new Application();

$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
    'db.options' => array(
        // db options
    ),
));

$app->register(new ConsoleServiceProvider(), array(
    // console options
));

// Usage

$app->run();
$app->register(new MigrationServiceProvider(), array(
    'migration.path' => __DIR__.'/../src/Resources/migrations',
    'migration.register_before_handler' => true,
    'migration.migrations_table_name'   => 'migration_version',
    'migration.db' => $app['db']
));

迁移示例

<?php

// I'm using a custom namespace with a custom loader
namespace Core\User\Migration;

use Doctrine\DBAL\Schema\Schema;
use AchrafSoltani\Migration\AbstractMigration;
use Silex\Application;

// Custom class, demonstration only
use Core\User\Model\User;

/**
 * Class UsersMigration
 *
 * @package Core\User\Migration
 */
class UsersMigration extends AbstractMigration
{
    /**
     * @param Schema $schema
     */
    public function schemaUp(Schema $schema)
    {
        // before app is started, do the following.
        $table = $schema->createTable('users');
        $table->addColumn('id', 'integer', array(
            'unsigned'      => true,
            'autoincrement' => true
        ));
        $table->addColumn('username', 'string', array('notnull' => true, 'default' => '', 'length' => 100));
        $table->addColumn('password', 'string', array('notnull' => true, 'default' => '', 'length' => 255));
        $table->addColumn('roles', 'string', array('notnull' => true, 'default' => '', 'length' => 255));
        $table->setPrimaryKey(array("id"));
        $table->addUniqueIndex(array("username"));
    }
    
    public function appUp(Application $app)
    {
        // create default user
        // My model manager class takes a Doctrine\DBAL\Connection instance as parameter
        // This part is fully custom, for demontration purpose only
        $user_model = new User($app['db']);
        $user_data = array(
            'username' => 'achraf',
            'password' => $app['security.encoder.digest']->encodePassword('password', ''),
            'roles'    => 'ROLE_USER'
        );
        $user_model->create($user_data);
    }
    
    public function getMigrationInfo()
    {
        return 'Added users table';
    }
}

// Very important to add if you wanna organise your migration files within namespaced folders
// if not specified, the migration will be expected to be in the \Migration namespace
return __NAMESPACE__;

运行迁移

运行迁移有两种方式

使用 before 处理器

如果您在注册服务时传递了 migration.register_before_handler(设置为 true),则将为迁移注册一个 before 处理器。这意味着迁移管理器将在您的应用程序每次被访问时运行。

您可能希望为开发模式启用此行为,但在生产中请勿这样做!

使用 migration:migrate 命令

如果您正确安装了控制台服务提供程序,则可以使用 migration:migrate 命令,这样您的应用程序就不需要在每次调用 Web 应用程序时运行迁移。

编写迁移

迁移由一个文件组成,包含一个迁移类。按照设计,迁移文件必须命名为类似 <version>_<migration_name>Migration.php 的样子,并位于 src/Resources/migrations,类的名称为 <migration_name>Migration。例如,如果您的迁移向 foo 表添加了一个 bar 字段,并且是您模式中的第 5 个迁移,您应该将您的文件命名为 05_FooBarMigration.php,类名为 FooBarMigration

除了这些命名约定之外,您的迁移类必须扩展 Gridonic\Migration\AbstractMigration,它提供了一些帮助方法,例如 getVersion 和迁移方法的默认实现。

迁移方法包括 4 个方法,按此顺序调用

  • schemaUp
  • appUp
  • schemaDown
  • appDown

schemaUp

您将获得一个 Doctrine\DBAL\Schema\Schema 实例,其中您可以添加、删除或修改数据库模式。

appUp

schemaUp 之后,您可以编辑应用程序 - 您将获得一个 Silex\Application 实例。在这里,您可以在添加列后修改现有数据。

schemaDown

appUp 之后,您可以再次修改数据库的模式。您将获得一个可以使用的 Doctrine\DBAL\Schema\Schema 实例。

appDown

最后但同样重要的是,您可以再次使用一个 Silex\Application 实例。修改现有数据或类似的东西。

迁移信息

您应该了解的最后一个方法是 getMigrationInfos。此方法应返回对迁移的简单描述(它是可选的,您可以跳过其实现)。如果您使用 Twig,我们内置了一个 migration_infos 用于 twig - 可能是一个仅用于开发者模式的功能。

然后您可以使用类似以下内容

      Migration informations: {{ migration_infos }}

完整的 API 文档

许可证

MigrationServiceProvider 在 MIT 许可证下授权。原始库来自 KnpLabs