studioemma / migrations

此包已被弃用,不再维护。未建议替代包。

Pimcore 4 迁移

1.2.4 2017-05-17 06:57 UTC

This package is auto-updated.

Last update: 2020-02-14 10:28:08 UTC


README

Pimcore只为自己的更新生成迁移。它们支持导入和导出类定义和其他,但所有操作都通过GUI完成。Pimcore没有内置的“真实”迁移支持。

安装插件

要使用迁移,请确保此处找到的文件和文件夹放置在您的pimcore项目内的plugins\Migrations文件夹中。

或者您可以通过composer安装插件

稳定版

composer require studioemma/migrations

不稳定版

composer require studioemma/migrations:dev-master

安装

Pimcore迁移包含内置的CLI安装器,以便于使用。

安装器接受与基于Web的安装器相同的参数,但在安装完成后还会启用此Migrations插件。这使我们能够扩展自动化。

要运行安装器,您可以执行php plugins/Migrations/cli/console.php install

示例

$ php plugins/Migrations/cli/console.php install --help
Usage:
  install [options]

Options:
      --db-adapter=DB-ADAPTER          what adapter to use (Mysqli or Pdo_Mysql)
      --db-host=DB-HOST                db host or socket
      --db-port[=DB-PORT]              db port [default: 3306]
      --db-username=DB-USERNAME        db username
      --db-password=DB-PASSWORD        db password
      --db-database=DB-DATABASE        db database name
      --admin-username=ADMIN-USERNAME  admin username
      --admin-password=ADMIN-PASSWORD  admin password
  -h, --help                           Display this help message
  -q, --quiet                          Do not output any message
  -V, --version                        Display this application version
      --ansi                           Force ANSI output
      --no-ansi                        Disable ANSI output
  -n, --no-interaction                 Do not ask any interactive question
  -v|vv|vvv, --verbose                 Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
 Migrations installer

安装

$ php plugins/Migrations/cli/console.php install \
    --db-adapter Pdo_Mysql \
    --db-host mysql \
    --db-username root \
    --db-password toor \
    --db-database pimcore \
    --admin-username admin \
    --admin-password admin
successfully installed pimcore

迁移

迁移可以向上或向下运行,如果需要,您可以指定版本。指定版本后,工具将自动检测方向,但当前方向仍然是必需的。

注意:此迁移工具也可以运行“系统”更新。这些迁移只能向上。当您向下运行迁移时,系统迁移将保持原位,因为您通常将Pimcore版本保持在那个修订版。

待办事项

  • 强制向下(同时获取系统迁移)

当您使用CLI安装器时,Migrations插件将自动启用。如果您在项目后期引入此插件,您必须先启用插件才能运行任何迁移。要启用插件,请转到“菜单 extras”(目前是heart图标)-> “扩展”并确保您启用了ID为Migrations的插件。

示例运行

$ php plugins/Migrations/cli/console.php migrate --help
Usage:
  migrate <direction> [<to>]

Arguments:
  direction             The migration direction up/down
  to                    up to what migration version

Options:
  -h, --help            Display this help message
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi            Force ANSI output
      --no-ansi         Disable ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Help:
 Migrations

迁移向上或向下

$ php plugins/Migrations/cli/console.php migrate up
Running migrations up
migrations run from 0 to 5

迁移到版本(示例中方向向上但检测到向下)

$ php plugins/Migrations/cli/console.php migrate up 3
Running migrations up
migrations run from 5 to 3

创建迁移

迁移文件必须位于您的网站文件夹中。更准确地说,您必须在website/lib/Website/Migrations中放置迁移。您必须按<number>-<ClassName>.php的格式命名您的迁移,例如001-FirstMigration.php

您可以使用内置的骨架生成器轻松完成此操作

$ php plugins/Migrations/cli/console.php generate YourMigrationClassName
Successfully created migration file 001-YourMigrationClassName.php

如果需要迁移数据文件夹,请添加-d选项

$ php plugins/Migrations/cli/console.php generate YourMigrationClassName -d
Successfully created migration data folder 001-YourMigrationClassName
Successfully created migration file 001-YourMigrationClassName.php

在迁移文件内部使用的类名必须以PMigration_<ClassName>开头。例如PMigration_FirstMigration

理论上,迁移只需实现\Migrations\Migration即可工作。但您可能希望扩展\Migrations\Migration\AbstractMigration,因为那里有一些很好的额外功能。

如果您正在执行系统迁移,您的迁移文件还必须实现\Migrations\SystemMigration

示例迁移

<?php

use Migrations\Migration\AbstractMigration;

class PMigration_FirstMigration extends AbstractMigration
{
    public function up()
    {
        // do something
    }

    public function down()
    {
        // do the reverse
    }
}

示例系统迁移

<?php

use Migrations\Migration\AbstractMigration;

class PMigration_PimcoreRevision3689 extends AbstractMigration
    implements \Migrations\SystemMigration
{
    public function getStartRevision()
    {
        return 3675;
    }

    public function getEndRevision()
    {
        return 3689;
    }

    public function up()
    {
        $this->db->query("ALTER TABLE `classificationstore_relations` ADD INDEX `groupId` (`groupId`);");

        $this->db->query("ALTER TABLE `classificationstore_collectionrelations` ADD INDEX `colId` (`colId`);");
    }

    public function down()
    {
        // down will not be executed but must exist
    }
}