Class: LcpRuby::ModelFactory::SchemaManager

Inherits:
Object
  • Object
show all
Includes:
ManagedTracking
Defined in:
lib/lcp_ruby/model_factory/schema_manager.rb

Constant Summary collapse

DEFAULT_DECIMAL_PRECISION =

Default precision/scale for a ‘decimal` field that declares neither (via type nor field column_options). Without this MySQL/MariaDB truncates to DECIMAL(10,0). 16 total digits with 4 fractional covers money and rate values with headroom; authors override per field/type.

16
DEFAULT_DECIMAL_SCALE =
4

Constants included from ManagedTracking

ManagedTracking::EMPTY_SUMMARY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ManagedTracking

reset_all!, #track!

Constructor Details

#initialize(model_definition, host_class: nil, mode: :full) ⇒ SchemaManager

‘mode` is :full (default) for dynamic models — runs create_table!, update_table!, or add_sti_child_columns! depending on table state. `mode: :managed_only` is for bind_to: host classes — runs the additive `apply_managed!` path, which only adds columns/indexes marked `lcp_managed: true` in YAML and never touches existing host-owned columns.

‘host_class` is required when mode is :managed_only (so the tracking ivar lands on the right class and so connection-aware operations use the host’s own connection — important for multi-database setups where the host class uses ‘connects_to database: :reporting`).



34
35
36
37
38
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 34

def initialize(model_definition, host_class: nil, mode: :full)
  @model_definition = model_definition
  @host_class = host_class
  @mode = mode
end

Instance Attribute Details

#model_definitionObject (readonly)

Returns the value of attribute model_definition.



20
21
22
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 20

def model_definition
  @model_definition
end

Instance Method Details

#ensure_table!Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 40

def ensure_table!
  return if model_definition.virtual?

  case @mode
  when :managed_only
    apply_managed!
    return
  when :full
    # fall through to existing behavior
  else
    raise ArgumentError, "SchemaManager: unknown mode #{@mode.inspect}"
  end

  # STI children add their columns to the parent's (already existing) table
  if model_definition.sti_child?
    add_sti_child_columns!
    return
  end

  table = model_definition.table_name

  if ActiveRecord::Base.connection.table_exists?(table)
    update_table!
  else
    create_table!
  end
end