【问题标题】:Can I use two different environments in a Phinx migration?我可以在 Phinx 迁移中使用两个不同的环境吗?
【发布时间】:2020-02-08 17:53:28
【问题描述】:

我正在使用 phinx 来管理我的数据库,我需要从一个数据库中收集数据并将其插入到另一个数据库中。

我在配置文件中定义了两个环境,如下所示:

'environments' => [
        'default_database' => 'current',
        'current' => [
            'adapter' => 'mysql',
            'host' => '127.0.0.1',
            'name' => 'old',
            'user' => 'root',
            'pass' => '*****',
            'port' => '3306',
            'charset' => 'utf8',
        ],
        'new' => [
          'adapter' => 'mysql',
          'host' => '127.0.0.1',
          'name' => 'new',
          'user' => 'root',
          'pass' => '*****',
          'port' => '3306',
          'charset' => 'utf8',
        ]
    ],

我想要实现的是这样的:

public function up () {

    // The environment is 'current' by default
    $data = $this->fetchAll("SELECT * FROM old_table WHERE x");

    // Change environment somehow
    $this->environment('new')

    $this->table('new_table')->insert($data);
}

这可能吗?我在official documentation 上找不到任何内容。

【问题讨论】:

    标签: php database-migration phinx


    【解决方案1】:

    查看 phinx 代码,他们在执行迁移时会这样做

        public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false)
    {
        $direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN;
        $migration->setMigratingUp($direction === MigrationInterface::UP);
    
        $startTime = time();
        $migration->setAdapter($this->getAdapter());
    

    您已在迁移中使用了 setAdaptor,因此您或许可以使用它。本来希望在评论中写这个,因为这不是一个真正的答案,但我没有足够的字符

    【讨论】:

      猜你喜欢
      • 2015-07-14
      • 1970-01-01
      • 2019-06-29
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 1970-01-01
      • 2020-11-01
      • 2021-03-11
      相关资源
      最近更新 更多