Skip to content

Commit

Permalink
Merge pull request #1961 from MasterOdin/mpeveler-feat-migration_table
Browse files Browse the repository at this point in the history
Allow setting migration_table per environment
  • Loading branch information
dereuromark authored Feb 24, 2021
2 parents d930d52 + 31365ce commit 6a66b88
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
21 changes: 20 additions & 1 deletion docs/en/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ Migration Table

To keep track of the migration statuses for an environment, phinx creates
a table to store this information. You can customize where this table
is created by configuring ``default_migration_table``:
is created by configuring ``default_migration_table`` to be used as default
for all environments:

.. code-block:: yaml
Expand All @@ -204,6 +205,24 @@ with a period separator (``.``). For example, ``phinx.log`` will create
the table ``log`` in the ``phinx`` schema instead of ``phinxlog`` in the
``public`` (default) schema.

You may also specify the ``migration_table`` on a per environment basis.
Any environment that does not have a ``migration_table`` specified will
fallback to using the ``default_migration_table`` that is defined at the
top level. An example of how you might use this is as follows:

.. code-block:: yaml
environment:
default_migration_table: phinxlog
development:
migration_table: phinxlog_dev
# rest of the development settings
production:
# rest of the production settings
In the above example, ``development`` will look to the ``phinxlog_dev``
table for migration statues while ``production`` will use ``phinxlog``.

Table Prefix and Suffix
-----------------------

Expand Down
7 changes: 5 additions & 2 deletions src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ public function getEnvironment($name)
$environments = $this->getEnvironments();

if (isset($environments[$name])) {
if (isset($this->values['environments']['default_migration_table'])) {
$environments[$name]['default_migration_table'] =
if (
isset($this->values['environments']['default_migration_table'])
&& !isset($environments[$name]['migration_table'])
) {
$environments[$name]['migration_table'] =
$this->values['environments']['default_migration_table'];
}

Expand Down
9 changes: 8 additions & 1 deletion src/Phinx/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,14 @@ public function setOptions(array $options)
$this->options = $options;

if (isset($options['default_migration_table'])) {
$this->setSchemaTableName($options['default_migration_table']);
trigger_error('The default_migration_table setting for adapter has been deprecated since 0.13.0. Use `migration_table` instead.', E_USER_DEPRECATED);
if (!isset($options['migration_table'])) {
$options['migration_table'] = $options['default_migration_table'];
}
}

if (isset($options['migration_table'])) {
$this->setSchemaTableName($options['migration_table']);
}

if (isset($options['data_domain'])) {
Expand Down
10 changes: 10 additions & 0 deletions tests/Phinx/Config/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,16 @@ public function testDefaultDatabaseThrowsDeprecatedNotice()
$config->getDefaultEnvironment();
}

public function testEnvironmentHasMigrationTable()
{
$configArray = $this->getConfigArray();
$configArray['environments']['production']['migration_table'] = 'test_table';
$config = new Config($configArray);

$this->assertSame('phinxlog', $config->getEnvironment('testing')['migration_table']);
$this->assertSame('test_table', $config->getEnvironment('production')['migration_table']);
}

/**
* @covers \Phinx\Config\Config::offsetGet
* @covers \Phinx\Config\Config::offsetSet
Expand Down
10 changes: 10 additions & 0 deletions tests/Phinx/Db/Adapter/PdoAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ public function testOptionsSetConnection()
public function testOptionsSetSchemaTableName()
{
$this->assertEquals('phinxlog', $this->adapter->getSchemaTableName());
$this->adapter->setOptions(['migration_table' => 'schema_table_test']);
$this->assertEquals('schema_table_test', $this->adapter->getSchemaTableName());
}

public function testOptionsSetDefaultMigrationTableThrowsDeprecation()
{
$this->assertEquals('phinxlog', $this->adapter->getSchemaTableName());

$this->expectDeprecation();
$this->expectExceptionMessage('The default_migration_table setting for adapter has been deprecated since 0.13.0. Use `migration_table` instead.');
$this->adapter->setOptions(['default_migration_table' => 'schema_table_test']);
$this->assertEquals('schema_table_test', $this->adapter->getSchemaTableName());
}
Expand Down

0 comments on commit 6a66b88

Please sign in to comment.