Module: Que::Migrations

Defined in:
lib/que/migrations.rb

Constant Summary collapse

CURRENT_VERSION =

In order to ship a schema change, add the relevant up and down sql files to the migrations directory, and bump the version here.

7

Class Method Summary collapse

Class Method Details

._raise_db_version_comment_missing_errorObject

The que_jobs table could be missing the schema version comment either due to:

  • Being created before the migration system existed; or

  • A bug in Rails schema dump in some versions of Rails

The former is the case on Que versions prior to v0.5.0 (2014-01-14). Upgrading directly from there is unsupported, so we just raise in all cases of the comment being missing

Raises:



62
63
64
65
66
67
68
69
70
# File 'lib/que/migrations.rb', line 62

def _raise_db_version_comment_missing_error
  raise Error, <<~ERROR
    Cannot determine Que DB schema version.

    The que_jobs table is missing its comment recording the Que DB schema version. This is likely due to a bug in Rails schema dump in Rails 7 versions prior to 7.0.3, omitting comments - see https://github.com/que-rb/que/issues/363. Please determine the appropriate schema version from your migrations and record it manually by running the following SQL (replacing version as appropriate):

    COMMENT ON TABLE que_jobs IS 'version';
  ERROR
end

.db_versionObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/que/migrations.rb', line 38

def db_version
  result =
    Que.execute <<-SQL
      SELECT relname, description
      FROM pg_class
      LEFT JOIN pg_description ON pg_description.objoid = pg_class.oid
      WHERE relname = 'que_jobs'
    SQL

  if result.none?
    # No table in the database at all.
    0
  elsif (d = result.first[:description]).nil?
    # The table exists but the version comment is missing
    _raise_db_version_comment_missing_error
  else
    d.to_i
  end
end

.migrate!(version:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/que/migrations.rb', line 10

def migrate!(version:)
  Que.transaction do
    current = db_version

    if current == version
      return
    elsif current < version
      direction = :up
      steps = ((current + 1)..version).to_a
    elsif current > version
      direction = :down
      steps = ((version + 1)..current).to_a.reverse
    end

    steps.each do |step|
      filename = [
        File.dirname(__FILE__),
        'migrations',
        step,
        direction,
      ].join('/') << '.sql'
      Que.execute(File.read(filename))
    end

    set_db_version(version)
  end
end

.set_db_version(version) ⇒ Object



72
73
74
75
# File 'lib/que/migrations.rb', line 72

def set_db_version(version)
  i = version.to_i
  Que.execute "COMMENT ON TABLE que_jobs IS '#{i}'" unless i.zero?
end