pipeforge 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +14 -0
  3. data/LICENSE +21 -0
  4. data/README.md +168 -0
  5. data/guides/matching-columns.md +126 -0
  6. data/guides/measuring-values.md +121 -0
  7. data/guides/running-the-migration.md +145 -0
  8. data/lib/pipeforge/adapter_registry.rb +37 -0
  9. data/lib/pipeforge/adapters/activerecord/adapter.rb +7 -0
  10. data/lib/pipeforge/adapters/activerecord/model_record.rb +202 -0
  11. data/lib/pipeforge/adapters/activerecord/schema_reader.rb +142 -0
  12. data/lib/pipeforge/adapters/activerecord/target.rb +277 -0
  13. data/lib/pipeforge/cast_result.rb +13 -0
  14. data/lib/pipeforge/column_limits_checker.rb +152 -0
  15. data/lib/pipeforge/column_value_analyzer/column_summary_builder.rb +163 -0
  16. data/lib/pipeforge/column_value_analyzer/distinct_values.rb +61 -0
  17. data/lib/pipeforge/column_value_analyzer/listed_values.rb +26 -0
  18. data/lib/pipeforge/column_value_analyzer.rb +96 -0
  19. data/lib/pipeforge/configuration.rb +209 -0
  20. data/lib/pipeforge/error_catalog.rb +184 -0
  21. data/lib/pipeforge/errors.rb +229 -0
  22. data/lib/pipeforge/header_analyzer/matcher.rb +170 -0
  23. data/lib/pipeforge/header_analyzer/pair_scorer.rb +116 -0
  24. data/lib/pipeforge/header_analyzer/tokenizer.rb +43 -0
  25. data/lib/pipeforge/header_analyzer.rb +129 -0
  26. data/lib/pipeforge/mapped_columns.rb +208 -0
  27. data/lib/pipeforge/migration.rb +189 -0
  28. data/lib/pipeforge/resolved_mapped_column.rb +13 -0
  29. data/lib/pipeforge/run_processor/chunk_processor.rb +141 -0
  30. data/lib/pipeforge/run_processor/row_transformer.rb +35 -0
  31. data/lib/pipeforge/run_processor/schema_checker.rb +67 -0
  32. data/lib/pipeforge/run_processor.rb +99 -0
  33. data/lib/pipeforge/source/csv_reader.rb +212 -0
  34. data/lib/pipeforge/source/reusable_io.rb +71 -0
  35. data/lib/pipeforge/target_schema.rb +47 -0
  36. data/lib/pipeforge/utilities.rb +58 -0
  37. data/lib/pipeforge/version.rb +5 -0
  38. data/lib/pipeforge.rb +22 -0
  39. metadata +97 -0
