Class: LcpRuby::ModelFactory::SchemaManager

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

Constant Summary

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`).



27
28
29
30
31
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 27

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.



13
14
15
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 13

def model_definition
  @model_definition
end

Instance Method Details

#ensure_table!Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/lcp_ruby/model_factory/schema_manager.rb', line 33

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