Module: Que::Scheduler::Migrations

Defined in:
lib/que/scheduler/migrations.rb

Constant Summary collapse

AUDIT_TABLE_NAME =
Que::Scheduler::Audit::TABLE_NAME
TABLE_COMMENT =
%(
  SELECT description FROM pg_class
  LEFT JOIN pg_description ON pg_description.objoid = pg_class.oid
  WHERE relname = '#{AUDIT_TABLE_NAME}'
).freeze
MAX_VERSION =
Dir.glob("#{__dir__}/migrations/*").map { |d| File.basename(d) }.map(&:to_i).max

Class Method Summary collapse

Class Method Details

.audit_table_exists?Boolean

rubocop:enable MagicNumbers/NoReturn

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/que/scheduler/migrations.rb', line 41

def audit_table_exists?
  result = Que::Scheduler::DbSupport.execute(<<-SQL)
    SELECT * FROM information_schema.tables WHERE table_name = '#{AUDIT_TABLE_NAME}';
  SQL
  result.any?
end

.db_versionObject

rubocop:disable MagicNumbers/NoReturn



30
31
32
33
34
35
36
37
38
# File 'lib/que/scheduler/migrations.rb', line 30

def db_version
  if audit_table_exists?
    return Que::Scheduler::DbSupport.execute(TABLE_COMMENT).first[:description].to_i
  end

  # At this point we used to be able to tell if it was 0 or 1 by the presence of the
  # que_scheduler job, but that isn't auto enqueued anymore, so we assume it is 0.
  0
end

.migrate!(version:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/que/scheduler/migrations.rb', line 15

def migrate!(version:)
  # Like que, Do not migrate test DBs.
  return if defined?(Que::Testing)

  Que::Scheduler::Db.transaction do
    current = db_version
    if current < version
      migrate_up(current, version)
    elsif current > version
      migrate_down(current, version)
    end
  end
end

.reenqueue_scheduler_if_missingObject

This method must be used during initial installation of que-scheduler and if the project migrations are squashed.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/que/scheduler/migrations.rb', line 50

def reenqueue_scheduler_if_missing
  raise <<~MSG unless Que::Migrations.db_version >= 6
    Cannot (re)enqueue the que-scheduler worker unless the Que migrations have been run to at least version 6.
    This probably means you have an old migration that installed que-scheduler, and have then since
    run in another migration that has upgraded que, and are now running the migrations from scratch into a new database.

    To fix this, you should remove the "Que::Scheduler::Migrations.reenqueue_scheduler_if_missing" line from any
    of the older migrations and add it after the last migration that updates Que to at least version 6. eg:

    Que.migrate!(version: 6)
    Que::Scheduler::Migrations.reenqueue_scheduler_if_missing
  MSG
  return unless Que::Scheduler::Db.count_schedulers.zero?

  Que::Scheduler::DbSupport.enqueue_a_job(Que::Scheduler::SchedulerJob)
end