My application relies on having about 48,000 rows of data already present in a database (MySql). It seems to me that with the ActiveRecord abstraction this would present the problem that every time a migration takes place this data would have to be re-inserted. Is this indeed the case? Or is there a workaround?
Share
Ruby on Rails handles migrations by keeping track of changes made to the application schema in a differential manner. The purpose of this is to offer a very controlled mechanism for managing both a database’s schema and data. By using a migration to pre-populate your database after your initial database schema has been created, this migration will only be executed once per application deployment.
Perhaps an example would be the best way to illustrate how rails
ActiveRecord::Migrationsare handled. Your timestamps will obviously be different, so it’d be best to try it by hand if you’re having trouble following along.This migration creates a table
userswith two string fields – username and password. Pretty simple. Now, in this example, I’ll be assuming you’ve preconfigured yourdatabase.yml.Creating the database, load the schema, and initial migrations
We still have to merge initial migrations into the schema. This will load the initial specification for your users model into the database schema, and then the database can be setup.
At this point, you can easily drop into
rails consoleand see the initial configuration.Pre-populating the database
Just so you can see what the initial users migration looks like.
To populate it, you will need to create a migration to act as a proxy between your preferred data interface and ActiveRecord. If it’s in a database somewhere, I would recommend using something like FasterCSV to import it in the
self.up(orup) section of your newly migration. This migration will only be executed once per application deployment, unless you drop your database and start over.A word of caution
I will caution you, though, you’re probably in for a long haul (for at least a day or two). I’ve done dirty migrations of data from old applications running on completely different platforms, and you’d better hope your dataset is perfect (and that you’ve mimic’d the data model perfectly) or things will break.
Also, a possible oversight
In case I misunderstood your question, and you’re looking to prepopulate your database with random data, you should take a peek at Forgery, random-data, and/or Faker.
And if I’m completely off-point here, feel free to comment and I’ll adjust my answer accordingly.