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



76
77
78
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
# File 'lib/kaal/internal/active_record/migration_templates.rb', line 76

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

.dispatches_templateObject



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

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

.locks_templateObject



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

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