Module: TypedEAV::SchemaPortability

Defined in:
lib/typed_eav/schema_portability.rb

Overview

Export and import field + section definitions for an exact partition tuple. Value rows are intentionally out of scope.

Class Method Summary collapse

Class Method Details

.export_schema(entity_type:, scope: nil, parent_scope: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/typed_eav/schema_portability.rb', line 8

def export_schema(entity_type:, scope: nil, parent_scope: nil)
  fields = TypedEAV::Field::Base
           .where(entity_type: entity_type, scope: scope, parent_scope: parent_scope)
           .includes(:field_options)
           .order(:sort_order)
           .map { |field| export_field_entry(field) }

  sections = TypedEAV::Section
             .where(entity_type: entity_type, scope: scope, parent_scope: parent_scope)
             .order(:sort_order)
             .map { |section| export_section_entry(section) }

  {
    "schema_version" => 1,
    "entity_type" => entity_type,
    "scope" => scope,
    "parent_scope" => parent_scope,
    "fields" => fields,
    "sections" => sections,
  }
end

.import_schema(hash, on_conflict: :error) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/typed_eav/schema_portability.rb', line 30

def import_schema(hash, on_conflict: :error)
  validate_schema_version!(hash)
  validate_conflict_policy!(on_conflict)

  result = { "created" => 0, "updated" => 0, "skipped" => 0, "unchanged" => 0, "errors" => [] }

  TypedEAV::Field::Base.transaction do
    Array(hash["fields"]).each do |entry|
      import_field_entry(entry, on_conflict, result)
    end

    Array(hash["sections"]).each do |entry|
      import_section_entry(entry, on_conflict, result)
    end
  end

  result
end