0

我一直在尝试基于以下 database.yml 运行迁移

数据库.yml:

development:
  adapter: postgresql
  encoding: utf8
  database: test_development
  timeout: 5000
  queue:
    adapter: postgresql
    encoding: utf8
    database: test_queue_development
    timeout: 5000

我想要做的是在 test_queue_development 数据库中创建一个表“delayed_jobs”。不幸的是,该表是在 test_development 中创建的。delay_job.rb 的初始化程序如下。

配置/initializers/delayed_job.rb:

db_queue_config = ActiveRecord::Base.configurations[Rails.env]['queue']
if db_queue_config
  Delayed::Backend::ActiveRecord::Job.establish_connection(db_queue_config)
end

我只在开发中运行迁移

迁移应该尊重 Delayed::Backend::ActiveRecord::Job.connection 并在 test_queue_development 中创建表,但它总是在 test_development 中创建表。

db/migrate/create_delayed_jobs.rb

class CreateDelayedJobs < ActiveRecord::Migration
  def self.connection
    Delayed::Backend::ActiveRecord::Job.connection
  end

  def self.up
    create_table :delayed_jobs do |table|
      # Allows some jobs to jump to the front of the queue
      table.integer :priority, :default => 0
      # Provides for retries, but still fail eventually.
      table.integer :attempts, :default => 0
      # YAML-encoded string of the object that will do work
      table.text :handler, :limit => 500.kilobytes
      # reason for last failure (See Note below)
      table.text :last_error
      # The queue that this job is in
      table.string :queue, :default => nil
      # When to run.
      # Could be Time.zone.now for immediately, or sometime in the future.
      table.datetime :run_at
      # Set when a client is working on this object
      table.datetime :locked_at
      # Set when all retries have failed
      table.datetime :failed_at
      # Who is working on this object (if locked)
      table.string :locked_by

      table.timestamps
    end

    add_index :delayed_jobs, [:priority, :run_at], :name => 'delayed_jobs_priority'
    add_index :delayed_jobs, [:queue], :name => 'delayed_jobs_queue'
  end

我的问题是:我怎样才能在迁移中引入额外的调试,这样我才能看到出了什么问题?如果我进入 rails 控制台并输入:

Delayed::Backend::ActiveRecord::Job.connection

我得到与队列数据库相关的信息,这意味着 self.connection 运行良好并且初始化程序按预期工作。代码示例取自 canvas-lms

谢谢你。

4

0 回答 0