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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/kaal/persistence/migration_templates.rb', line 76

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

.delayed_jobs_template(backend) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kaal/persistence/migration_templates.rb', line 100

def delayed_jobs_template(backend)
  args_definition =
    if backend == 'mysql'
      'String :args, text: true, null: false'
    else
      "String :args, text: true, null: false, default: '[]'"
    end

  <<~RUBY
    Sequel.migration do
      change do
        create_table?(:kaal_delayed_jobs) do
          primary_key :id
          String :job_id, null: false
          Time :run_at, null: false
          String :job_class, null: false
          #{args_definition}
          String :queue
          Time :created_at, null: false
        end

        add_index :kaal_delayed_jobs, :job_id, unique: true
        add_index :kaal_delayed_jobs, :run_at
      end
    end
  RUBY
end

.dispatches_templateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/kaal/persistence/migration_templates.rb', line 35

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
30
31
32
33
# File 'lib/kaal/persistence/migration_templates.rb', line 13

def for_backend(backend)
  backend_name = backend.to_s

  case backend_name
  when 'sqlite'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_locks.rb' => locks_template,
      '003_create_kaal_definitions.rb' => definitions_template,
      '004_create_kaal_delayed_jobs.rb' => delayed_jobs_template('sqlite')
    }
  when 'postgres', 'mysql'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_definitions.rb' => definitions_template,
      '003_create_kaal_delayed_jobs.rb' => delayed_jobs_template(backend_name)
    }
  else
    {}
  end
end

.locks_templateObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/kaal/persistence/migration_templates.rb', line 58

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