在没有数据库的heroku应用程序中运行ruby测试

时间:2018-10-12 09:20:36

标签: ruby-on-rails ruby heroku minitest

我有一个Rails API应用程序( App1 ),它没有任何数据库(没有 schema.rb 文件),但是我正在使用其他一些应用程序的数据库没有添加任何heroku postgres插件,因此当我将此应用程序推送到heroku时,我将环境变量设置为 DATABASE_URL = [App2的数据库URL],我还添加了一些具有mini-test并已启用的测试用例CI在heroku中,它将在部署之前运行测试,但是那时候我遇到此错误

enter image description here

这是我的 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

有什么办法可以在heroku中运行此测试

1 个答案:

答案 0 :(得分:0)

正确的方法是将Postgres插件从app2附加到app1

假设您在app1上设置DATABASE_URL指向app2上的附件。然后,Heroku需要进行维护-当数据库URL转移到另一台服务器时会对其进行更改。 Heroku非常好,可以在app2上自动更新DATABASE_URL-但是app1将指向无效的URL。

这假定您已经安装了Heroku CLI客户端。

首先获取附件列表:

$ heroku addons

删除默认的Postgres插件

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

在您的heroku上用您的应用程序名称替换app2

从其他应用程序附加插件。

然后将Postgres附件从app2附加到app1:

$ heroku addons:attach attachment_name -a app1

Schema.rb

schema.rb是ActiveRecord正常运行所必需的。如果app1确实创建了迁移,则只需提交“空” 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