Module: BetterAuth::Rails::Migration

Defined in:
lib/better_auth/rails/migration.rb

Class Method Summary collapse

Class Method Details

.column_line(logical_field, attributes) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/better_auth/rails/migration.rb', line 40

def column_line(logical_field, attributes)
  column = attributes[:field_name] || physical_name(logical_field)
  parts = ["t.#{rails_type(attributes)} :#{column}"]
  parts << "null: false" if attributes[:required]
  default = default_value(attributes)
  parts << "default: #{default}" unless default.nil?
  "      #{parts.join(", ")}"
end

.create_table_lines(table) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/better_auth/rails/migration.rb', line 31

def create_table_lines(table)
  table_name = table.fetch(:model_name)
  lines = ["", "    create_table :#{table_name}, id: false do |t|"]
  table.fetch(:fields).each do |logical_field, attributes|
    lines << column_line(logical_field, attributes)
  end
  lines << "    end"
end

.default_value(attributes) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/better_auth/rails/migration.rb', line 91

def default_value(attributes)
  default = attributes[:default_value]
  return if default.respond_to?(:call)

  case default
  when true then "true"
  when false then "false"
  when Numeric then default.to_s
  when String then default.inspect
  end
end

.foreign_key_lines(table, options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/better_auth/rails/migration.rb', line 69

def foreign_key_lines(table, options)
  table_name = table.fetch(:model_name)
  table.fetch(:fields).filter_map do |logical_field, attributes|
    reference = attributes[:references]
    next unless reference

    column = attributes[:field_name] || physical_name(logical_field)
    target = foreign_key_target(reference.fetch(:model), options)
    on_delete = reference[:on_delete] ? ", on_delete: :#{reference[:on_delete]}" : ""
    "    add_foreign_key :#{table_name}, :#{target}, column: :#{column}#{on_delete}"
  end
end

.foreign_key_target(model, options) ⇒ Object



107
108
109
# File 'lib/better_auth/rails/migration.rb', line 107

def foreign_key_target(model, options)
  BetterAuth::Schema.auth_tables(options).fetch(model.to_s, nil)&.fetch(:model_name) || model
end

.index_lines(table) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/better_auth/rails/migration.rb', line 49

def index_lines(table)
  table_name = table.fetch(:model_name)
  table.fetch(:fields).filter_map do |logical_field, attributes|
    next unless attributes[:unique] || attributes[:index]

    column = attributes[:field_name] || physical_name(logical_field)
    unique = attributes[:unique] ? ", unique: true" : ""
    "    add_index :#{table_name}, :#{column}#{unique}"
  end
end

.migration_versionObject



25
26
27
28
29
# File 'lib/better_auth/rails/migration.rb', line 25

def migration_version
  return ::ActiveRecord::Migration.current_version if defined?(::ActiveRecord::Migration)

  "7.0"
end

.physical_name(value) ⇒ Object



103
104
105
# File 'lib/better_auth/rails/migration.rb', line 103

def physical_name(value)
  BetterAuth::Schema.send(:physical_name, value)
end

.primary_key_lines(table) ⇒ Object



60
61
62
63
64
65
66
67
# File 'lib/better_auth/rails/migration.rb', line 60

def primary_key_lines(table)
  table_name = table.fetch(:model_name)
  return [] unless table.fetch(:fields).key?("id")

  [
    %(    execute "ALTER TABLE \#{quote_table_name(:#{table_name})} ADD PRIMARY KEY (\#{quote_column_name(:id)})")
  ]
end

.rails_type(attributes) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/better_auth/rails/migration.rb', line 82

def rails_type(attributes)
  case attributes[:type]
  when "boolean" then "boolean"
  when "date" then "datetime"
  when "number" then attributes[:bigint] ? "bigint" : "integer"
  else "string"
  end
end

.render(options, migration_version: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/better_auth/rails/migration.rb', line 8

def render(options, migration_version: nil)
  migration_version ||= self.migration_version
  tables = BetterAuth::Schema.auth_tables(options)
  lines = [
    "# frozen_string_literal: true",
    "",
    "class CreateBetterAuthTables < ActiveRecord::Migration[#{migration_version}]",
    "  def change"
  ]
  tables.each_value { |table| lines.concat(create_table_lines(table)) }
  tables.each_value { |table| lines.concat(primary_key_lines(table)) }
  tables.each_value { |table| lines.concat(index_lines(table)) }
  tables.each_value { |table| lines.concat(foreign_key_lines(table, options)) }
  lines.concat(["  end", "end", ""])
  lines.join("\n")
end