2

I'm using phinx to manage my databases and I need to gather data from a database and insert it into another one.

I have defined the two environments in a config file like so:

'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',
        ]
    ],

What I'm trying to achieve is something like this:

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

Is this possible ? I can't find anything on the official documentation.

1 Answers1

0

Looking in phinx code they do this when they execute a migration

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

you have setAdaptor available in a migration, so you might be able to use that. Would have prefered to write this in a comment as it's not really an answer but I didn't have enough charactors

Ebski
  • 236
  • 1
  • 10