Module: Kaal::Internal::ActiveRecord::MigrationTemplates

Defined in:
lib/kaal/internal/active_record/migration_templates.rb

Overview

Rails migration templates for Active Record-backed Kaal tables.

Class Method Summary collapse

Class Method Details

.definitions_template(backend) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 79

def definitions_template(backend)
   =
    if backend == 'mysql'
      't.text :metadata, null: false'
    else
      "t.text :metadata, null: false, default: '{}'"
    end

  <<~RUBY
    class CreateKaalDefinitions < ActiveRecord::Migration[7.1]
      def change
        create_table :kaal_definitions do |t|
          t.string :key, null: false
          t.string :cron, null: false
          t.boolean :enabled, null: false, default: true
          t.string :source, null: false
          #{}
          t.datetime :disabled_at
          t.datetime :created_at, null: false
          t.datetime :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



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 109

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

  <<~RUBY
    class CreateKaalDelayedJobs < ActiveRecord::Migration[7.1]
      def change
        create_table :kaal_delayed_jobs do |t|
          t.string :job_id, null: false
          t.datetime :run_at, null: false
          t.string :job_class, null: false
          #{args_definition}
          t.string :queue
          t.datetime :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



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 40

def dispatches_template
  <<~RUBY
    class CreateKaalDispatches < ActiveRecord::Migration[7.1]
      def change
        create_table :kaal_dispatches do |t|
          t.string :key, null: false
          t.datetime :fire_time, null: false
          t.datetime :dispatched_at, null: false
          t.string :node_id, null: false
          t.string :status, null: false, default: 'dispatched', limit: 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



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 14

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('sqlite'),
      '004_create_kaal_delayed_jobs.rb' => delayed_jobs_template('sqlite')
    }
  when 'postgres'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_definitions.rb' => definitions_template('postgres'),
      '003_create_kaal_delayed_jobs.rb' => delayed_jobs_template('postgres')
    }
  when 'mysql'
    {
      '001_create_kaal_dispatches.rb' => dispatches_template,
      '002_create_kaal_definitions.rb' => definitions_template('mysql'),
      '003_create_kaal_delayed_jobs.rb' => delayed_jobs_template('mysql')
    }
  else
    {}
  end
end

.locks_templateObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 62

def locks_template
  <<~RUBY
    class CreateKaalLocks < ActiveRecord::Migration[7.1]
      def change
        create_table :kaal_locks do |t|
          t.string :key, null: false
          t.datetime :acquired_at, null: false
          t.datetime :expires_at, null: false
        end

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