Module: Kaal::ActiveRecord

Defined in:
lib/kaal/active_record_support.rb

Overview

Active Record migration/install support for SQL-backed Kaal backends.

Class Method Summary collapse

Class Method Details

.alphanumeric?(char) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/kaal/active_record_support.rb', line 76

def alphanumeric?(char)
  char.between?('a', 'z') ||
    char.between?('A', 'Z') ||
    char.between?('0', '9')
end

.default_migration_class_for(backend) ⇒ Object



66
67
68
# File 'lib/kaal/active_record_support.rb', line 66

def default_migration_class_for(backend)
  "CreateKaal#{backend.capitalize}Backend"
end

.install_migrations(target_dir:, backend:, migration_name: nil, time_source: -> { Time.now.utc }) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/kaal/active_record_support.rb', line 26

def install_migrations(target_dir:, backend:, migration_name: nil, time_source: -> { Time.now.utc })
  class_name = normalize_migration_name(migration_name, fallback: default_migration_class_for(backend))
  base_path = File.expand_path(target_dir)
  FileUtils.mkdir_p(base_path)
  templates = Kaal::Internal::ActiveRecord::MigrationTemplates.for_backend(backend)

  templates.map.with_index do |(_name, contents), index|
    suffix = underscore(class_name)
    suffix = "#{suffix}_#{migration_suffixes_for(backend).fetch(index)}" if templates.length > 1
    path = File.expand_path("#{(time_source.call + index).strftime('%Y%m%d%H%M%S')}_#{suffix}.rb", base_path)
    File.write(path, contents)
    path
  end
end

.install_mysql_migration(target_dir:, migration_name: 'Create Kaal MySQL Backend') ⇒ Object



18
19
20
# File 'lib/kaal/active_record_support.rb', line 18

def install_mysql_migration(target_dir:, migration_name: 'Create Kaal MySQL Backend')
  install_migrations(target_dir:, backend: 'mysql', migration_name:)
end

.install_postgres_migration(target_dir:, migration_name: 'Create Kaal Postgres Backend') ⇒ Object



14
15
16
# File 'lib/kaal/active_record_support.rb', line 14

def install_postgres_migration(target_dir:, migration_name: 'Create Kaal Postgres Backend')
  install_migrations(target_dir:, backend: 'postgres', migration_name:)
end

.install_sqlite_migration(target_dir:, migration_name: 'Create Kaal SQLite Backend') ⇒ Object



22
23
24
# File 'lib/kaal/active_record_support.rb', line 22

def install_sqlite_migration(target_dir:, migration_name: 'Create Kaal SQLite Backend')
  install_migrations(target_dir:, backend: 'sqlite', migration_name:)
end

.migration_suffixes_for(backend) ⇒ Object



70
71
72
73
74
# File 'lib/kaal/active_record_support.rb', line 70

def migration_suffixes_for(backend)
  return %w[dispatches locks definitions delayed_jobs] if backend.to_s == 'sqlite'

  %w[dispatches definitions delayed_jobs]
end

.normalize_migration_name(name, fallback:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/kaal/active_record_support.rb', line 50

def normalize_migration_name(name, fallback:)
  normalized = name.to_s.each_char.with_object(+'') do |char, buffer|
    if alphanumeric?(char)
      buffer << char
    elsif !buffer.empty? && !buffer.end_with?(' ')
      buffer << ' '
    end
  end.split.map!(&:capitalize).join
  normalized.empty? ? fallback : normalized
end

.require_activerecord!Object



41
42
43
44
45
46
47
48
# File 'lib/kaal/active_record_support.rb', line 41

def require_activerecord!
  require 'active_record'
  require 'active_support/inflector'
rescue LoadError => e
  raise LoadError,
        "#{e.message}. Add `gem 'activerecord'` to your Gemfile to use Active Record-backed Kaal SQL support.",
        cause: e
end

.underscore(value) ⇒ Object



61
62
63
64
# File 'lib/kaal/active_record_support.rb', line 61

def underscore(value)
  require_activerecord!
  ::ActiveSupport::Inflector.underscore(value)
end