Module: PartitionGardener::ConfigDocument

Defined in:
lib/partition_gardener/config_document.rb

Constant Summary collapse

SCHEMA_PATH =
File.expand_path("../../docs/schemas/partition_garden.schema.json", __dir__)
PLAN_SCHEMA_PATH =
File.expand_path("../../docs/schemas/plan_report.schema.json", __dir__)
PORTABLE_KEYS =
%i[
  table_name
  layout
  bucket
  partition_key_column
  conflict_key
  active_months
  active_days
  active_weeks
  active_quarters
  active_years
  premake_months
  split_row_threshold
  move_batch_size
  statement_timeout
  retention_months
  retention_apply
  retention_keep_table
  retention_detach_concurrently
  align_child_columns
  hash_modulus
  maintenance_backend
  incremental_rebalance
  run_record_enabled
  analyze_after_rebalance
].freeze
IMPORTABLE_LAYOUTS =
%w[
  sliding_window
  rolling_current
  calendar_year
  premake_monthly
  integer_window
  hash_branches
].freeze
ALLOWED_REGISTRY_KEYS =
(
  %w[table_name layout partition_key_column conflict_key bucket] +
  PORTABLE_KEYS.map(&:to_s)
).uniq.freeze

Class Method Summary collapse

Class Method Details

.export(config) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/partition_gardener/config_document.rb', line 49

def export(config)
  PORTABLE_KEYS.each_with_object({}) do |key, document|
    next unless config.key?(key)

    document[key] = config[key]
  end
end

.export_all(configs) ⇒ Object



57
58
59
# File 'lib/partition_gardener/config_document.rb', line 57

def export_all(configs)
  configs.map { |config| export(config) }
end

.from_hash(document) ⇒ Object



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
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/partition_gardener/config_document.rb', line 61

