Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate production database (heroku) with development data? (Rails)

heroku run rake db:migrate works fine to alter the structure of the production database:

Migrating to CreateUsers (20120318090252)
Migrating to AddIndexToUsersEmail (20120319191315)
Migrating to AddPasswordDigestToUsers (20120319194632)
Migrating to AddRememberTokenToUsers (20120323142854)

But the entries, the data I have added to my local development database haven't been uploaded to the heroku production database.

Is this normal, or is there a way to make the two databases match?

Thanks in advance.

like image 205
user1011444 Avatar asked Mar 24 '12 00:03

user1011444


2 Answers

This is the normal behavior. rake db:migrate will only create your database structure. If you want to duplicate the data you already have in development on Heroku, use the heroku db:push command, or just use the seeds.rb if you want to initialize the database with some fixed records.

like image 167
eugen Avatar answered Nov 15 '22 08:11

eugen


Migrations can handle both structure (schema) and data, but once you're rolling, the assumption is that in most cases your production data is the canonical source of information. If there's data needed to set up the database, for example things like lists ("Mastercard, Visa, Amex) or bootstrapping data (e.g. setting up an admin user) this can go in the "seeds .rb" file. There's nothing built in that makes a copy of a database (schema and content) and automatically applied it -- this would typically be a one-time thing.

(Going the other direction -- copying the production database to QA or development instances is a common use case. At first, you might think: Rails should be able to do this. But copying a typical production database may be fraught with issues. The most important is: copying a production database having user info is a significant security risk; any copy operation should at the very least make users anonymous. A second issue is just database size: a production database is often useful or even necessary to reproduce real-life performance issues or other edge cases, but any large database will end up taking a long time to replicate, and is highly dependent both on the specific database you're using, and on the permissions)

In short, Rails does the right thing with migration: assumes structural updates are OK, but requires you to populate data. Hope this helps!

like image 6
Tom Harrison Avatar answered Nov 15 '22 08:11

Tom Harrison