Module: EventSourcery::Postgres::TableOwner

Defined in:
lib/event_sourcery/postgres/table_owner.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

DefaultTableError =
Class.new(StandardError)
NoSuchTableError =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



7
8
9
# File 'lib/event_sourcery/postgres/table_owner.rb', line 7

def self.prepended(base)
  base.extend(ClassMethods)
end

Instance Method Details

#resetObject

Reset by dropping each table.



38
39
40
41
42
43
44
45
46
47
# File 'lib/event_sourcery/postgres/table_owner.rb', line 38

def reset
  self.class.tables.keys.each do |table_name|
    prefixed_name = table_name_prefixed(table_name)
    if @db_connection.table_exists?(prefixed_name)
      @db_connection.drop_table(prefixed_name, cascade: true)
    end
  end
  super if defined?(super)
  setup
end

#setupObject

Create each table.



29
30
31
32
33
34
35
# File 'lib/event_sourcery/postgres/table_owner.rb', line 29

def setup
  self.class.tables.each do |table_name, schema_block|
    prefixed_name = table_name_prefixed(table_name)
    @db_connection.create_table?(prefixed_name, &schema_block)
  end
  super if defined?(super)
end

#truncateObject

This will truncate all the tables and reset the tracker back to 0, done as a transaction.



51
52
53
54
55
56
57
58
59
# File 'lib/event_sourcery/postgres/table_owner.rb', line 51

def truncate
  self.class.tables.each do |table_name, _|
    @db_connection.transaction do
      prefixed_name = table_name_prefixed(table_name)
      @db_connection[prefixed_name].truncate
      tracker.reset_last_processed_event_id(self.class.processor_name)
    end
  end
end