Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeatable migrations #1742

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions UPGRADE_0.9.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Upgrading Phinx to 0.9

* Template Creation is now supported for use by Migrations, Repeatable Migrations and Seeds.

Some renaming of the interface, abstract base class and methods has
been undertaken so that the names are not tied specifically to
Migrations.

The following elements have been renamed:

* Interface

Phinx 0.8

Phinx\Migration\CreationInterface

Phinx 0.9

Phinx\Templates\TemplateCreationInterface

* Class

Phinx 0.8

Phinx\Migration\AbstractTemplateCreation

Phinx 0.9

Phinx\Templates\AbstractTemplateCreation

* Methods

Phinx 0.8:

Phinx\Migration\CreationInterface\getMigrationTemplate()`
Phinx\Migration\CreationInterface\postMigrationCreation($migrationFilename, $className, $baseClassName)

Phinx 0.9

Phinx\Templates\TemplateCreationInterface\getTemplate()
Phinx\Templates\TemplateCreationInterface\postTemplateCreation($filename, $className, $baseClassName)
6 changes: 3 additions & 3 deletions docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ alternative template filename.
$ phinx create MyNewMigration --template="<file>"

You can also supply a template generating class. This class must implement the
interface ``Phinx\Migration\CreationInterface``.
interface ``Phinx\Templates\TemplateCreationInterface``.

.. code-block:: bash

Expand Down Expand Up @@ -148,8 +148,8 @@ breakpoint using the ``--force`` parameter or ``-f`` for short.

.. note::

When rolling back, Phinx orders the executed migrations using
the order specified in the ``version_order`` option of your
When rolling back, Phinx orders the executed migrations using
the order specified in the ``version_order`` option of your
``phinx.yml`` file.
Please see the :doc:`Configuration <configuration>` chapter for more information.

Expand Down
82 changes: 78 additions & 4 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,65 @@ setting ``migration_base_class`` in your config:

migration_base_class: MyMagicalMigration

Repeatable Migration Paths
--------------------------

The second option specifies the path to your repeatable migration directory. Phinx uses
``%%PHINX_CONFIG_DIR%%/db/repeatables`` by default.

.. note::

``%%PHINX_CONFIG_DIR%%`` is a special token and is automatically replaced
with the root directory where your ``phinx.yml`` file is stored.

In order to overwrite the default ``%%PHINX_CONFIG_DIR%%/db/repeatables``, you
need to add the following to the yaml configuration.

.. code-block:: yaml

paths:
repeatables: /your/full/path

You can also provide multiple repeatable migration paths by using an array in your configuration:

.. code-block:: yaml

paths:
repeatables:
- application/module1/repeatables
- application/module2/repeatables


You can also use the ``%%PHINX_CONFIG_DIR%%`` token in your path.

.. code-block:: yaml

paths:
repeatables: %%PHINX_CONFIG_DIR%%/your/relative/path

Repeatable migrations are captured with ``glob``, so you can define a pattern for multiple
directories.

.. code-block:: yaml

paths:
migrations: %%PHINX_CONFIG_DIR%%/module/*/{views,triggers}/repeatables

Custom Repeatable Migration Base
--------------------------------

By default all repeatable migrations will extend from Phinx's `AbstractRepeatableMigration` class.
This can be set to a custom class that extends from `AbstractRepeatableMigration` by
setting ``repeatable_migration_base_class`` in your config:

.. code-block:: yaml

repeatable_migration_base_class: MyMagicalRepeatableMigration

Seed Paths
----------

The second option specifies the path to your seed directory. Phinx uses
The third option specifies the path to your seed directory. Phinx uses
``%%PHINX_CONFIG_DIR%%/db/seeds`` by default.

.. note::
Expand Down Expand Up @@ -140,6 +195,25 @@ You can also use the ``%%PHINX_CONFIG_DIR%%`` token in your path.
paths:
seeds: %%PHINX_CONFIG_DIR%%/your/relative/path

Seeds are captured with ``glob``, so you can define a pattern for multiple
directories.

.. code-block:: yaml

paths:
seeds: %%PHINX_CONFIG_DIR%%/module/*/{primary,secondary}/seeds

Custom Seed Base
----------------

By default all seeds will extend from Phinx's `AbstractSeed` class.
This can be set to a custom class that extends from `AbstractSeed` by
setting ``seed_base_class`` in your config:

.. code-block:: yaml

seed_base_class: MyMagicalSeeder

Environments
------------

Expand Down Expand Up @@ -293,7 +367,7 @@ Aliases

Template creation class names can be aliased and used with the ``--class`` command line option for the :doc:`Create Command <commands>`.

The aliased classes will still be required to implement the ``Phinx\Migration\CreationInterface`` interface.
The aliased classes will still be required to implement the ``Phinx\Templates\TemplateCreationInterface`` interface.

.. code-block:: yaml

Expand All @@ -304,8 +378,8 @@ The aliased classes will still be required to implement the ``Phinx\Migration\Cr
Version Order
------

When rolling back or printing the status of migrations, Phinx orders the executed migrations according to the
When rolling back or printing the status of migrations, Phinx orders the executed migrations according to the
``version_order`` option, which can have the following values:

* ``creation`` (the default): migrations are ordered by their creation time, which is also part of their filename.
* ``execution``: migrations are ordered by their execution time, also known as start time.
* ``execution``: migrations are ordered by their execution time, also known as start time.
3 changes: 2 additions & 1 deletion phinx.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
paths:
migrations: %%PHINX_CONFIG_DIR%%/db/migrations
repeatables: %%PHINX_CONFIG_DIR%%/db/repeatables
seeds: %%PHINX_CONFIG_DIR%%/db/seeds

environments:
Expand Down Expand Up @@ -32,4 +33,4 @@ environments:
port: 3306
charset: utf8

version_order: creation
version_order: creation
43 changes: 37 additions & 6 deletions src/Phinx/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,7 @@ public function getMigrationPaths()
}

/**
* Gets the base class name for migrations.
*
* @param boolean $dropNamespace Return the base migration class name without the namespace.
* @return string
* {@inheritdoc}
*/
public function getMigrationBaseClassName($dropNamespace = true)
{
Expand All @@ -263,6 +260,32 @@ public function getMigrationBaseClassName($dropNamespace = true)
return $dropNamespace ? substr(strrchr($className, '\\'), 1) ?: $className : $className;
}

/**
* {@inheritdoc}
*/
public function getRepeatableMigrationPaths()
{
if (!isset($this->values['paths']['repeatables'])) {
throw new \UnexpectedValueException('Repeatables path missing from config file');
}

if (is_string($this->values['paths']['repeatables'])) {
$this->values['paths']['repeatables'] = array($this->values['paths']['repeatables']);
}

return $this->values['paths']['repeatables'];
}

/**
* @inheritDoc
*/
public function getRepeatableMigrationBaseClassName($dropNamespace = true)
{
$className = !isset($this->values['repeatable_migration_base_class']) ? 'Phinx\Repeatable\AbstractRepeatableMigration' : $this->values['repeatable_migration_base_class'];

return $dropNamespace ? substr(strrchr($className, '\\'), 1) ?: $className : $className;
}

/**
* {@inheritdoc}
*/
Expand All @@ -279,6 +302,16 @@ public function getSeedPaths()
return $this->values['paths']['seeds'];
}

/**
* @inheritDoc
*/
public function getSeedBaseClassName($dropNamespace = true)
{
$className = !isset($this->values['seed_base_class']) ? 'Phinx\Seed\AbstractSeed' : $this->values['seed_base_class'];

return $dropNamespace ? substr(strrchr($className, '\\'), 1) ?: $className : $className;
}

/**
* Get the template file name.
*
Expand Down Expand Up @@ -333,8 +366,6 @@ public function isVersionOrderCreationTime()
return $versionOrder == self::VERSION_ORDER_CREATION_TIME;
}



/**
* Replace tokens in the specified array.
*
Expand Down
23 changes: 23 additions & 0 deletions src/Phinx/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public function getConfigFilePath();
*/
public function getMigrationPaths();

/**
* Gets the paths to search for repeatable migration files.
*
* @return string[]
*/
public function getRepeatableMigrationPaths();

/**
* Gets the paths to search for seed files.
*
Expand Down Expand Up @@ -137,4 +144,20 @@ public function isVersionOrderCreationTime();
* @return string
*/
public function getMigrationBaseClassName($dropNamespace = true);

/**
* Gets the base class name for repeatable migrations.
*
* @param boolean $dropNamespace Return the base repeatable migration class name without the namespace.
* @return string
*/
public function getRepeatableMigrationBaseClassName($dropNamespace = true);

/**
* Gets the base class name for seeders.
*
* @param boolean $dropNamespace Return the base seeder class name without the namespace.
* @return string
*/
public function getSeedBaseClassName($dropNamespace = true);
}
Loading