yiisoft / yiisoft/db-migration
[RFC] Aliases for migrations
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 32
- Forks
- 18
- Avg merge
- 22h 18m
- Merged PRs (30d)
- 2
Description
Migrations names can't be changed after release, because it will break any installation that already applied them. This means that:
- you can never fix typo in migration name,
- you can not migrate from non-namespaced migrations to namespaced migrations,
- if you already use namespaced migration, you can never change namespace of your project or change structure of directories that can affect migrations namespaces.
Basically, migrations can be really PITA for long-term projects, especially if you decide to use namespaced migrations.
I propose to add new static property to yii\console\Migration:
namespace yii\log\migrations;
use yii\db\Migration;
class M141106185632LogInit extends Migration
{
public static $aliases = [
'm141106_185632_log_init', // old non-namespaced migrations
'yii\log\migrations\M141106185632LogInt', // typo
];
// ...
}
On migration/up
- Check if any of migration aliases are already present in migration history.
- If it is present - add current migration and all non-applied aliases to migration history with current timestamp and some prefix for name (like
-), without runningup()method. - If it's not applied - run
up(), add migration (without prefix for name) and all aliases (with prefix) to history with current timestamp.
On migrate/down
- Pick last applied migration from history.
- If name of this migration is prefixed - remove from history without running
down(). Back to step 1. (removing prefixed migration from history should not count as revert). - If migration does not have alias prefix in history - run
down(), remove migration from history, and all related aliases (so if we're revertingm141106_185632_log_initwe should also removeyii\log\migrations\M141106185632LogInitandyii\log\migrations\M141106185632LogIntfrom history).
Tricky part
The obvious problem is finding real migration class for an alias (if we're reverting m141106_185632_log_init we need actually revert yii\log\migrations\M141106185632LogInit). In the worst case we could scan all migrations and use $aliases values to create alias => real-class map. But usually we could avoid this - in most cases we first load a real class, so we could start building map from already loaded migrations and do a full scan only if migration to revert can not be found.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the yii\console\Migration class and the migration/up and migrate/down entry points. Trace how migration history is read and written, then compare the proposed alias handling for applying and reverting migrations; done means aliases preserve migration history without rerunning or incorrectly reverting the real migration.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- databases, tooling
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100