Rebase Rails migrations in a long running project
In general, you don't need to clean up old migrations. If you're running db:migrate from scratch (no existing db), Rails uses db/schema.rb to create the tables instead of running every migration. Otherwise, it only runs the migrations required to upgrade from the current schema to the latest.
If you still want to combine migrations up to a given point into a single one, you could try to:
- migrate from scratch up to the targeted schema using
rake db:migrate VERSION=xxx
- dump the schema using
rake db:schema:dump
- remove the migrations from the beginning up to version xxx and create a single new migration using the contents of db/schema.rb (put create_table and add_index statements into the self.up method of the new migration).
Make sure to choose one of the old migration version numbers for your aggregated new migration; otherwise, Rails would try to apply that migration on your production server (which would wipe your existing data, since the create_table statements use :force⇒true).
Anyway, I wouldn't recommend to do this since Rails usually handles migrations well itself. But if you still want to, make sure to double check everything and try locally first before you risk data loss on your production server.
rails: any way to run ONLY the NEXT migration (of several new migrations), do manual stuff, then run the rest of the migrations?
rake db:migrate:up VERSION=X
# do your stuff
rake db:migrate
Source: Migrations-Guide
How to rollback to beginning and recreate/rebuild new migrations
Your schema.rb
file should contain the actual schema from your database. You could use it as a starting point to create you migrations. You could create a new migration for each table with the :force => true
parameter to overwrite the old table. Afterwards you could just delete the old migrations (you would probably also need to delete their entries from schema_migrations table).
Another options would be just updating the old migrations to match your current schema.
Way to flatten Rails migrations?
This is what the db/schema.rb
file is for. If you've only got structural changes in your migrations you will be able to run rake db:schema:load
rather than running rake db:migrate
to get the absolute structure for your tables.
Aggregate migrations in Rails
It is possible, but probably not a good idea to aggregate the migrations!
Maybe ask:
- Why do you want to do this?
- How often do you really need to migrate all the way to VERSION=0 and then back up again?
- Is something really broken? (if not, then don't fix it)
I've had the same problem once.. I ended up just re-ordering my migrations, because changes in the schema caused it to not correctly migrate up/down anymore. I would be hesitant to do that again.
If you have migrations which just add fields or indexes, then maybe you can combine them with the main migration for the model -- but beware that you can't reproduce old situations anymore, e.g. older DB-dumps may not be compatible with what migration number they should be compatible with -- that is probably the biggest argument against aggregating...
Technically, you can dump the schema and then load it directly - that is one way:
rake db:schema:dump
then create a single new migration with the contents of the schema dump file db/schema.rb
Here are some similar questions:
Rebase Rails migrations in a long running project
Deleting/"Rebasing" rails migrations
Way to "flatten" Rails migrations?
Should I flatten Rails migrations?
P.S.: I found it useful to stick with the old migration numbering scheme, where the migrations do not use timestamps - for me this works better (is easier to see in which order they are).
e.g. in your config/application.rb file:
config.active_record.timestamped_migrations = false
Keeping track of completed migrations in rails when using config.active_record.schema_format = :sql
I tracked down the problem...
Rails ActiveRecord does append INSERT
statements for each up migration at the end of the structure.sql
file.
The problem wound up being a problem in the activerecord-postgis-adapter gem which overrides the db:structure:dump
rake task and does not append the said INSERT
statement. I submitted a pull request to fix the problem.
editing already run migrations
If I changed my mind about something regarding the migration, could I
just edit the migration and run rake db:migrate again?
You have to follow these steps
Do
rake db:rollback
ORrake db:migrate:down
Edit your changes and
Do
rake db:migrate
again.
This will let your changes reflect in the DB
If you already have some data
in the DB
and worried about losing
it,then i would suggest you
to create another migration file
which will reflect the changes in the DB
with out losing of data
Loading the DB dumb, and running all the migrations
I would first load the db dump, as it also contains all you data and the current schema structure.
running rake db:migrate will only run the new migration
This depends. If your db dump has a schema_migrations
table, that lists all the previously applied migrations, it will not apply the migrations. Otherwise, it will try to and fail.
You basically need these steps:
- Import your dump
- Create a Rails schema (rake db:schema:dump)
- If you have newer migrations, run them
Related Topics
How to Overwrite a Getter Method in an Activerecord Model
Ruby on Rails Advanced JSON Serialization
Ruby Dropped in Netbeans 7,How to Use It in Netbeans7
Rails App: Solr Throwing Rsolr::Error::Http - 404 Not Found When Executing Search
How to Remove Ruby from Ubuntu
Active Admin - Refresh Second Drop Down Based on First Drop Down, Ruby on Rails
Ruby Dynamic Classes. How to Fix "Warning: Class Variable Access from Toplevel"
Can't Install Thrift Gem on Os X El Capitan
Ruby Code to Extract Host from Url String
Getting Webpage Content with Ruby -- I'm Having Troubles
How to Return Something Early from a Block
Executing User-Supplied Ruby Code on a Web Server
How to Install Ruby-Debug in Ruby 1.9.3/Rails 3.2.1
Jekyll - Generating JSON Files Alongside the HTML Files
Maintaining Cookies Between Mechanize Requests
How to Share the Factories That I Have in a Gem and Use It in Other Project