I'm developing a web app and thinking about making addons for this app so another developers can use them or create addons too.
The problem are migrations. I want migrations in addons to have sequential numbers in the beggining, instead of timestamps, and also to have prefix of addon name in migrations.
Like this:
--products_addon
----migrations
------001_products_migration.rb
--tools_addon
----migrations
------001_tools_migrations.rb
In the end I would like schema_migrations table in database contain values:
"001_products_addon"
"001_tools_addon"
But it seems ActiveRecord just skips any migration having the same sequential number as existing previous migration. When I use one MigrationContext for multiple directories with migrations, I get DuplicateMigrationError. When I use different MigrationContext instances, the second migration is skipped without explanation or warning or error.
Maybe I can somehow force the specific name for version for each migration?
TL;DR; It can be done by overriding ActiveRecord classes for migrations to add specific suffixes to the migrations.
By default you can't do that in plain ActiveRecord.
First of all, ActiveRecord accepts and sorts migrations with integer value in the beginning of the migration filename.
In the "old" times ActiveRecord used incremental values like "001_migration", "002_migration", "003_migration" etc.
At some point developers of ActiveRecord switched to timestamp in the beginning of the migration filename.
It solves the problem of ambiguous migration prefixes, which usually occurs when several developers work in different VCS (version control system) like git, mercurial, subversion. (example: 2 devs push migrations "002_users" and "002_products" into the repository, in the "old" days ActiveRecord would notify about the ambigous migrations and possibly raise an exception, since it does not know which migration should be applied to the database first).
Now, having timestamps in migration names, problem solved. It is very unlikely several migrations with same timestamp occur (it would require a lot of effort of 2+ developers synchronising in time or less effort simply renaming migrations manually)
Behaviour described in question is similar to behaviour in Redmine system, where each plugin has its prefix for its migrations.
You can find Redmine source code here: https://github.com/redmine/redmine
Redmine, which have similar approach needed in the question, does not care about prefixes. But it does care about plugin names.
Developers of Redmine overrided ActiveRecord classes to add a twist - plugin suffixes when creating migrations and when applying the to the database.
When developing plugin for Redmine, template generates migrations in the fashion "INCREMENTAL_INTEGER_plugin_name".
When applying migrations from different plugins - they are written in the "schema_migration" table (ActiveRecord built-in table to store information about applied migrations) with suffixes of the respectful plugins.
Their approach is a little more complex than just simply overriding several methods (you can check their source code). They override methods that checks for applied migrations, reverts them, etc.
But it's doable. And not rocket science. Just follow the steps of their experience in their code.