Module: Kaal::Persistence::MigrationTemplates

Defined in:
lib/kaal/persistence/migration_templates.rb

Overview

Sequel migration templates emitted by ‘kaal init`.

Class Method Summary collapse

Class Method Details

.definitions_templateObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kaal/persistence/migration_templates.rb', line 72

def definitions_template
  <<~RUBY
    Sequel.migration do
      change do
        create_table?(:kaal_definitions) do
          primary_key :id
          String :key, null: false
          String :cron, null: false
          TrueClass :enabled, null: false, default: true
          String :source, null: false
          String :metadata, text: true, null: false, default: '{}'
          Time :disabled_at
          Time :created_at, null: false
          Time :updated_at, null: false
        end

        add_index :kaal_definitions, :key, unique: true
        add_index :kaal_definitions, :enabled
        add_index :kaal_definitions, :source
      end
    end
  RUBY
end

.dispatches_templateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/kaal/persistence/migration_templates.rb', line 31

def dispatches_template
  <<~RUBY
    Sequel.migration do
      change do
        create_table?(:kaal_dispatches) do
          primary_key :id
          String :key, null: false
          Time :fire_time, null: false
          Time :dispatched_at, null: false
          String :node_id, null: false
          String :status, null: false, default: 'dispatched', size: 50
        end

        add_index :kaal_dispatches, [:key, :fire_time], unique: true
        add_index :kaal_dispatches, :key
        add_index :kaal_dispatches, :node_id
        add_index :kaal_dispatches, :status
        add_index :kaal_dispatches, :fire_time
      end
    end
  RUBY
end

.for_backend(backend) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kaal/persistence/migration_templates.rb', line 13

def for_backend(backend)
  case backend.to_s
  when 'sqlite'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_locks.rb' => locks_template,
      '003_create_kaal_definitions.rb' => definitions_template
    }
  when 'postgres', 'mysql'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_definitions.rb' => definitions_template
    }
  else
    {}
  end
end

.locks_templateObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kaal/persistence/migration_templates.rb', line 54

def locks_template
  <<~RUBY
    Sequel.migration do
      change do
        create_table?(:kaal_locks) do
          primary_key :id
          String :key, null: false
          Time :acquired_at, null: false
          Time :expires_at, null: false
        end

        add_index :kaal_locks, :key, unique: true
        add_index :kaal_locks, :expires_at
      end
    end
  RUBY
end