Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First off, I would highly suggest just paying the extra $50/mo. For that, you get all kinds of cool stuff like <a href="https://devcenter.heroku.com/articles/fork-app" rel="nofollow">forking</a> and <a href="https://devcenter.heroku.com/articles/labs-pipelines" rel="nofollow">pipelines</a>, as well as the fact that this is kind of hacky. I honestly don't know if this might end up wiping your production data when you clear the staging database. Please make backups before trying this.</p> <p>I'll view this answer as an answer to a technical challenge rather than a business decision. Without further ado,</p> <h1>Setting up multiple environments on the same database with Heroku Postgres schemas</h1> <p>First, I deleted the existing dev database and added a production database to my production app.</p> <pre><code>heroku addons:add heroku-postgresql:crane &gt; Adding heroku-postgresql:crane on test-shared-app... done, v# ($50/mo) &gt; Attached as HEROKU_POSTGRESQL_RED_URL </code></pre> <p>This attached a database under the <code>RED</code> color, so replace <code>HEROKU_POSTGRESQL_RED_URL</code> with the appropriate color for your app.</p> <p>This attaches a database to the production app, but we need to connect to the same app for staging. First, create the staging schema</p> <pre><code>heroku run "bundle exec rails runner 'ActiveRecord::Base.connection.execute(%q{CREATE SCHEMA staging})'" </code></pre> <p>Next, create the staging app. See <a href="https://devcenter.heroku.com/articles/multiple-environments" rel="nofollow">Managing Multiple Environments for an App</a> for more information.</p> <pre><code>heroku create --remote staging test-shared-app-staging heroku config:set RACK_ENV=staging RAILS_ENV=staging --remote staging </code></pre> <p>Then, copy the environment data from your existing app. <strong>Add <code>?schema_search_path=staging</code> to the end of the URL.</strong></p> <pre><code>heroku config --remote heroku --shell # make note of your database URLs heroku config:set --remote staging \ DATABASE_URL=postgres://...?schema_search_path=staging \ HEROKU_POSTGRESQL_RED_URL=postgres://...?schema_search_path=staging </code></pre> <p>And push to staging</p> <pre><code>git push staging master </code></pre> <p>Now, run migrations on staging</p> <pre><code>heroku run --remote staging bundle exec rake db:migrate </code></pre> <p>And try it out.</p> <p>My "app" is running at <a href="http://test-shared-app.herokuapp.com/posts" rel="nofollow">http://test-shared-app.herokuapp.com/posts</a> and <a href="http://test-shared-app-staging.herokuapp.com/posts" rel="nofollow">http://test-shared-app-staging.herokuapp.com/posts</a>. You can see the source at <a href="https://github.com/benmanns/heroku-shared-app" rel="nofollow">https://github.com/benmanns/heroku-shared-app</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload