1

I have one rails API application (App1), which doesn't have any database (no schema.rb file), but i am using some other application's database without adding any heroku postgres addon so when i push this application to heroku, i set the environment variable as DATABASE_URL = [ Database url of App2 ], also i have added some test cases with mini-test and enabled CI in heroku, it will run the test before the deployment but that time i am getting this error

enter image description here

and this is my application.rb

    require_relative 'boot'

    require "rails"
    # Pick the frameworks you want:
    require "active_model/railtie"
    require "active_job/railtie"
    require "active_record/railtie"
    require "action_controller/railtie"
    require "action_mailer/railtie"
    require "action_view/railtie"
    require "action_cable/engine"
    # require "sprockets/railtie"
    require "rails/test_unit/railtie"

    # Require the gems listed in Gemfile, including any gems
    # you've limited to :test, :development, or :production.
    Bundler.require(*Rails.groups)

    module DemoGroup
      class Application < Rails::Application
        # Initialize configuration defaults for originally generated Rails version.
        config.load_defaults 5.1

        # Settings in config/environments/* take precedence over those specified here.
        # Application configuration should go into files in config/initializers
        # -- all .rb files in that directory are automatically loaded.

        # Only loads a smaller set of middleware suitable for API only apps.
        # Middleware like session, flash, cookies can be added back manually.
        # Skip views, helpers and assets when generating a new resource.
        config.api_only = true
      end
    end

is there any way i can run this test in heroku

Developer
  • 561
  • 7
  • 29

1 Answers1

0

The correct way to do this is by attaching the Postgres addon from app2 to app1.

Lets say you set DATABASE_URL on app1 to point to the attachment on app2. Heroku then needs to do maintenance - which changes the database url as its shifted to another server. Heroku is nice enough to automatically update DATABASE_URL on app2 - but app1 will be pointing to an invalid url.

This assumes you have the Heroku CLI client installed.

First get a list of the attachments:

$ heroku addons

Remove the default Postgres addon

$ heroku addons:remove heroku-postgresql:dev --app app1

Replace app2 with the name of your application on heroku.

Attach the addon from the other application.

Then attach the Postgres add-on from app2 to app1:

$ heroku addons:attach attachment_name -a app1

Schema.rb

schema.rb is required for ActiveRecord to work properly. If app1 does actually create migrations you can just commit an "empty" schema.rb:

ActiveRecord::Schema.define(version: 0) do
  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
end
max
  • 96,212
  • 14
  • 104
  • 165
  • Hopefully I understood what you are trying to do. But I don't really get why you would want to attach your CI server to another apps database as it will nuke that DB. And you do still need to run `rake db:migrate` to create a `schema.db` and commit it for ActiveRecord to work properly. – max Oct 12 '18 at 10:59
  • yes App1 have proper db, schema and migration, using pg, but i don't want to run any migrations on App2. – Developer Oct 12 '18 at 11:25
  • Then just commit an "empty" `schema.rb`. If your not really using ActiveRecord in App2 though I would consider removing it and just connecting with the PG gem which saves a ton of overhead. You can connect with `conn = PG.connect(ENV['DATABASE_URL'])`. https://deveiate.org/code/pg/ – max Oct 12 '18 at 11:45
  • Sorry for wrong information on last commit, App2 has the proper db, schema and all App1 doesn't have anything, but i want to use pg, bcz i had some connection issue when i run sidekiq in App1, so now i am attaching App2 db to App1, is that fine? i am going to test that, – Developer Oct 12 '18 at 12:38
  • can you give me one more clarification, when i attach App2 db to App1 and run the test, will it clear all the data of my app2? – Developer Oct 12 '18 at 12:43
  • Depends completely on your test setup. But yea its very possible that it will completely wipe the database and you will need a separate database for testing. – max Oct 12 '18 at 12:59