Class: Postspec::State

Inherits:
Object
  • Object
show all
Defined in:
lib/postspec/state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cleanObject

Returns the value of attribute clean.



6
7
8
# File 'lib/postspec/state.rb', line 6

def clean
  @clean
end

#created_atObject (readonly)

Returns the value of attribute created_at.



8
9
10
# File 'lib/postspec/state.rb', line 8

def created_at
  @created_at
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/postspec/state.rb', line 3

def id
  @id
end

#modeObject (readonly)

Returns the value of attribute mode.



4
5
6
# File 'lib/postspec/state.rb', line 4

def mode
  @mode
end

#readyObject

Returns the value of attribute ready.



5
6
7
# File 'lib/postspec/state.rb', line 5

def ready
  @ready
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/postspec/state.rb', line 7

def status
  @status
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



9
10
11
# File 'lib/postspec/state.rb', line 9

def updated_at
  @updated_at
end

Class Method Details

.create(conn, mode) ⇒ Object



25
26
27
28
29
30
# File 'lib/postspec/state.rb', line 25

def self.create(conn, mode)
  id, created_at = conn.tuple <<~EOS
      insert into postspec.runs (mode) values ('#{mode}') returning id, created_at
  EOS
  State.new(conn, id, mode, false, false, nil, created_at, nil)
end

.dirty?(conn) ⇒ Boolean

Return true if change-tables contains any records. It is used to check if the developer made any changes to the database after a (successful) postspec run

Returns:

  • (Boolean)


35
36
37
38
39
40
41
# File 'lib/postspec/state.rb', line 35

def self.dirty?(conn)
  conn.value(%(
    select true as present from postspec.inserts
    union select true from postspec.updates
    union select true from postspec.deletes
  )) || false
end

.ensure(conn, mode) ⇒ Object



43
44
# File 'lib/postspec/state.rb', line 43

def self.ensure(conn, mode)
end

.read(conn) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/postspec/state.rb', line 46

def self.read(conn)
  tuples = conn.tuples <<~EOS
      select id, mode, ready, clean, status, created_at, updated_at
      from postspec.runs
      order by id desc
      limit 1
  EOS
  tuple = tuples.first
  tuple && State.new(conn, *tuple)
end

.write(conn, state) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/postspec/state.rb', line 57

def self.write(conn, state)
  conn.exec <<~EOS
      update postspec.runs
          set ready = #{state.ready},
              clean = #{state.clean},
              status = #{state.status.nil? ? 'null' : state.status},
              updated_at = now() at time zone 'UTC'
          where id = #{state.id}
  EOS
  @updated_at = conn.value "select updated_at from postspec.runs where id = #{state.id}"
end

Instance Method Details

#deletedObject



18
# File 'lib/postspec/state.rb', line 18

def deleted() get_multimap("deletes") end

#dumpObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/postspec/state.rb', line 69

def dump
  puts "State"
  indent {
    puts "id: #{id.inspect}"
    puts "mode: #{mode.inspect}"
    puts "ready: #{ready.inspect}"
    puts "clean: #{clean.inspect}"
    puts "status: #{status.inspect}"
    puts "duration: #{@duration.inspect}"
    puts "created_at: #{created_at.inspect}"
  }
end

#durationObject



11
# File 'lib/postspec/state.rb', line 11

def duration() @duraction ||= (1000 * (updated_at - created_at)).round(0) end

#insertedObject

Maps from table UID to sorted list of record IDs. TODO: Are they in use? Should they be used?? Used from postspec! FIXME: Move to Frame - maybe?



16
# File 'lib/postspec/state.rb', line 16

def inserted() get_multimap("inserts") end

#refreshObject



23
# File 'lib/postspec/state.rb', line 23

def refresh() @inserted = @updated = @deleted = @seeds = nil end

#seedsObject

Map from table UID to max record ID for that table



21
# File 'lib/postspec/state.rb', line 21

def seeds() @seeds ||= get_map("seeds") end

#updatedObject



17
# File 'lib/postspec/state.rb', line 17

def updated() get_multimap("updates") end