Class: Exwiw::TableConfig

Inherits:
Object
  • Object
show all
Includes:
Serdes
Defined in:
lib/exwiw/table_config.rb

Constant Summary collapse

RAILS_MANAGED_SCHEMA_MIGRATIONS =
"rails_managed_schema_migrations"
RAILS_MANAGED_INTERNAL_METADATA =
"rails_managed_internal_metadata"
RAILS_MANAGED_TYPES =
[
  RAILS_MANAGED_SCHEMA_MIGRATIONS,
  RAILS_MANAGED_INTERNAL_METADATA,
].freeze
UNSUPPORTED_COMPOSITE_PRIMARY_KEY =

exwiw が現状サポートしていない複合主キーのテーブルを表す type。schema:generate が skip:true と併せて付与する。将来サポートする際の目印になるよう、rails-managed とは異なり columns/belongs_tos は保持する。

"unsupported_composite_primary_key"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(hash) ⇒ Object



29
30
31
32
33
# File 'lib/exwiw/table_config.rb', line 29

def self.from(hash)
  config = super
  config.send(:validate_after_load!)
  config
end

.from_symbol_keys(hash) ⇒ Object



35
36
37
# File 'lib/exwiw/table_config.rb', line 35

def self.from_symbol_keys(hash)
  from(JSON.parse(hash.to_json))
end

Instance Method Details

#belongs_to(table_name) ⇒ Object



56
57
58
# File 'lib/exwiw/table_config.rb', line 56

def belongs_to(table_name)
  belongs_tos.find { |relation| relation.table_name == table_name }
end

#build_extract_query(extract_target_table, extract_target_ids, tables_by_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/exwiw/table_config.rb', line 60

def build_extract_query(extract_target_table, extract_target_ids, tables_by_name)
  # target is itself
  if name == extract_target_table
    return [{
      from: name,
      where: [{ primary_key => extract_target_ids }],
      join: [],
      select: column_names,
    }]
  end

  # it is not related to target table
  if belongs_to.empty?
    return [{
      from: name,
      where: [],
      join: [],
      select: column_names,
    }]
  end

  belongs_to_extract_target_table = belongs_tos.find { |relation| relation.table_name == extract_target_table }
  if belongs_to_extract_target_table
    key = belongs_to_extract_target_table.foreign_key
    return [{ from: name, where: [{ key => extract_target_ids }], join: [], select: column_names }]
  end

  ret = compute_dependency_to_table(extract_target_table, tables_by_name)

  if ret.empty?
    [{
      from: name,
      where: [],
      join: [],
      select: column_names,
    }]
  else
    last = ret.last
    last[:where] = [{ last[:foreign_key] => extract_target_ids }]
    ret
  end
end

#column_namesObject



52
53
54
# File 'lib/exwiw/table_config.rb', line 52

def column_names
  columns.map(&:name)
end

#merge(passed_table) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/exwiw/table_config.rb', line 103

def merge(passed_table)
  return passed_table if passed_table.to_hash == self.to_hash


  TableConfig.new.tap do |merged_table|
    merged_table.name = name
    merged_table.primary_key = passed_table.primary_key
    merged_table.type = passed_table.type
    merged_table.comment = comment
    merged_table.filter = filter
    merged_table.belongs_tos = passed_table.belongs_tos
    merged_table.bulk_insert_chunk_size = passed_table.bulk_insert_chunk_size
    merged_table.skip = skip

    receiver_column_by_name = columns.each_with_object({}) { |column, hash| hash[column.name] = column }

    merged_table.columns =
      passed_table.columns.map do |passed_column|
        if receiver_column_by_name.key?(passed_column.name)
          receiver_column = receiver_column_by_name[passed_column.name]
          receiver_column
        else
          passed_column
        end
      end
  end
end

#rails_managed?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/exwiw/table_config.rb', line 39

def rails_managed?
  RAILS_MANAGED_TYPES.include?(type)
end

#to_hashObject



43
44
45
46
47
48
49
50
# File 'lib/exwiw/table_config.rb', line 43

def to_hash
  hash = super
  if rails_managed?
    hash.delete("belongs_tos")
    hash.delete("columns")
  end
  hash
end