Class: CouchTomato::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/couch_tomato/migration.rb

Class Method Summary collapse

Class Method Details

.announce(message) ⇒ Object



36
37
38
39
40
# File 'lib/couch_tomato/migration.rb', line 36

def announce(message)
  text = "#{@version} #{name}: #{message}"
  length = [0, 75 - text.length].max
  write "== %s %s" % [text, "=" * length]
end

.migrate(direction, db) ⇒ Object

Execute this migration in the named direction



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/couch_tomato/migration.rb', line 5

def migrate(direction, db)
  return unless respond_to?(direction)

  write db.name
  case direction
    when :up   then announce "migrating"
    when :down then announce "reverting"
  end

  time = Benchmark.measure do
    docs = []

    rows = db.get('_all_docs', :include_docs => true)['rows']
    rows.each do |row|
      next if row['id'] =~ /^_design/
      docs << row['doc'] if send(direction, row['doc'])
    end

    db.bulk_save(docs) unless docs.empty?
  end

  case direction
    when :up   then announce "migrated (%.4fs)" % time.real; write
    when :down then announce "reverted (%.4fs)" % time.real; write
  end
end

.say(message, subitem = false) ⇒ Object



42
43
44
# File 'lib/couch_tomato/migration.rb', line 42

def say(message, subitem=false)
  write "#{subitem ? "   ->" : "--"} #{message}"
end

.say_with_time(message) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/couch_tomato/migration.rb', line 46

def say_with_time(message)
  say(message)
  result = nil
  time = Benchmark.measure { result = yield }
  say "%.4fs" % time.real, :subitem
  say("#{result} rows", :subitem) if result.is_a?(Integer)
  result
end

.write(text = "") ⇒ Object



32
33
34
# File 'lib/couch_tomato/migration.rb', line 32

def write(text="")
  puts(text)
end