phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Multiple Databases

Open wpillar opened this issue 12 years ago • 97 comments

It would be cool if Phinx could handle migrations for different databases all from the same instance. In our company setup we have one application which uses n databases, with different migrations for each that need to be handled.

What are you thoughts on handling multiple databases? I might fork and see if I can get it working but I want to know if you'd ever merge something like that?

wpillar avatar Feb 02 '14 16:02 wpillar

Hi @wpillar, in it's present state you can "hack" Phinx to support multiple databases, but only using raw SQL. We would have to do work to improve the native PHP functions. Feel free to fork, if enough people wanted this feature then I would consider adding it.

robmorgan avatar Feb 03 '14 10:02 robmorgan

Ah I see, I'll implement a nice way to specify the database from each migration.

There's a step further we can take to really make this the only migrations library you'll ever need, the ability to override default environments on a per-database basis. For example, with our application we have n databases across n servers, would be amazing if Phinx could handle migrations for all of that.

What do you think?

wpillar avatar Feb 03 '14 17:02 wpillar

It is really necessary option

ivigasin avatar Mar 06 '14 10:03 ivigasin

+1 for this feature.

farnoux avatar Mar 13 '14 10:03 farnoux

+1 for this feature!

eko3alpha avatar Apr 04 '14 00:04 eko3alpha

+1 for this feature!

kornrunner avatar Apr 22 '14 23:04 kornrunner

+1, would love this feature!

Spadbob avatar May 01 '14 20:05 Spadbob

As per our email @robmorgan - I would like to see multiple databases per environment, and have migrations only executed for a specific environment.

I'd like to see environment sub-folders as an option, such as:

migrations/users/file.php
migrations/products/file.php
migrations/file.php

Where 'users' and 'products' are different environments and thus have different databases, however if you look at the third option it's global implying it will be executed for all environments.

What are your thoughts here @robmorgan ?

dragoonis avatar Jun 12 '14 07:06 dragoonis

Ok guys the hardest thing for me to visualize with this feature is how we manage multiple db's across multiple environments? I see environments as 'testing', 'staging' & 'production'. 99% of the time I want to run the same migrations on each environment to develop, test and finally release it. I believe people want to introduce the concept of conditional migrations that may/may not run depending on the env?

robmorgan avatar Jun 13 '14 10:06 robmorgan

So I actually ended up writing our own migration library with multiple DB support, it's not public yet though. The way we handled multiple DBs is with config files for each environment.

config_dev.yml config_staging.yml config_live.yml

Then we passed those config files to the console application:

/bin/migrate migrate config_dev.yml

And use our build tool (Jake) to wrap that command for each environment so we run one command and it figures out what environment it is.

And in each of those files it the connection details for all the databases. I don't think migrations should be conditional on environment and I don't see why that would be the desired functionality? Surely if you're making a schema change and going from dev -> live it needs to go through the release pipeline and be the same for each environment?

wpillar avatar Jun 13 '14 10:06 wpillar

I was thinking maybe this could work. Allow for a directory structure like so:

/migrations/ [ database name ] / [ phinx yml config]

/migrations/db-1/phinx.yml
/migrations/db-2/phinx.yml
/migrations/db-3/phinx.yml

and when creating a new migration for db-1 you could do something like this

/bin/phinx db-1 create RenameLabelToAttribute

this would create a new file

/migrations/db-1/20140330133629_rename_label_to_attribute.php

And to migrate

/bin/phinx db-1 migrate -e staging

Just an idea, I was thinking about this last. A project im working on requires 3 db's and was trying to find easier ways to do this. The only solution I found that works right now is to create 3 composer projects for the 3 databases with only phinx. And in each project directory do this

php vendor/bin/phinx migrate -e testing

it works, not ideal but it works.

eko3alpha avatar Jun 13 '14 11:06 eko3alpha

Create an alias for each database and reference the alias in the SQL. At least this'd work for SQL (believe flywaydb.org uses this and it's possible using Phing's DBDeploy using placeholders).

The resulting SQL looks something like this:

placeholder.my_db_alias = biscuits;

