Refactoring Database Migration to Ruby on Rails

When a project grows, the number of migrations becomes quite high, and looking back, I see many migrations that can be reorganized. Like merging create_posts and rename_posts_to_responses into create_responses .

Is this a bad habit or should I encourage migration refactoring?

+4
source share
4 answers

You could, but later in the project, you should not really start migrations completely, you should be schema:load 'ing, that is, if you need to start a completely new instance of the project. In my experience, you will cause yourself more headaches than anything else.

Running schema:load bit complicated, however, if you included data in your migrations (what happens to the best of us).

+5
source

Once you check the transition to the original control, I would recommend not changing it. I make a rare exception if there is an error in it, but this is pretty rare (maybe 1 out of 100).

The reason is that as soon as they get out of the wild, some people can run them. They are written as completed in db. If you change them and check in the new version, other people will not be able to use this change. You can ask people to revert certain changes and rerun them, but that defeats the goal of automation. Running often, it becomes a mess. This is best left alone.

When you receive a large number of migrations, it can begin to feel awkward. In general, you will not work so hard. The only thing we do is our integration server, which rolls and recreates the database for each launch. Therefore, you can simply not open this directory and pretend that they are not there.

There is a practice of consolidating migrations. To do this, simply copy the current scheme into the transfer and delete all previous migrations. Then you have fewer files to manage, and tests can run faster. You need to be careful, especially if you have migrations that automatically start during production. Usually I replace the transfer, which, as I know, everyone started with a new scheme. Other people have slightly different ways of doing this.

+5
source

in my opinion, this is normal for refactoring during development [although I would discourage it, especially when I work as part of a team], but it’s too easy to spoil applications in production by reorganizing the migration

+1
source

I saw large projects where all migrations are replaced by one migration taken from schema.rb. This is another reason not to use data migration and instead maintain a set of source data.

0
source

All Articles