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 both here and in the add_que generator template.

3

Class Method Summary collapse

Class Method Details

.db_versionObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/que/migrations.rb', line 32

def db_version
  result = Que.execute "    SELECT relname, description\n    FROM pg_class\n    LEFT JOIN pg_description ON pg_description.objoid = pg_class.oid\n    WHERE relname = 'que_jobs'\n  SQL\n\n  if result.none?\n    # No table in the database at all.\n    0\n  elsif (d = result.first[:description]).nil?\n    # There's a table, it was just created before the migration system existed.\n    1\n  else\n    d.to_i\n  end\nend\n"

.migrate!(options = {:version => CURRENT_VERSION}) ⇒ Object



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

def migrate!(options = {:version => CURRENT_VERSION})
  transaction do
    version = options[:version]

    if (current = db_version) == 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|
      sql = File.read("#{File.dirname(__FILE__)}/migrations/#{step}/#{direction}.sql")
      Que.execute(sql)
    end

    set_db_version(version)
  end
end

.set_db_version(version) ⇒ Object



51
52
53
54
# File 'lib/que/migrations.rb', line 51

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

.transactionObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/que/migrations.rb', line 56

def transaction
  Que.adapter.checkout do
    if Que.adapter.in_transaction?
      yield
    else
      begin
        Que.execute "BEGIN"
        yield
      rescue => error
        raise
      ensure
        # Handle a raised error or a killed thread.
        if error || Thread.current.status == 'aborting'
          Que.execute "ROLLBACK"
        else
          Que.execute "COMMIT"
        end
      end
    end
  end
end