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 =

type marking a table with a composite primary key, which exwiw does not support yet. schema:generate attaches it together with ignore:true. Unlike rails-managed tables, columns/belongs_tos are retained so it can serve as a signpost for adding support later.

"unsupported_composite_primary_key"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from(hash) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/exwiw/table_config.rb', line 54

def self.from(hash)
  # Reject unknown keys before deserializing: Serdes silently drops them,
  # which would turn a typo'd or unsupported key into a silent no-op (see
  # Exwiw::StrictKeys). `comment` is a declared attribute here and on the
  # nested belongs_to/column entries, so free-form notes stay accepted.
  if hash.is_a?(Hash)
    table_name = hash["name"] || hash[:name]
    StrictKeys.validate!(self, hash, owner: "table '#{table_name}'")
  end

  config = super
  config.send(:validate_after_load!)
  config
end

.from_symbol_keys(hash) ⇒ Object



69
70
71
# File 'lib/exwiw/table_config.rb', line 69

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

Instance Method Details

#belongs_to(table_name) ⇒ Object



116
117
118
# File 'lib/exwiw/table_config.rb', line 116

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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/exwiw/table_config.rb', line 120

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



88
89
90
# File 'lib/exwiw/table_config.rb', line 88

def column_names
  columns.map(&:name)
end

#merge(passed_table) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/exwiw/table_config.rb', line 163

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.bulk_insert_chunk_size = passed_table.bulk_insert_chunk_size
    merged_table.ignore = ignore
    # User-owned, never regenerated: carry over from the existing config.
    merged_table.scope_exempt = scope_exempt
    merged_table.scope_column = scope_column
    merged_table.reverse_scope = reverse_scope
    merged_table.batch_scope = batch_scope

    # Structural facts of each belongs_to come from the freshly generated
    # config, but the user-owned `comment`/`ignore`/`ignore_type`/`references`
    # carry over when the same relation still exists. (`references` is only
    # consumed by the MongodbAdapter, but it lives on the shared BelongsTo, so
    # preserve it here too rather than silently dropping a hand-added value.)
    receiver_belongs_to_by_identity = belongs_tos.each_with_object({}) { |bt, hash| hash[bt.identity] = bt }
    merged_table.belongs_tos =
      passed_table.belongs_tos.map do |passed_belongs_to|
        receiver_belongs_to = receiver_belongs_to_by_identity[passed_belongs_to.identity]
        if receiver_belongs_to
          passed_belongs_to.comment = receiver_belongs_to.comment if receiver_belongs_to.comment
          passed_belongs_to.ignore = receiver_belongs_to.ignore unless receiver_belongs_to.ignore.nil?
          passed_belongs_to.ignore_type = receiver_belongs_to.ignore_type if receiver_belongs_to.ignore_type
          passed_belongs_to.references = receiver_belongs_to.references if receiver_belongs_to.references
        end
        passed_belongs_to
      end

    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)


73
74
75
# File 'lib/exwiw/table_config.rb', line 73

def rails_managed?
  RAILS_MANAGED_TYPES.include?(type)
end

#reject_ignored_members!Object

Drop the belongs_tos/columns flagged ignore:true so they are excluded from extraction (dependency ordering, SELECT projection, INSERT). The config files on disk keep these entries; this is applied to the runtime config right after it is loaded from a file (see Runner#load_table_config).



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/exwiw/table_config.rb', line 96

def reject_ignored_members!
  self.belongs_tos = belongs_tos.reject(&:ignore)
  self.columns = columns.reject(&:ignore)

  # A table that will be extracted needs at least one effective column;
  # otherwise the generated SELECT (`SELECT  FROM ...`) and INSERT
  # (`INSERT INTO t () ...`) are syntactically broken. Checked here, after
  # rejection, so it catches both a genuinely empty `columns: []` and a list
  # left empty because every column was ignore:true. Schema generation does
  # not call this method, so regenerating a broken config still works.
  if !rails_managed? && !ignore && columns.empty?
    raise ArgumentError,
          "Table '#{name}' has no columns to extract " \
          "(it may be empty in the config or have all columns set to ignore:true); " \
          "define or unignore at least one column."
  end

  self
end

#to_hashObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/exwiw/table_config.rb', line 77

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