2 Rails Apps, 1 Database (using Heroku)
I've made 2 apps, App A and App B. App A's sole purpose is to allow users to sign up and App B's purpose is to take sele开发者_如何学编程ct users from App A email them. Since App A & B were created independently & are hosted in 2 separate Heroku instances, how can App B access the users database in App A? Is there a way to push certain relevant rows from App A to App B?
There is currently no way of sharing databases between Heroku apps.
You might be able to use the Amazon RDS add-on to run a dedicated MySQL instance.
The alternative is going to be creating an API and pushing data between the apps. You can run a background process to push the data in and out.
You can connect several Heroku instances on the same shared PostgreSQL database, given by the shared-database:5mb
free add-on on Heroku.
On the instance on which you have the database, type:
$ heroku config
This will show a lot of settings, in which you'll see something like this:
DATABASE_URL => postgres://fbksgiuqlv:BuIvJDfS_eOBDJDZCc9SP@ec2-104-20-247-168.compute-1.amazonaws.com/fbksgiuqlv
This is the database connection string that your instances will be using.
Then on the other instances, overwrite the DATABASE_URL config variable by typing:
$ heroku config:add DATABASE_URL=your_new_connection_string
So tinkered a little around and did as below and it worked
prompt$ heroku console
Ruby console for your-app.heroku.com
>> dbconfig = YAML.load(File.read('config/database.yml'))
=> {"production"=>{"encoding"=>"unicode", "port"=>5432, "adapter"=>"postgresql", "username"=>"xxxxxxxxxxxxxx", "database"=>"xxxxxxxxxxxxx", "host"=>"kklasfdsfjsfdsfsd.fsdf.dsfdsf", "password"=>"xxxxxxxxxxxxxxxxxx"}}
puts dbconfig.to_yaml
---
production:
encoding: unicode
port: 5432
adapter: postgresql
username: xxxxxxxxxxx
database: xxxxxxxxxxxxxxx
host: ec2-50-2323kskdsakd231.amazonaws.com
password: xxxxxxxxxxxxxx
Then copy and paste the yaml to a connection for the other DB
and it works!!! For me!!!
I was looking into this as well and I noticed that it seems like it is now possible to actually do this.
See: http://newsletterer.heroku.com/2011/07 ("did you know" section at the bottom)
Basically you set up one app, retrieve the app database url, and add that url to the config of the other app, like so:
$ heroku config | grep DATABASE_URL --app sushi
DATABASE_URL => postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf
Then, set the DATABASE_URL for new apps to this value:
$ heroku config:add DATABASE_URL=postgres://lswlmfdsfos:5FSLVUSLLT123@ec2-123-456-78-90.compute-1.amazonaws.com/ldfoiusfsf --app sushi-analytics
Adding config vars: DATABASE_URL => postgres://lswlm...m/ldfoiusfsf
Restarting app... done, v74.
That's it — now both apps will share one database.
I haven't tried this yet, but I am about to give it a shot, as I too was thinking about splitting an existing app into two apps. Let's hope this actually works!
Heroku actually overwrites the database.yml file that you have checked in, you can verify this by using the "heroku shell" command and typing cat config/databse.yml
They make up a bunch of random stuff that is used per application. I don't believe its possible to stop them from doing that.
You might be able to do this if you use schemas with PostgreSQL.
精彩评论