Module: Midnight::Rails::DefaultMigration

Extended by:
ActiveSupport::Concern
Defined in:
lib/midnight/rails/default_migration.rb

Overview

noinspection RubyDefParenthesesInspection noinspection RubyParameterNamingConvention

Instance Method Summary collapse

Instance Method Details

#change_event_table(table_name: :midnight__events, state_index_name: nil, occurred_at_index_name: nil, position_index_name: nil, concurrent_safe: false, **_) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/midnight/rails/default_migration.rb', line 34

def change_event_table(\
  table_name: :midnight__events,
  state_index_name: nil,
  occurred_at_index_name: nil,
  position_index_name: nil,
  concurrent_safe: false,
  **_
)
  # if you have an application with relatively small traffic
  # and don't have that much events to store
  # you can turn the concurrent safety on for the peace of mind
  create_table table_name do |t|
    t.binary :data, null: false
    t.binary :metadata, null: false
    t.string :event_type, null: false
    t.integer :position, null: false
    t.references :state, index: {
      name: state_index_name.presence || randomized_index_name(\
        "{#{table_name}: :state_id}"\
      )
    }
    t.datetime :occurred_at, index: {
      name: occurred_at_index_name.presence || randomized_index_name(\
        "{#{table_name}: :occurred_at}"\
      )
    }, null: false
    t.index [:state_id, :position], \
      name: position_index_name.presence || randomized_index_name(\
        "{#{table_name}:[:state_id,:position]}"\
      ), \
      unique: concurrent_safe
  end
end

#change_state_table(table_name: :midnight__states, identification_index_name: nil, concurrent_safe: true, **_) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/midnight/rails/default_migration.rb', line 12

def change_state_table(\
  table_name: :midnight__states,
  identification_index_name: nil,
  concurrent_safe: true,
  **_
)
  # if you are sure your application don't generate aggregate_key
  # that easy to collide (e.g. running number) you can turn
  # the concurrent safety off
  create_table table_name do |t|
    t.binary :state
    t.string :aggregate_key, null: false, index: {
      name: identification_index_name.presence || randomized_index_name(
        "{#{table_name}: :aggregate_key}"
      ),
      unique: concurrent_safe
    }
    t.integer :next_position, default: 1
    t.integer :lock_version, default: 0
  end
end