phpcr/phpcr-migrations-bundle

1.6.0 2024-04-05 15:47 UTC

This package is auto-updated.

Last update: 2024-09-05 16:51:19 UTC


README

此库为PHPCR 迁移库提供了 Symfony 集成。

它最初由 Daniel Leech 创建,作为 dantleech/phpcr-migrations-bundle,后来捐赠给了 PHPCR 组织。

配置

配置迁移路径

# config/packages/phpcr-migrations.yaml
phpcr_migrations:
    paths: [%kernel.project_dir%/phpcr-migrations]

然后,包将自动检测在内核中注册的任何包中的 Resources/phpcr-migrations 文件夹中的任何迁移。

创建迁移

首先创建两个新的迁移文件

<?php
// phpcr-migrations/Version201501011200.php

use PHPCR\SessionInterface;
use PHPCR\Migrations\VersionInterface;

class Version201501011200 implements VersionInterface
{
    public function up(SessionInterface $session): void
    {
        $session->getRootNode()->addNode('hello');
    }

    public function down(SessionInterface $session): void
    {
        $session->getRootNode()->getNode('hello')->remove();
    }
}

<?php
// app/phpcr-migrations/Version201501011212.php

use PHPCR\SessionInterface;
use PHPCR\Migrations\VersionInterface;

class Version201501011212 implements VersionInterface
{
    public function up(SessionInterface $session): void
    {
        $session->getNode('/hello')->addNode('world');
    }

    public function down(SessionInterface $session): void
    {
        $session->getNode('/hello')->getNode('world')->remove();
    }
}

迁移状态

请注意,迁移必须命名为以下格式:VersionYYYMMDDHHSS。如果它们不是这样的名称,将无法检测到。时间戳应该是当前日期(在这个例子中是 2015/01/01 12:00)。

现在执行 phpcr:migrations:status 命令

$ php app/console phpcr:migrations:status
+--+---------------+------------------+----------+----------------------------------------------+
|  | Version       | Date             | Migrated | Path                                         |
+--+---------------+------------------+----------+----------------------------------------------+
|  | 201501011200 | 2015-01-01 12:00 | NO       | app/phpcr-migrations/Version201501011200.php |
|  | 201501011212 | 2015-01-01 12:12 | NO       | app/phpcr-migrations/Version201501011212.php |
+--+---------------+------------------+----------+----------------------------------------------+
No migrations have been executed

执行迁移

现在我们可以运行迁移了

$ php app/console phpcr:migrations:migrate
Upgrading 2 version(s):
 + [1/2]: 201501011200
 + [2/2]: 201501011212

这将运行这两个迁移。

回滚

您现在可以按如下方式回滚到第一个版本

$ php app/console phpcr:migrations:migrate 201501011200
Reverting 1 version(s):
 - [1/4]: V201501011212

操作

除了指定版本,您还可以指定操作

$ php app/console phpcr:migrations:migrate up
Upgrading 1 version(s):
 - [1/4]: V201501011212

操作包括:

  • up:升级一个版本
  • down:回滚一个版本
  • top:迁移到最新版本
  • bottom:回滚所有迁移