def from_hash(document)
  layout = document.fetch("layout").to_sym
  table_name = document.fetch("table_name")
  partition_key_column = document.fetch("partition_key_column")
  conflict_key = document.fetch("conflict_key")
  options = document.except("table_name", "layout", "partition_key_column", "conflict_key").transform_keys(&:to_sym)

  case layout
  when :sliding_window
    import_sliding_window(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :rolling_current
    Templates.rolling_current_monthly(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :premake_monthly
    Templates.premake_monthly(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :calendar_year
    Templates.calendar_year(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :integer_window
    Templates.integer_window(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :hash_branches
    Templates.hash_branches(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  else
    raise ArgumentError, "layout #{layout} is not supported in JSON config import"
  end
end

.import_sliding_window(table_name:, partition_key_column:, conflict_key:, bucket: :month, **options) ⇒ Object



116
117
118
119
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
# File 'lib/partition_gardener/config_document.rb', line 116

def import_sliding_window(table_name:, partition_key_column:, conflict_key:, bucket: :month, **options)
  case bucket.to_sym
  when :day
    Templates.sliding_window_daily(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :week
    Templates.sliding_window_weekly(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  when :quarter
    Templates.sliding_window_quarterly(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  else
    Templates.sliding_window_monthly(
      table_name: table_name,
      partition_key_column: partition_key_column,
      conflict_key: conflict_key,
      **options
    )
  end
end

.load_registry_file!(path) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/partition_gardener/config_document.rb', line 149

def load_registry_file!(path)
  Registry.reset!
  payload = JSON.parse(File.read(path))

  case payload
  when Array
    payload.each { |document| register_validated_document!(document) }
  when Hash
    if payload["tables"].is_a?(Array)
      payload["tables"].each { |document| register_validated_document!(document) }
    else
      register_validated_document!(payload)
    end
  else
    raise ArgumentError, "registry file must be a JSON object or array"
  end
end

.register_validated_document!(document) ⇒ Object



245
246
247
# File 'lib/partition_gardener/config_document.rb', line 245

def register_validated_document!(document)
  Registry.register(from_hash(validate_registry_document!(document)))
end

.validate_registry_conflict_key!(document) ⇒ Object

Raises:

  • (ArgumentError)


213
214
215
216
217
218
# File 'lib/partition_gardener/config_document.rb', line 213

def validate_registry_conflict_key!(document)
  conflict_key = document.fetch("conflict_key")
  return if conflict_key.is_a?(Array) && conflict_key.any? && conflict_key.all? { |column| column.is_a?(String) && !column.empty? }

  raise ArgumentError, "conflict_key must be a non-empty array of strings"
end

.validate_registry_core_strings!(document) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/partition_gardener/config_document.rb', line 203

def validate_registry_core_strings!(document)
  unless document.fetch("table_name").is_a?(String) && !document.fetch("table_name").empty?
    raise ArgumentError, "table_name must be a non-empty string"
  end

  unless document.fetch("partition_key_column").is_a?(String) && !document.fetch("partition_key_column").empty?
    raise ArgumentError, "partition_key_column must be a non-empty string"
  end
end

.validate_registry_document!(document) ⇒ Object

Raises:

  • (ArgumentError)


167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/partition_gardener/config_document.rb', line 167

def validate_registry_document!(document)
  raise ArgumentError, "registry entry must be a JSON object" unless document.is_a?(Hash)

  validate_registry_required_keys!(document)
  validate_registry_unknown_keys!(document)
  validate_registry_layout!(document)
  validate_registry_core_strings!(document)
  validate_registry_conflict_key!(document)
  validate_registry_optional_bucket!(document)
  validate_registry_optional_maintenance_backend!(document)
  validate_registry_optional_boolean!(document, "align_child_columns")
  document
end

.validate_registry_layout!(document) ⇒ Object

Raises:

  • (ArgumentError)


195
196
197
198
199
200
201
# File 'lib/partition_gardener/config_document.rb', line 195

def validate_registry_layout!(document)
  layout = document.fetch("layout")
  return if layout.is_a?(String) && IMPORTABLE_LAYOUTS.include?(layout)

  supported = IMPORTABLE_LAYOUTS.join(", ")
  raise ArgumentError, "layout #{layout.inspect} is not supported in JSON import (supported: #{supported}; composite and list_split require Ruby registration)"
end

.validate_registry_optional_boolean!(document, key) ⇒ Object

Raises:

  • (ArgumentError)


238
239
240
241
242
243
# File 'lib/partition_gardener/config_document.rb', line 238

def validate_registry_optional_boolean!(document, key)
  return unless document.key?(key)
  return if document.fetch(key) == true || document.fetch(key) == false

  raise ArgumentError, "#{key} must be true or false"
end

.validate_registry_optional_bucket!(document) ⇒ Object

Raises:

  • (ArgumentError)


220
221
222
223
224
225
226
227
# File 'lib/partition_gardener/config_document.rb', line 220

def validate_registry_optional_bucket!(document)
  return unless document.key?("bucket")

  bucket = document.fetch("bucket")
  return if bucket.is_a?(String) && %w[day week month quarter year].include?(bucket)

  raise ArgumentError, "bucket must be one of: day, week, month, quarter, year"
end

.validate_registry_optional_maintenance_backend!(document) ⇒ Object

Raises:

  • (ArgumentError)


229
230
231
232
233
234
235
236
# File 'lib/partition_gardener/config_document.rb', line 229

def validate_registry_optional_maintenance_backend!(document)
  return unless document.key?("maintenance_backend")

  backend = document.fetch("maintenance_backend")
  return if backend.is_a?(String) && %w[gardener pg_partman hybrid_layout_only].include?(backend)

  raise ArgumentError, "maintenance_backend must be gardener, pg_partman, or hybrid_layout_only"
end

.validate_registry_required_keys!(document) ⇒ Object

Raises:

  • (ArgumentError)


181
182
183
184
185
186
# File 'lib/partition_gardener/config_document.rb', line 181

def validate_registry_required_keys!(document)
  missing_keys = %w[table_name layout partition_key_column conflict_key] - document.keys
  return if missing_keys.empty?

  raise ArgumentError, "registry entry missing required keys: #{missing_keys.join(", ")}"
end

.validate_registry_unknown_keys!(document) ⇒ Object

Raises:

  • (ArgumentError)


188
189
190
191
192
193
# File 'lib/partition_gardener/config_document.rb', line 188

def validate_registry_unknown_keys!(document)
  unknown_keys = document.keys - ALLOWED_REGISTRY_KEYS
  return if unknown_keys.empty?

  raise ArgumentError, "registry entry has unsupported keys: #{unknown_keys.join(", ")}"
end