@@ -0,0 +1,209 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "yaml"
4
+
5
+ module Pipeforge
6
+ # The settings of Pipeforge, reached from the module itself: every part reads
7
+ # the ones it needs here, for example +Pipeforge::Configuration.execution.chunk_size+.
8
+ #
9
+ # {.load} reads them from the conventional file, +config/pipeforge.yml+, relative to the
10
+ # directory the program runs in (the root of a Rails application). It runs once, when the gem
11
+ # is required (see pipeforge.rb), so a file that cannot be used stops the application as it
12
+ # boots. Without that file every setting has its default; a setting the file does not mention
13
+ # keeps its default.
14
+ #
15
+ # Each value is checked when the file is read: a whole number of at least 1 for +chunk_size+
16
+ # and +inclusion_threshold+, a whole number from 0 to 100 for +reorder_penalty+, and the name of a known
17
+ # adapter for +adapter+. The adapter is installed when the gem is required, so a later load does not change it.
18
+ #
19
+ # A file that cannot be read at all (not valid YAML, not name: value entries) stops at once.
20
+ # Every other mistake is collected, so one ArgumentError lists them all. When {.load} fails,
21
+ # the settings it had before stay.
22
+ #
23
+ # @example
24
+ # Pipeforge::Configuration.execution.chunk_size # => 2000
25
+ module Configuration
26
+ # Where Pipeforge looks for its settings, by convention.
27
+ DEFAULT_PATH = "config/pipeforge.yml"
28
+
29
+ # The +execution+ section: +chunk_size+ is how many rows are read and processed at a time.
30
+ ExecutionSettings = Data.define(:chunk_size)
31
+
32
+ # The +recommendation+ section: +inclusion_threshold+ is the lowest score (a whole number of
33
+ # at least 1) a pair of columns needs to be a match, and +reorder_penalty+ is how many points
34
+ # (0 to 100) are taken off when the matched words are in a different order.
35
+ RecommendationSettings = Data.define(:inclusion_threshold, :reorder_penalty)
36
+
37
+ # The +target+ section: +adapter+ is the name of the adapter that reaches the target.
38
+ TargetSettings = Data.define(:adapter)
39
+
40
+ # The sections the settings file may have.
41
+ SECTION_NAMES = %w[execution recommendation target].freeze
42
+
43
+ # The value of each setting when the file does not give it.
44
+ DEFAULTS = {
45
+ execution: {chunk_size: 2000},
46
+ recommendation: {inclusion_threshold: 55, reorder_penalty: 10},
47
+ target: {adapter: "activerecord"}
48
+ }.freeze
49
+
50
+ class << self
51
+ # @return [ExecutionSettings]
52
+ attr_reader :execution
53
+
54
+ # @return [RecommendationSettings]
55
+ attr_reader :recommendation
56
+
57
+ # @return [TargetSettings]
58
+ attr_reader :target
59
+
60
+ # Reads {DEFAULT_PATH} and keeps its settings, replacing the ones kept before.
61
+ #
62
+ # @raise [ArgumentError] the file is not valid YAML or is not made of name: value
63
+ # entries (stops at once); or it holds errors in its sections and settings (an unknown
64
+ # section or setting, a section that is not settings, a value of the wrong type or
65
+ # range), all listed in the message
66
+ def load
67
+ @error_messages = []
68
+ @sections_by_name = read_sections
69
+ execution_settings = build_execution
70
+ recommendation_settings = build_recommendation
71
+ target_settings = build_target
72
+ raise_errors
73
+
74
+ @execution = execution_settings
75
+ @recommendation = recommendation_settings
76
+ @target = target_settings
77
+ end
78
+
79
+ private
80
+
81
+ # The sections of the file, which are its top-level entries; empty when the file has none, or when there is
82
+ # no file (every setting then has its default). A section that is not known is
83
+ # recorded as an error, and the rest is still read.
84
+ def read_sections
85
+ return {} unless File.file?(DEFAULT_PATH)
86
+
87
+ sections = YAML.safe_load_file(DEFAULT_PATH) || {}
88
+ unless sections.is_a?(Hash)
89
+ fail_fast(message: ArgumentMessages::MESSAGES.fetch(:settings_not_name_value_entries).call)
90
+ end
91
+
92
+ (sections.keys.map(&:to_s) - SECTION_NAMES).each do |section_name|
93
+ add_error(message: ArgumentMessages::MESSAGES.fetch(:settings_section_unknown).call(section_name: section_name))
94
+ end
95
+
96
+ sections
97
+ rescue Psych::Exception => yaml_error
98
+ fail_fast(message: ArgumentMessages::MESSAGES.fetch(:settings_not_valid_yaml).call(reason: yaml_error.message))
99
+ end
100
+
101
+ # Records a mistake as a sentence.
102
+ def add_error(message:)
103
+ @error_messages << message
104
+ end
105
+
106
+ # Records the mistake and raises at once: nothing after it can be checked.
107
+ def fail_fast(message:)
108
+ add_error(message: message)
109
+ raise_errors
110
+ end
111
+
112
+ def build_execution
113
+ execution_settings = build_section(
114
+ settings_class: ExecutionSettings, section_name: "execution", defaults: DEFAULTS.fetch(:execution)
115
+ )
116
+ require_positive_whole_number(setting_name: "execution.chunk_size", value: execution_settings.chunk_size)
117
+
118
+ execution_settings
119
+ end
120
+
121
+ def build_recommendation
122
+ recommendation_settings = build_section(
123
+ settings_class: RecommendationSettings,
124
+ section_name: "recommendation",
125
+ defaults: DEFAULTS.fetch(:recommendation)
126
+ )
127
+ require_positive_whole_number(
128
+ setting_name: "recommendation.inclusion_threshold", value: recommendation_settings.inclusion_threshold
129
+ )
130
+ require_whole_number_from_0_to_100(
131
+ setting_name: "recommendation.reorder_penalty", value: recommendation_settings.reorder_penalty
132
+ )
133
+
134
+ recommendation_settings
135
+ end
136
+
137
+ def build_target
138
+ target_settings = build_section(
139
+ settings_class: TargetSettings, section_name: "target", defaults: DEFAULTS.fetch(:target)
140
+ )
141
+ require_known_adapter(setting_name: "target.adapter", value: target_settings.adapter)
142
+
143
+ target_settings
144
+ end
145
+
146
+ # The section's settings from the file, over the defaults given. A section that is not
147
+ # settings, or a setting that is not known, is recorded as an error and the rest is still
148
+ # read, so the values that can be checked are checked.
149
+ def build_section(settings_class:, section_name:, defaults:)
150
+ section = @sections_by_name[section_name] || {}
151
+
152
+ unless section.is_a?(Hash)
153
+ add_error(message: ArgumentMessages::MESSAGES.fetch(:settings_section_not_settings).call(section_name: section_name))
154
+ return settings_class.new(**defaults)
155
+ end
156
+
157
+ given_settings = section.transform_keys(&:to_sym)
158
+
159
+ (given_settings.keys - settings_class.members).each do |setting_name|
160
+ add_error(
161
+ message: ArgumentMessages::MESSAGES.fetch(:settings_setting_unknown).call(
162
+ setting_name: setting_name, section_name: section_name
163
+ )
164
+ )
165
+ end
166
+
167
+ settings_class.new(**defaults.merge(given_settings.slice(*settings_class.members)))
168
+ end
169
+
170
+ # A whole number (not a decimal, text, true or nothing) of at least 1.
171
+ def require_positive_whole_number(setting_name:, value:)
172
+ return if Utilities.positive_whole_number?(value: value)
173
+
174
+ add_error(
175
+ message: ArgumentMessages::MESSAGES.fetch(:value_not_whole_number_at_least_1).call(name: setting_name, value: value)
176
+ )
177
+ end
178
+
179
+ # A whole number from 0 to 100, for a score or a number of points.
180
+ def require_whole_number_from_0_to_100(setting_name:, value:)
181
+ return if value.is_a?(Integer) && value.between?(0, 100)
182
+
183
+ add_error(
184
+ message: ArgumentMessages::MESSAGES.fetch(:value_not_whole_number_from_0_to_100).call(name: setting_name, value: value)
185
+ )
186
+ end
187
+
188
+ # The name of an adapter of the registry.
189
+ def require_known_adapter(setting_name:, value:)
190
+ return if AdapterRegistry.names.include?(value)
191
+
192
+ add_error(
193
+ message: ArgumentMessages::MESSAGES.fetch(:value_not_a_known_adapter).call(
194
+ name: setting_name, value: value, known_names: AdapterRegistry.names
195
+ )
196
+ )
197
+ end
198
+
199
+ # One ArgumentError for everything collected: the sentence itself for one, a list for several.
200
+ def raise_errors
201
+ return if @error_messages.empty?
202
+
203
+ raise ArgumentError, ArgumentMessages::MESSAGES.fetch(:settings_file_has_errors).call(
204
+ path: DEFAULT_PATH, error_messages: @error_messages
205
+ )
206
+ end
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,184 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pipeforge
4
+ # The kinds of error of a row, each written once. {ERRORS} is keyed by the code and its kind joined
5
+ # (+:value_out_of_range_bytes+, or the code alone when it has no kind). The value of a key is a function that takes
6
+ # +origin:+ (where the mistake was found), +target_column_name:+, the failed +value+ (in JSON types, or nil when there
7
+ # is none) and the facts its sentence uses, and returns the error as a {ColumnError}.
8
+ #
9
+ # Only these functions can make a {ColumnError}: the class cannot be named or built outside the module, so
10
+ # {.error?} is how another class asks whether an object is one. A failed row holds the error objects, and
11
+ # +FailedRow#to_h+ gives each of them as the Hash that +ColumnError#to_h+ gives, with the seven keys.
12
+ #
13
+ # A function lists every fact it accepts as a keyword, so a fact it does not know is refused. The kinds that the
14
+ # database can also report take +native_message+ as an optional fact.
15
+ #
16
+ # The message is always ours, never the text of the model or of the database. That text, when there is one, is the
17
+ # fact +native_message+, and a few sentences end with it in brackets.
18
+ #
19
+ # @example
20
+ # error = Pipeforge::ErrorCatalog::ERRORS[:value_too_long].call(
21
+ # origin: :schema, target_column_name: "status", value: "x" * 34, length: 34, max_length: 20
22
+ # )
23
+ # error.to_h # => {origin: :schema, code: :value_too_long, kind: nil, target_column_name: "status",
24
+ # # message: "status: the value has 34 characters, more than the 20 allowed",
25
+ # # details: {length: 34, max_length: 20}, value: "xxxxxxxx..."}
26
+ module ErrorCatalog
27
+ # One error of a row, as a function of {ERRORS} builds it.
28
+ # origin where the mistake was found: +:schema+, +:model+ or +:database+
29
+ # code the stable code of the mistake
30
+ # kind which case of the code, or nil
31
+ # target_column_name the column it is about; nil for the record as a whole
32
+ # message what is wrong, always ours, with the column first
33
+ # details the facts of the mistake, as a frozen Hash
34
+ # value the value that failed, in JSON types; nil when there is none
35
+ ColumnError = Data.define(:origin, :code, :kind, :target_column_name, :message, :details, :value) do
36
+ private_class_method :new
37
+
38
+ # The one way to make an error, used by {ErrorCatalog.error}.
39
+ #
40
+ # @return [ColumnError]
41
+ def self.build(origin:, code:, kind:, target_column_name:, message:, details:, value:)
42
+ new(
43
+ origin: origin, code: code, kind: kind, target_column_name: target_column_name, message: message,
44
+ details: details.freeze, value: value
45
+ )
46
+ end
47
+ end
48
+ private_constant :ColumnError
49
+
50
+ ERRORS = {
51
+ value_missing: ->(origin:, target_column_name:, value:, native_message: nil) {
52
+ ErrorCatalog.error(
53
+ code: :value_missing, kind: nil, sentence: "the value is required but empty",
54
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}.compact
55
+ )
56
+ },
57
+ parent_missing: ->(origin:, target_column_name:, value:, native_message: nil) {
58
+ ErrorCatalog.error(
59
+ code: :parent_missing, kind: nil, sentence: "the parent row does not exist",
60
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}.compact
61
+ )
62
+ },
63
+ value_not_unique: ->(origin:, target_column_name:, value:, native_message: nil) {
64
+ ErrorCatalog.error(
65
+ code: :value_not_unique, kind: nil, sentence: "a row with the same value already exists",
66
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}.compact
67
+ )
68
+ },
69
+ # The most specific sentence the given facts allow.
70
+ value_too_long: ->(origin:, target_column_name:, value:, length: nil, max_length: nil, native_message: nil) {
71
+ sentence = if !length.nil? && !max_length.nil?
72
+ "the value has #{length} characters, more than the #{max_length} allowed"
73
+ elsif !max_length.nil?
74
+ "the value is longer than the #{max_length} characters allowed"
75
+ else
76
+ "the value is longer than its column allows"
77
+ end
78
+
79
+ ErrorCatalog.error(
80
+ code: :value_too_long, kind: nil, sentence: sentence, origin: origin, target_column_name: target_column_name, value: value,
81
+ details: {length: length, max_length: max_length, native_message: native_message}.compact
82
+ )
83
+ },
84
+ value_out_of_range_bytes: ->(origin:, target_column_name:, value:, bytes:) {
85
+ ErrorCatalog.error(
86
+ code: :value_out_of_range, kind: :bytes, sentence: "the value does not fit in #{bytes} bytes",
87
+ origin: origin, target_column_name: target_column_name, value: value, details: {bytes: bytes}
88
+ )
89
+ },
90
+ value_out_of_range_digits: ->(origin:, target_column_name:, value:, allowed_digits:) {
91
+ ErrorCatalog.error(
92
+ code: :value_out_of_range, kind: :digits,
93
+ sentence: "the value has more than #{allowed_digits} digits before the point",
94
+ origin: origin, target_column_name: target_column_name, value: value, details: {allowed_digits: allowed_digits}
95
+ )
96
+ },
97
+ value_out_of_range_float: ->(origin:, target_column_name:, value:) {
98
+ ErrorCatalog.error(
99
+ code: :value_out_of_range, kind: :float,
100
+ sentence: "the value is larger than a float of this column can hold",
101
+ origin: origin, target_column_name: target_column_name, value: value, details: {}
102
+ )
103
+ },
104
+ value_out_of_range: ->(origin:, target_column_name:, value:, native_message: nil) {
105
+ ErrorCatalog.error(
106
+ code: :value_out_of_range, kind: nil, sentence: "the value is outside what its column holds",
107
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}.compact
108
+ )
109
+ },
110
+ value_not_allowed: ->(origin:, target_column_name:, value:, labels:) {
111
+ ErrorCatalog.error(
112
+ code: :value_not_allowed, kind: nil, sentence: "the value is not one of #{labels.join(", ")}",
113
+ origin: origin, target_column_name: target_column_name, value: value, details: {labels: labels}
114
+ )
115
+ },
116
+ value_not_accepted_not_castable: ->(origin:, target_column_name:, value:) {
117
+ ErrorCatalog.error(
118
+ code: :value_not_accepted, kind: :not_castable, sentence: "the value cannot be given to the model",
119
+ origin: origin, target_column_name: target_column_name, value: value, details: {}
120
+ )
121
+ },
122
+ value_not_accepted_enum_label: ->(origin:, target_column_name:, value:) {
123
+ ErrorCatalog.error(
124
+ code: :value_not_accepted, kind: :enum_label, sentence: "the value is not a label of the enum",
125
+ origin: origin, target_column_name: target_column_name, value: value, details: {}
126
+ )
127
+ },
128
+ value_not_accepted_polymorphic_type: ->(origin:, target_column_name:, value:) {
129
+ ErrorCatalog.error(
130
+ code: :value_not_accepted, kind: :polymorphic_type,
131
+ sentence: "the value is not a model of the application",
132
+ origin: origin, target_column_name: target_column_name, value: value, details: {}
133
+ )
134
+ },
135
+ value_invalid: ->(origin:, target_column_name:, value:, native_message:, error_type: nil) {
136
+ ErrorCatalog.error(
137
+ code: :value_invalid, kind: nil, sentence: "the model does not accept the value (#{native_message})",
138
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message, error_type: error_type}.compact
139
+ )
140
+ },
141
+ value_invalid_save_stopped: ->(origin:, target_column_name:, value:, native_message:) {
142
+ ErrorCatalog.error(
143
+ code: :value_invalid, kind: :save_stopped,
144
+ sentence: "a callback of the model stopped the save (#{native_message})",
145
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}
146
+ )
147
+ },
148
+ database_error: ->(origin:, target_column_name:, value:, native_message:) {
149
+ ErrorCatalog.error(
150
+ code: :database_error, kind: nil, sentence: "the database refused the row (#{native_message})",
151
+ origin: origin, target_column_name: target_column_name, value: value, details: {native_message: native_message}
152
+ )
153
+ }
154
+ }.freeze
155
+
156
+ class << self
157
+ # The error with its message; the one place that joins the column and the sentence.
158
+ #
159
+ # @param origin [Symbol] where the mistake was found: +:schema+, +:model+ or +:database+
160
+ # @param code [Symbol] the stable code of the mistake
161
+ # @param kind [Symbol, nil] which case of the code, or nil
162
+ # @param sentence [String] what is wrong, without the column
163
+ # @param target_column_name [String, nil] the column it is about; nil for the record as a whole
164
+ # @param value [Object, nil] the value that failed, in JSON types; nil when there is none
165
+ # @param details [Hash] the facts of the mistake
166
+ # @return [ColumnError]
167
+ def error(origin:, code:, kind:, sentence:, target_column_name:, value:, details:)
168
+ message = sentence
169
+ message = "#{target_column_name}: #{sentence}" unless target_column_name.nil?
170
+
171
+ ColumnError.build(
172
+ origin: origin, code: code, kind: kind, target_column_name: target_column_name, message: message,
173
+ details: details, value: value
174
+ )
175
+ end
176
+
177
+ # @param candidate [Object] anything
178
+ # @return [Boolean] true when the object is an error built by this module
179
+ def error?(candidate:)
180
+ candidate.is_a?(ColumnError)
181
+ end
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,229 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pipeforge
4
+ # The base class of every error Pipeforge raises on purpose.
5
+ #
6
+ # An error holds +errors+: a list with one entry per mistake. Each entry has a stable +code+
7
+ # (a Symbol that a program can check), a +message+ (the sentence for a person, which may be
8
+ # reworded) and the fields that say where the mistake is (a setting, a column, a line).
9
+ # Several mistakes found in one pass are raised together as one error.
10
+ #
11
+ # @example
12
+ # begin
13
+ # migration.analyze_column_values(mapped_columns: mapped_columns)
14
+ # rescue Pipeforge::InvalidMappedColumnsError => error
15
+ # error.codes # => [:mapped_column_not_in_file, :required_column_unmapped]
16
+ # error.errors # => [{code: :mapped_column_not_in_file, message: "...", target_column_name: "email", source_column_name: "Mail"}, ...]
17
+ # end
18
+ class Error < StandardError
19
+ # @return [Array<Hash>] one entry per mistake: +code+, +message+ and the fields that locate it
20
+ attr_reader :errors
21
+
22
+ # An error for one mistake.
23
+ #
24
+ # @param code [Symbol] the stable name of the mistake
25
+ # @param message [String] the sentence for a person
26
+ # @param context [Hash] the fields that say where the mistake is
27
+ # @return [Error]
28
+ def self.single(code:, message:, **context)
29
+ new(errors: [{code: code, message: message, **context}])
30
+ end
31
+
32
+ # The message of the whole error is the messages of its entries, joined with "; ", so it can
33
+ # never differ from them. Give +message+ only when the whole error needs its own layout.
34
+ #
35
+ # @param errors [Array<Hash>] one entry per mistake, each with +code+ and +message+
36
+ # @param message [String, nil] the sentence for the whole error, instead of the joined entries
37
+ def initialize(errors:, message: nil)
38
+ if message.nil?
39
+ message = errors.map { |entry| entry[:message] }.join("; ")
40
+ end
41
+
42
+ super(message)
43
+ @errors = errors
44
+ end
45
+
46
+ # @return [Array<Symbol>] the code of each entry, in order
47
+ def codes
48
+ errors.map { |entry| entry[:code] }
49
+ end
50
+ end
51
+
52
+ # Raised when the mapped columns given to {Migration#analyze_column_values} cannot be used (checked by
53
+ # {MappedColumns}) because they do not fit the table or the file. Codes:
54
+ # +target_column_not_in_schema+ (the target column is not a column of the table that can be loaded),
55
+ # +column_missing_source_column+, +mapped_column_not_in_file+ and +required_column_unmapped+ (a required target
56
+ # column has no entry), one entry for each mistake. A mapping with the wrong shape is an ArgumentError instead.
57
+ class InvalidMappedColumnsError < Error
58
+ # The entries of the error, each written once and keyed by its code. The value of a key is a function that takes the
59
+ # facts its sentence uses and returns the entry: the +code+, the +message+ (with the column first) and the fields
60
+ # that locate the mistake.
61
+ ENTRIES = {
62
+ target_column_not_in_schema: ->(target_column_name:) {
63
+ {
64
+ code: :target_column_not_in_schema,
65
+ message: "#{target_column_name} is not a column of the target table that can be loaded",
66
+ target_column_name: target_column_name
67
+ }
68
+ },
69
+ column_missing_source_column: ->(target_column_name:) {
70
+ {
71
+ code: :column_missing_source_column,
72
+ message: "#{target_column_name} has no source_column_name",
73
+ target_column_name: target_column_name
74
+ }
75
+ },
76
+ mapped_column_not_in_file: ->(target_column_name:, source_column_name:) {
77
+ {
78
+ code: :mapped_column_not_in_file,
79
+ message: "#{target_column_name} is mapped to the source column #{source_column_name}, which is not in the file",
80
+ target_column_name: target_column_name,
81
+ source_column_name: source_column_name
82
+ }
83
+ },
84
+ required_column_unmapped: ->(target_column_name:) {
85
+ {
86
+ code: :required_column_unmapped,
87
+ message: "#{target_column_name} is required but no source column is mapped to it",
88
+ target_column_name: target_column_name
89
+ }
90
+ }
91
+ }.freeze
92
+ end
93
+
94
+ # Raised by a target that cannot read the schema of the target. A model that is not found is +model_not_found+
95
+ # (field +model_name+) and a table that does not exist is +target_table_not_found+ (field +table_name+). A code that
96
+ # belongs to one adapter carries its name.
97
+ class TargetSchemaError < Error
98
+ # The entries of the error, each written once and keyed by its code. The value of a key is a function that takes the
99
+ # facts its sentence uses and returns the entry: the +code+, the +message+ and the fields that locate the mistake.
100
+ ENTRIES = {
101
+ model_not_found: ->(target_model_name:) {
102
+ {
103
+ code: :model_not_found,
104
+ message: "no loaded model is named #{target_model_name}; the model must be loaded before the schema is read",
105
+ model_name: target_model_name
106
+ }
107
+ },
108
+ target_table_not_found: ->(table_name:, model_name:) {
109
+ {
110
+ code: :target_table_not_found,
111
+ message: "table #{table_name} of #{model_name} does not exist in the database",
112
+ table_name: table_name
113
+ }
114
+ }
115
+ }.freeze
116
+ end
117
+
118
+ # The errors about the source file: the file itself, its header and its rows.
119
+ module Source
120
+ # Raised when the source file cannot be used. One entry for each mistake, told apart by its code:
121
+ # +source_empty+ (no header line to read; a file with a header and no data rows is not an error),
122
+ # +source_header_blank+ (a header cell is blank, with its +position+), +source_header_duplicated+ (a name
123
+ # appears twice, with the +source_column_name+), +malformed_row+ (a row cannot be parsed, for example a quote
124
+ # that is never closed) and +source_row_too_long+ (a row has more cells than the header). The two row codes
125
+ # have the +line+ where the row starts.
126
+ class Error < Pipeforge::Error
127
+ # The entries of the error, each written once and keyed by its code. The value of a key is a function that takes
128
+ # the facts its sentence uses and returns the entry: the +code+, the +message+ and the fields that locate the
129
+ # mistake.
130
+ ENTRIES = {
131
+ source_empty: -> {
132
+ {code: :source_empty, message: "the source has no header row to read"}
133
+ },
134
+ source_header_blank: ->(position:) {
135
+ {code: :source_header_blank, message: "header cell #{position} is blank", position: position}
136
+ },
137
+ source_header_duplicated: ->(source_column_name:, count:) {
138
+ {
139
+ code: :source_header_duplicated,
140
+ message: "column name #{source_column_name} appears #{count} times",
141
+ source_column_name: source_column_name
142
+ }
143
+ },
144
+ malformed_row: ->(line:, reason:) {
145
+ {code: :malformed_row, message: "malformed CSV at line #{line}: #{reason}", line: line}
146
+ },
147
+ source_row_too_long: ->(line:) {
148
+ {
149
+ code: :source_row_too_long,
150
+ message: "the row at line #{line} has more cells than the header",
151
+ line: line
152
+ }
153
+ }
154
+ }.freeze
155
+ end
156
+ end
157
+
158
+ # The sentences of the ArgumentError that a wrong call or a wrong configuration file raises. A plain ArgumentError has
159
+ # no code, so each sentence is written once, keyed by a name, and is a function of the facts it uses that gives the
160
+ # message as a String.
161
+ module ArgumentMessages
162
+ MESSAGES = {
163
+ mode_not_allowed: ->(mode:) {
164
+ "mode must be :dry_run or :run, got #{mode.inspect}"
165
+ },
166
+ value_not_whole_number_at_least_0: ->(name:, value:) {
167
+ "#{name} must be a whole number of at least 0, got #{value.inspect}"
168
+ },
169
+ value_not_whole_number_at_least_1: ->(name:, value:) {
170
+ "#{name} must be a whole number of at least 1, got #{value.inspect}"
171
+ },
172
+ value_not_whole_number_from_0_to_100: ->(name:, value:) {
173
+ "#{name} must be a whole number from 0 to 100, got #{value.inspect}"
174
+ },
175
+ source_missing_methods: ->(required_method_names:, missing_method_names:) {
176
+ "the source must respond to #{required_method_names.join(", ")}; " \
177
+ "it does not respond to #{missing_method_names.join(", ")}"
178
+ },
179
+ error_not_from_error_catalog: ->(given_class:) {
180
+ "add_error takes an error built by ErrorCatalog (ErrorCatalog::ERRORS.fetch(key).call(...)), not #{given_class}"
181
+ },
182
+ adapter_not_loaded: ->(name:, reason:) {
183
+ "cannot load the #{name} adapter of Pipeforge. Missing a gem it depends on? #{reason}"
184
+ },
185
+ value_not_a_known_adapter: ->(name:, value:, known_names:) {
186
+ "#{name} must be one of #{known_names.join(", ")}, got #{value.inspect}"
187
+ },
188
+ mapped_columns_not_a_hash: ->(given_class:) {
189
+ "mapped_columns must be a Hash of target column name => entry, got #{given_class}"
190
+ },
191
+ mapped_column_name_not_symbol_or_string: ->(target_column_name:) {
192
+ "the key #{target_column_name.inspect} of mapped_columns must be a Symbol or a String"
193
+ },
194
+ mapped_column_entry_not_a_hash: ->(target_column_name:, given_class:) {
195
+ "#{target_column_name} must be given as {source_column_name: ...}, got #{given_class}"
196
+ },
197
+ value_mapping_not_a_hash: ->(target_column_name:, given_class:) {
198
+ "the value_mapping of #{target_column_name} must be a Hash of value in the file => value to load, " \
199
+ "got #{given_class}"
200
+ },
201
+ mapped_column_unknown_key: ->(target_column_name:, key:, entry_keys_text:) {
202
+ "#{target_column_name} has the unknown key #{key.inspect}; the keys are #{entry_keys_text}"
203
+ },
204
+ settings_not_name_value_entries: -> {
205
+ "must be made of name: value entries at the top level"
206
+ },
207
+ settings_not_valid_yaml: ->(reason:) {
208
+ "not valid YAML: #{reason}"
209
+ },
210
+ settings_section_unknown: ->(section_name:) {
211
+ "unknown section #{section_name}"
212
+ },
213
+ settings_section_not_settings: ->(section_name:) {
214
+ "section #{section_name} must hold settings, each written as name: value"
215
+ },
216
+ settings_setting_unknown: ->(setting_name:, section_name:) {
217
+ "unknown setting #{setting_name} in section #{section_name}"
218
+ },
219
+ settings_file_has_errors: ->(path:, error_messages:) {
220
+ if error_messages.size == 1
221
+ "#{path}: #{error_messages.first}"
222
+ else
223
+ lines = error_messages.map { |error_message| " - #{error_message}" }
224
+ "#{path} has #{error_messages.size} errors:\n#{lines.join("\n")}"
225
+ end
226
+ }
227
+ }.freeze
228
+ end
229
+ end