【问题标题】:Run ruby test in heroku application without database在没有数据库的 heroku 应用程序中运行 ruby​​ 测试
【发布时间】:2018-10-12 19:17:58
【问题描述】:

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

这是我的 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 中运行这个测试

【问题讨论】:

    标签: ruby-on-rails ruby heroku minitest


    【解决方案1】:

    执行此操作的正确方法是将 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
    

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

    附加来自其他应用程序的插件。

    然后将 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
    

    【讨论】:

    • 希望我能理解你想要做什么。但我真的不明白为什么要将 CI 服务器附加到另一个应用程序数据库,因为它会破坏该数据库。而且您仍然需要运行rake db:migrate 来创建schema.db 并提交它以使ActiveRecord 正常工作。
    • 是的,App1 有适当的数据库、架构和迁移,使用 pg,但我不想在 App2 上运行任何迁移。
    • 然后提交一个“空”schema.rb。如果您没有真正在 App2 中使用 ActiveRecord,但我会考虑将其删除并仅与 PG gem 连接,这样可以节省大量开销。您可以联系conn = PG.connect(ENV['DATABASE_URL'])deveiate.org/code/pg
    • 抱歉上次提交的信息有误,App2 有正确的数据库、架构,所有 App1 都没有,但我想使用 pg,bcz 我在运行 sidekiq 时遇到了一些连接问题App1,所以现在我将 App2 db 附加到 App1,可以吗?我要测试一下,
    • 你能再澄清一下,当我将 App2 db 附加到 App1 并运行测试时,它会清除我的 app2 的所有数据吗?
    猜你喜欢
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2011-11-08
    • 2018-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-06
    相关资源
    最近更新 更多