USE ${my_db_alias}; DROP TABLE test_table;

CowChris avatar Jul 14 '14 15:07 CowChris

+1

baversjo avatar Sep 17 '14 00:09 baversjo

+1

oniric85 avatar Sep 19 '14 11:09 oniric85

+1

adam-paterson avatar Oct 17 '14 09:10 adam-paterson

+1 This is exactly what I'm implementing, since it's on the issue. Here's a question - I tried posting this as a new Issue but the submit button was disabled (Github bug?)

I'm bamboozled by this issue I'm having.

I'm writing a method that consumes the phinx applicaiton, and passing arguments to it. It works!!!

What doesn't work is stubbing/mocking the method in my test so I can TDD it.

class.MyCommand.php looks like this (something like this):

class MyCommand extends Command {
    public function runMigrations(InputInterface $input, OutputInterface $output, $target, $app, $environment, $configuration)
    {
        $phinx_application = new PhinxApplication('1');
        $migrate_command = $phinx_application->find('migrate');


        $configuration = (strlen($configuration) > 0 ? $configuration :__DIR__ . "/../apps/$app/phinx.yml");

        if ($target == '' || is_null($target)) {
            $arguments = array(
                'command' => 'migrate',
                '--configuration' => $configuration,
                '--environment' => "$environment"
            );
        } else {
            $arguments = array(
                'command' => 'migrate',
                '--configuration' => $configuration,
                '--environment' => "$environment",
                '--target' => "$target"
            );
        }

        $input = new ArrayInput($arguments);
        $returncode = $migrate_command->run($input, $output);


        return $returncode;
    }
}

class.MyCommandTest.php looks like this:

require_once '../src/MyCommand.php'
class MyApplicationTest extends PHPUnit_Framework_TestCase {

    protected $application;
    protected $applicationTester;

    public function setUp() {
        $this->application = new SQLUp\Console\SQLUpApplication('testing','testing');
        $this->application->setAutoExit(false);
        $this->application->setCatchExceptions(false);

        $this->applicationTester = new ApplicationTester($this->application);

    }

      public function test_enough_arguments() {


        $stub = $this->getMock('\\MyApplication\\Console\\MyCommand');
        $stub->expects($this->any())
            ->method('runMigrations')
            ->will($this->returnValue(0));



        $this->applicationTester->run(
            array(  'command' => 'update',
                    'app' => 'web',
                    'environment' => 'testing',
                    '--configuration' => __DIR__.'/fixtures/phinx.yml')
        );

        $this->assertContains('Finished with no errors.',
            $this->applicationTester->getDisplay());

    }
}

I have exhausted all resources, even looked through Github instances mentioning applicationTester and Phinx to see if anyone else has written the solution for this problem. When I run my tests (PHPUNIT 4.3.1) I get

There was a problem connecting to the database: SQLSTATE[HY000] [2002] No such file or directory.
Time: 41 ms, Memory: 6.00Mb


FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

Process finished with exit code 2

The key mock/stub I want to accomplish is to have runMigrations return the value 0, this is a success, which means I don't care what it does, just return 0 so I can move forward with the System Under Test, and verify it's unit test.

ghost avatar Oct 20 '14 23:10 ghost

@wpillar have you seen https://github.com/Pajk/PHPDeploy

ghost avatar Oct 27 '14 23:10 ghost

+1

alekseyp avatar Dec 02 '14 18:12 alekseyp

I think something like the below configuration would let us define multiple databases to apply the migrations to, but then log to some global database.

  --- 
  environments: 
    default_database: Global
    default_migration_table: PhinxLog
    development: 
      adapter: mysql
      charset: utf8
      host: localhost
      name: 
        - one
        - two
        - three

jamespsterling avatar Mar 18 '15 21:03 jamespsterling

The configuration you are proposing doesn't fit being able to drive multiple databases with different revisions. Since sharing the same PhinxLog, you'd be dependent on collapsing the sequencing migrations after another instead hence removing a major flexibility. Unless, that's not a requirement. Then, why not.

ghost avatar Mar 19 '15 21:03 ghost