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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +14 -0
- data/LICENSE +21 -0
- data/README.md +168 -0
- data/guides/matching-columns.md +126 -0
- data/guides/measuring-values.md +121 -0
- data/guides/running-the-migration.md +145 -0
- data/lib/pipeforge/adapter_registry.rb +37 -0
- data/lib/pipeforge/adapters/activerecord/adapter.rb +7 -0
- data/lib/pipeforge/adapters/activerecord/model_record.rb +202 -0
- data/lib/pipeforge/adapters/activerecord/schema_reader.rb +142 -0
- data/lib/pipeforge/adapters/activerecord/target.rb +277 -0
- data/lib/pipeforge/cast_result.rb +13 -0
- data/lib/pipeforge/column_limits_checker.rb +152 -0
- data/lib/pipeforge/column_value_analyzer/column_summary_builder.rb +163 -0
- data/lib/pipeforge/column_value_analyzer/distinct_values.rb +61 -0
- data/lib/pipeforge/column_value_analyzer/listed_values.rb +26 -0
- data/lib/pipeforge/column_value_analyzer.rb +96 -0
- data/lib/pipeforge/configuration.rb +209 -0
- data/lib/pipeforge/error_catalog.rb +184 -0
- data/lib/pipeforge/errors.rb +229 -0
- data/lib/pipeforge/header_analyzer/matcher.rb +170 -0
- data/lib/pipeforge/header_analyzer/pair_scorer.rb +116 -0
- data/lib/pipeforge/header_analyzer/tokenizer.rb +43 -0
- data/lib/pipeforge/header_analyzer.rb +129 -0
- data/lib/pipeforge/mapped_columns.rb +208 -0
- data/lib/pipeforge/migration.rb +189 -0
- data/lib/pipeforge/resolved_mapped_column.rb +13 -0
- data/lib/pipeforge/run_processor/chunk_processor.rb +141 -0
- data/lib/pipeforge/run_processor/row_transformer.rb +35 -0
- data/lib/pipeforge/run_processor/schema_checker.rb +67 -0
- data/lib/pipeforge/run_processor.rb +99 -0
- data/lib/pipeforge/source/csv_reader.rb +212 -0
- data/lib/pipeforge/source/reusable_io.rb +71 -0
- data/lib/pipeforge/target_schema.rb +47 -0
- data/lib/pipeforge/utilities.rb +58 -0
- data/lib/pipeforge/version.rb +5 -0
- data/lib/pipeforge.rb +22 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4728a1c1250a780a7958678e262d45cee38994cbe50138cec80c06c6179058f7
|
|
4
|
+
data.tar.gz: 27753fdcc71026f9b83316ff5301e289b3201cf5af7c2ffadd9b1e88492e1278
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 2ae4548975c9a7dda5c532574e7d213e07dfab0c7ca63692618ddeab7f2cee9238e2d34805d82b944cf6b6bb778666fdff92c2e9160a48ee249f316ddf0ee8e6
|
|
7
|
+
data.tar.gz: bf1e52437d28062a546035333d9fa1dfb2f4e116aa4556b6ce3895ff2d6a6f9dfb8ff12fbdf245b2760e58ac9ce3d7a6bd59ddddeaa87e66502659aa87ba1e68
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
The versions follow [Semantic Versioning](https://semver.org). While the version starts with 0, a new minor version may change
|
|
4
|
+
the interface.
|
|
5
|
+
|
|
6
|
+
## 0.1.0 (unreleased)
|
|
7
|
+
|
|
8
|
+
The first release.
|
|
9
|
+
|
|
10
|
+
- Match the columns of a CSV file to the columns of a model's table, by name.
|
|
11
|
+
- Measure the values of each mapped column against the table: empty, compatible and incompatible values, with a value mapping.
|
|
12
|
+
- Load the rows through the application's model, as a dry run or a run, in chunks, with the failed rows and their error codes.
|
|
13
|
+
- Settings in `config/pipeforge.yml`.
|
|
14
|
+
- An adapter for ActiveRecord 8, on PostgreSQL.
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 venky
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# Pipeforge
|
|
2
|
+
|
|
3
|
+
Pipeforge loads a CSV file into the table of a model in your Ruby application, and tells you what will happen before it writes a row. It matches the file's columns to the table's columns, lists the values that will not fit, and runs the whole load as a rehearsal first.
|
|
4
|
+
|
|
5
|
+
It is a library that you call. It has no screens and runs nothing by itself: each answer comes back as plain data, and you decide what to do with it.
|
|
6
|
+
|
|
7
|
+
Use it when someone hands you an export from another system, and you need to know which rows will fail, and why, before they reach your database.
|
|
8
|
+
|
|
9
|
+
## What you get
|
|
10
|
+
|
|
11
|
+
| Your question | The call | The guide |
|
|
12
|
+
|---|---|---|
|
|
13
|
+
| Which column of the file goes where? | `analyze_headers` | [Matching columns](guides/matching-columns.md) |
|
|
14
|
+
| Which values will not fit, and which need translating? | `analyze_column_values` | [Measuring values](guides/measuring-values.md) |
|
|
15
|
+
| Which rows will fail, and why? Then load them. | `run` | [Running the migration](guides/running-the-migration.md) |
|
|
16
|
+
|
|
17
|
+
Your model stays in charge. Rows go through its validations and callbacks, and its way of casting values, so a dry run fails in the same places a real run would. Pipeforge adds the checks of the table itself, so a value that is too long is reported even when no validation mentions it.
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
|
|
21
|
+
You need Ruby 3.3 or later, ActiveRecord 8 and PostgreSQL. Add the gem to your `Gemfile`:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
gem "pipeforge", "~> 0.1.0"
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Pipeforge does not install ActiveRecord or the `pg` driver. A Rails application on PostgreSQL has both; elsewhere, add them yourself. Bundler requires the gem for you; without Bundler, write `require "pipeforge"`.
|
|
28
|
+
|
|
29
|
+
Your database connection is the one ActiveRecord already uses. Pipeforge never asks for credentials.
|
|
30
|
+
|
|
31
|
+
## Settings
|
|
32
|
+
|
|
33
|
+
Behaviour that stays the same across migrations goes in `config/pipeforge.yml`. The file is optional, and a setting you leave out keeps its default. These are the defaults:
|
|
34
|
+
|
|
35
|
+
```yaml
|
|
36
|
+
execution:
|
|
37
|
+
chunk_size: 2000 # rows read, checked and given to your block at a time
|
|
38
|
+
recommendation:
|
|
39
|
+
inclusion_threshold: 55 # the lowest score a pair of columns needs to be a match
|
|
40
|
+
reorder_penalty: 10 # points taken off (0 to 100) when matched words are in another order
|
|
41
|
+
target:
|
|
42
|
+
adapter: activerecord # the only adapter so far
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The `recommendation` settings decide which columns are [matched](guides/matching-columns.md#how-a-score-is-worked-out). The file is read once, when the gem is required, from the directory the program runs in. A file with a mistake stops the application at boot with one `ArgumentError` that lists every mistake.
|
|
46
|
+
|
|
47
|
+
## Words used below
|
|
48
|
+
|
|
49
|
+
- Migration: here, moving the data of a file into a table, done by a `Pipeforge::Migration`. It is not a Rails database migration, which changes the structure of the database.
|
|
50
|
+
- Target model: the model the rows are loaded through, given by its name, for example `"Employee"`. Its table is the target table.
|
|
51
|
+
- Source column: a column of your CSV file, named by its header.
|
|
52
|
+
- Mapped columns: a Hash you write that says which source column fills which column of the table.
|
|
53
|
+
- Value mapping: part of one entry of the mapped columns. It rewrites values of the file before they are loaded, for example `"Left"` to `"inactive"`.
|
|
54
|
+
- Dry run: the same checks and writes as a real run, inside one transaction that is rolled back at the end.
|
|
55
|
+
- Failed row: a row that was not loaded, with its row number and the reason for each mistake.
|
|
56
|
+
|
|
57
|
+
## A migration, start to finish
|
|
58
|
+
|
|
59
|
+
An old HR system exports `employees.csv`:
|
|
60
|
+
|
|
61
|
+
```csv
|
|
62
|
+
Email,Full Name,Status,Salary,Date of Joining,Department Id
|
|
63
|
+
priya@example.com,Priya Raman,Working,85000.00,2021-04-12,1
|
|
64
|
+
arjun@example.com,Arjun Mehta,On Leave,N/A,2022-01-03,1
|
|
65
|
+
meena@example.com,Meena Iyer,Left,72000.00,2019-07-22,1
|
|
66
|
+
ravi@example.com,Ravi Kumar,Working,8500000.00,2020-11-02,1
|
|
67
|
+
priya@example.com,Priya Raman,Working,85000.00,2021-04-12,1
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
Your application has this model. Its table has `salary` as `decimal(8, 2)`, and a `Department` with id 1 exists.
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
class Employee < ActiveRecord::Base
|
|
74
|
+
belongs_to :department, optional: true
|
|
75
|
+
enum :status, {active: 0, on_leave: 1, inactive: 2}
|
|
76
|
+
validates :email, presence: true, uniqueness: true
|
|
77
|
+
end
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Pipeforge looks for the model among the classes already loaded. Rails in development loads a model the first time your code refers to it, so refer to `Employee` before you create the migration.
|
|
81
|
+
|
|
82
|
+
Match the columns:
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
migration = Pipeforge::Migration.new(source_io: File.open("employees.csv"), target_model_name: "Employee")
|
|
86
|
+
|
|
87
|
+
header_analysis = migration.analyze_headers
|
|
88
|
+
header_analysis.matches.each { |match| puts "#{match[:target_column_name]} <- #{match[:source_column_name]}" }
|
|
89
|
+
# email <- Email
|
|
90
|
+
# full_name <- Full Name
|
|
91
|
+
# status <- Status
|
|
92
|
+
# salary <- Salary
|
|
93
|
+
# department_id <- Department Id
|
|
94
|
+
|
|
95
|
+
header_analysis.unmatched_source_column_names # => ["Date of Joining"]
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Write the mapped columns, adding the one that was not matched, and measure the values:
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
mapped_columns = {
|
|
102
|
+
email: {source_column_name: "Email"},
|
|
103
|
+
full_name: {source_column_name: "Full Name"},
|
|
104
|
+
status: {source_column_name: "Status"},
|
|
105
|
+
salary: {source_column_name: "Salary"},
|
|
106
|
+
joined_on: {source_column_name: "Date of Joining"},
|
|
107
|
+
department_id: {source_column_name: "Department Id"}
|
|
108
|
+
}
|
|
109
|
+
analysis = migration.analyze_column_values(mapped_columns: mapped_columns)
|
|
110
|
+
|
|
111
|
+
status = analysis.column_summaries["status"].distinct_values
|
|
112
|
+
status.incompatible.map(&:value) # => ["Working", "On Leave", "Left"]
|
|
113
|
+
|
|
114
|
+
salary = analysis.column_summaries["salary"].distinct_values
|
|
115
|
+
salary.incompatible.map(&:value) # => ["8500000.00"]
|
|
116
|
+
salary.compatible.map { |listed| [listed.value, listed.casted_to] }
|
|
117
|
+
# => [["85000.00", "85000.0"], ["N/A", "0.0"], ["72000.00", "72000.0"]]
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Look at the last line. ActiveRecord turns `N/A` in a number column into `0.0`, so it counts as fitting and would give Arjun a salary of zero. `casted_to` is how you see it.
|
|
121
|
+
|
|
122
|
+
Map the old words to your own:
|
|
123
|
+
|
|
124
|
+
```ruby
|
|
125
|
+
mapped_columns[:status][:value_mapping] = {"Working" => "active", "On Leave" => "on_leave", "Left" => "inactive"}
|
|
126
|
+
mapped_columns[:salary][:value_mapping] = {"N/A" => nil}
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Rehearse, then run:
|
|
130
|
+
|
|
131
|
+
```ruby
|
|
132
|
+
totals = migration.run(mode: :dry_run, mapped_columns: mapped_columns) do |chunk_result|
|
|
133
|
+
chunk_result.failed_rows.each do |failed_row|
|
|
134
|
+
puts "row #{failed_row.row_number}: #{failed_row.errors.map(&:code).inspect}"
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
# row 4: [:value_out_of_range]
|
|
138
|
+
# row 5: [:value_not_unique]
|
|
139
|
+
|
|
140
|
+
totals
|
|
141
|
+
# => #<data Pipeforge::RunProcessor::RunResult processed_rows_count=5, valid_rows_count=3, failed_rows_count=2>
|
|
142
|
+
Employee.count # => 0
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
Row 4 has a salary too large for the column, and row 5 is the same person as row 1. Change the mode to `:run` and you get the same two failed rows, and `Employee.count` is 3.
|
|
146
|
+
|
|
147
|
+
## Limits
|
|
148
|
+
|
|
149
|
+
- One run loads one model. Rows are inserted; nothing is updated.
|
|
150
|
+
- The source is a CSV file and the database is PostgreSQL.
|
|
151
|
+
- Rows are written one at a time. This gives exact row numbers, and is slower than loading in batches, which is not built.
|
|
152
|
+
- The primary key is filled by the database, so you cannot keep the ids of the old system by mapping them.
|
|
153
|
+
- Array columns, generated columns and columns the model ignores are not offered. A composite foreign key is loaded like any other column, without being described as a foreign key.
|
|
154
|
+
- Parent rows are never looked up by name. If the file says `Engineering` and the table needs an id, map the value yourself.
|
|
155
|
+
|
|
156
|
+
The two analysis steps read only the file and the structure of your model. They read no rows of your tables.
|
|
157
|
+
|
|
158
|
+
## More
|
|
159
|
+
|
|
160
|
+
- [Changelog](CHANGELOG.md)
|
|
161
|
+
- [How column names are matched](https://github.com/venkydev02/Pipeforge/blob/v0.1.0/docs/architecture/recommendation-name-matching.md)
|
|
162
|
+
- [Architecture overview](https://github.com/venkydev02/Pipeforge/blob/v0.1.0/docs/architecture/overview.md)
|
|
163
|
+
- [Design decisions and their reasons](https://github.com/venkydev02/Pipeforge/blob/v0.1.0/docs/decisions/adrs.md)
|
|
164
|
+
- [Contributing](https://github.com/venkydev02/Pipeforge/blob/v0.1.0/CONTRIBUTING.md)
|
|
165
|
+
|
|
166
|
+
## License
|
|
167
|
+
|
|
168
|
+
MIT. See [LICENSE](LICENSE).
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
# Matching columns: `analyze_headers`
|
|
2
|
+
|
|
3
|
+
This step answers "which column of the file goes into which column of my table?" It compares names only.
|
|
4
|
+
|
|
5
|
+
## Terms
|
|
6
|
+
|
|
7
|
+
- Source column: a column of the CSV file, named by its header.
|
|
8
|
+
- Target column: a column of the target model's table that can be loaded.
|
|
9
|
+
- Score: a number from 0 to 100 for how alike two names are.
|
|
10
|
+
- Match: a target column and the source column that scored best for it.
|
|
11
|
+
|
|
12
|
+
## Starting a migration
|
|
13
|
+
|
|
14
|
+
Every step starts from a `Pipeforge::Migration`, created for one file and one model:
|
|
15
|
+
|
|
16
|
+
```ruby
|
|
17
|
+
migration = Pipeforge::Migration.new(source_io: File.open("employees.csv"), target_model_name: "Employee")
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`source_io` is an open file, or any object with `read`, `rewind` and `seek`. Creating it reads the header of the file and the structure of the model's table, once. It reads no row of your table.
|
|
21
|
+
|
|
22
|
+
What you can rely on:
|
|
23
|
+
|
|
24
|
+
- A file that cannot be used stops here, before the database is asked anything: no header (`source_empty`), a blank header cell (`source_header_blank`) or a name used twice (`source_header_duplicated`). The first data row is read together with the header, so a broken first row (`malformed_row`, `source_row_too_long`) stops here too.
|
|
25
|
+
- A model that cannot be used raises `Pipeforge::TargetSchemaError`: `model_not_found` or `target_table_not_found`.
|
|
26
|
+
- An object without one of the three methods raises `ArgumentError`.
|
|
27
|
+
|
|
28
|
+
Pipeforge looks for the model among the classes already loaded and loads none itself. Rails in development loads a model the first time your code refers to it, so refer to the class before you create the migration.
|
|
29
|
+
|
|
30
|
+
## What you get back
|
|
31
|
+
|
|
32
|
+
`header_analysis` is a `Pipeforge::HeaderAnalyzer::Result`:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
HeaderAnalyzer::Result
|
|
36
|
+
├── target_table_name String, for example "employees"
|
|
37
|
+
├── model_name String, for example "Employee"
|
|
38
|
+
├── matches Array of Hash, in table order
|
|
39
|
+
│ ├── target_column_name String
|
|
40
|
+
│ ├── source_column_name String
|
|
41
|
+
│ ├── score Float, from 0 to 100
|
|
42
|
+
│ ├── tied_source_column_names Array of String, empty when nothing tied
|
|
43
|
+
│ └── metadata the target column, see below
|
|
44
|
+
├── unmatched_source_column_names Array of String, in file order
|
|
45
|
+
└── unmatched_target_columns Array of Hash, in table order
|
|
46
|
+
├── target_column_name String
|
|
47
|
+
└── metadata the target column, see below
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
The metadata of one target column, here `department_id`:
|
|
51
|
+
|
|
52
|
+
```text
|
|
53
|
+
metadata
|
|
54
|
+
├── type "integer"
|
|
55
|
+
├── sql_type "bigint"
|
|
56
|
+
├── limit 8 bytes of an integer; significand bits of a float; nil for text
|
|
57
|
+
├── is_signed true false for an unsigned integer; nil for other types
|
|
58
|
+
├── max_length nil only for a text column
|
|
59
|
+
├── precision nil only for a decimal column
|
|
60
|
+
├── scale nil only for a decimal column
|
|
61
|
+
├── default nil
|
|
62
|
+
├── enum_labels nil or the labels, for example ["active", "on_leave", "inactive"]
|
|
63
|
+
├── required false
|
|
64
|
+
├── foreign_key {association_name: "department", parent_table_name: "departments"}, or nil
|
|
65
|
+
└── polymorphic nil, or {association_name: "commentable", role: "id"} (or role "type")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`header_analysis.to_h` gives the same data as plain Hashes, Arrays and text, ready for JSON.
|
|
69
|
+
|
|
70
|
+
`metadata` describes the target column: `type`, `sql_type`, `limit`, `is_signed`, `max_length`, `precision`, `scale`, `default`, `enum_labels`, `required`, `foreign_key` (the association and the parent table) and `polymorphic` (the association and whether the column is its id or its type).
|
|
71
|
+
|
|
72
|
+
`header_analysis.to_h` gives all of it as plain data, ready for JSON.
|
|
73
|
+
|
|
74
|
+
## What the step promises
|
|
75
|
+
|
|
76
|
+
- Only names are compared. Values, types and constraints never change a score, and no language model is involved.
|
|
77
|
+
- The same names and settings give the same result every time. The order of the columns in the file or the table does not change which columns are paired.
|
|
78
|
+
- No row of the file's data and no row of your table is looked at.
|
|
79
|
+
- A target column has at most one match.
|
|
80
|
+
- The primary key, columns your model ignores, generated columns and array columns are not offered.
|
|
81
|
+
|
|
82
|
+
## How a score is worked out
|
|
83
|
+
|
|
84
|
+
Each name is split into lowercase words at spaces and punctuation, at capital letters and between letters and digits:
|
|
85
|
+
|
|
86
|
+
| Name | Words |
|
|
87
|
+
|---|---|
|
|
88
|
+
| `Date of Joining` | `date`, `of`, `joining` |
|
|
89
|
+
| `DepartmentID` | `department`, `id` |
|
|
90
|
+
| `Address2` | `address`, `2` |
|
|
91
|
+
|
|
92
|
+
Two words score 1 when they are the same. When one is the beginning of the other, they score the shorter length divided by the longer (`f` and `first` score 0.2). Otherwise they score 0. A plural is not turned into its singular.
|
|
93
|
+
|
|
94
|
+
The score of two columns is twice the sum of the word scores, divided by the number of words on both sides, as a percentage. When the paired words are in a different order, the `reorder_penalty` is taken off.
|
|
95
|
+
|
|
96
|
+
| Target | Source | Score | Why |
|
|
97
|
+
|---|---|---|---|
|
|
98
|
+
| `first_name` | `First Name` | 100 | the same words |
|
|
99
|
+
| `first_name` | `Name First` | 90 | the same words, in another order |
|
|
100
|
+
| `email` | `Email Address` | 66.67 | one word of two on the source side |
|
|
101
|
+
| `company_id` | `Company` | 66.67 | `id` is missing |
|
|
102
|
+
| `first_name` | `F Name` | 60 | `f` is only a short beginning of `first` |
|
|
103
|
+
| `email` | `E-mail` | 13.33 | `E-mail` splits into `e` and `mail` |
|
|
104
|
+
| `joined_on` | `Date of Joining` | 0 | no word is the same or the beginning of another |
|
|
105
|
+
|
|
106
|
+
A pair needs a score of at least `inclusion_threshold` (55 by default) to be a match.
|
|
107
|
+
|
|
108
|
+
## How matches are chosen
|
|
109
|
+
|
|
110
|
+
Pairs are taken from the best score down.
|
|
111
|
+
|
|
112
|
+
- A target column keeps the first source column it gets.
|
|
113
|
+
- A source column used by a better pair is not used again for a weaker one.
|
|
114
|
+
- Two target columns with the same score for one source column both get it. You choose which one keeps it.
|
|
115
|
+
- When several source columns score the same for one target column, one becomes the match and the others are listed in `tied_source_column_names`.
|
|
116
|
+
|
|
117
|
+
## Getting the most from it
|
|
118
|
+
|
|
119
|
+
- Show `score` and `tied_source_column_names` to the person who reviews the matches, so that a doubtful pair is easy to see.
|
|
120
|
+
- Use `unmatched_target_columns` with `metadata[:required]` to show what must still be filled.
|
|
121
|
+
- Expect different words to stay unmatched (`Mail`, `Surname`, `Date of Joining`). Pairing them is the reviewer's job.
|
|
122
|
+
- If your files abbreviate heavily, lower `recommendation.inclusion_threshold` and review more carefully; raise it if you see matches you would not accept.
|
|
123
|
+
|
|
124
|
+
The reasons behind these rules are in [how column names are matched](https://github.com/venkydev02/Pipeforge/blob/v0.1.0/docs/architecture/recommendation-name-matching.md).
|
|
125
|
+
|
|
126
|
+
Next: [measuring values](measuring-values.md).
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# Measuring values: `analyze_column_values`
|
|
2
|
+
|
|
3
|
+
This step answers "will the values in each column fit, and which ones need translating?" It reads the whole file once and writes nothing.
|
|
4
|
+
|
|
5
|
+
## Terms
|
|
6
|
+
|
|
7
|
+
- Mapped columns: a Hash you write that says which source column fills which target column.
|
|
8
|
+
- Value mapping: part of one entry of the mapped columns. It rewrites values of the file before they are loaded.
|
|
9
|
+
- Cast: what your model turns a text into for a column's type, for example `"31"` into 31.
|
|
10
|
+
- Distinct values: each different text of a column, with how often it appears.
|
|
11
|
+
|
|
12
|
+
## What you give
|
|
13
|
+
|
|
14
|
+
```ruby
|
|
15
|
+
mapped_columns = {
|
|
16
|
+
email: {source_column_name: "Email"},
|
|
17
|
+
status: {source_column_name: "Status", value_mapping: {"Left" => "inactive"}},
|
|
18
|
+
salary: {source_column_name: "Salary", value_mapping: {"N/A" => nil}}
|
|
19
|
+
}
|
|
20
|
+
analysis = migration.analyze_column_values(mapped_columns: mapped_columns)
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
- Keys are target column names, as Symbols or Strings. A Hash read with `JSON.parse` works as it is.
|
|
24
|
+
- Each entry has `source_column_name` and, if you want, `value_mapping`. Any other key is an error.
|
|
25
|
+
- The keys of a value mapping are texts as they appear in the file. A key such as `1` is read as `"1"`.
|
|
26
|
+
- One source column may fill several target columns.
|
|
27
|
+
- Every required target column needs an entry. A column is required when it is NOT NULL and has no default, in the table or the model; timestamps that Rails fills do not count.
|
|
28
|
+
- Your Hash is not changed.
|
|
29
|
+
|
|
30
|
+
## What you get back
|
|
31
|
+
|
|
32
|
+
`analysis` is a `Pipeforge::ColumnValueAnalyzer::Result`:
|
|
33
|
+
|
|
34
|
+
```text
|
|
35
|
+
ColumnValueAnalyzer::Result
|
|
36
|
+
├── row_count Integer, the data rows read
|
|
37
|
+
└── column_summaries Hash, keyed by the target column name as a String
|
|
38
|
+
└── one ColumnSummary per mapped column
|
|
39
|
+
├── source_column_name String
|
|
40
|
+
├── position Integer, starting at 1
|
|
41
|
+
├── empty Integer, the empty cells
|
|
42
|
+
├── value_mapping Hash, the mapping that was applied
|
|
43
|
+
└── distinct_values
|
|
44
|
+
├── compatible Array of CompatibleValue, most frequent first
|
|
45
|
+
│ ├── value the text in the file
|
|
46
|
+
│ ├── count how often it appears
|
|
47
|
+
│ ├── mapping_applied whether your mapping had an entry for it
|
|
48
|
+
│ ├── mapped_value what would be loaded
|
|
49
|
+
│ └── casted_to what the model makes of mapped_value; nil for the JSON null
|
|
50
|
+
├── incompatible Array of IncompatibleValue, most frequent first
|
|
51
|
+
│ └── the same fields as a CompatibleValue, and also
|
|
52
|
+
│ ├── casted_to nil when the type cannot read the value
|
|
53
|
+
│ └── reason Symbol, one of the codes below
|
|
54
|
+
├── compatible_count Integer, the sum of the counts of compatible
|
|
55
|
+
└── incompatible_count Integer, the sum of the counts of incompatible
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`analysis.to_h` gives the same data as plain Hashes, Arrays and text, ready for JSON. The `reason` codes are:
|
|
59
|
+
|
|
60
|
+
| `reason` | Meaning |
|
|
61
|
+
|---|---|
|
|
62
|
+
| `value_not_castable` | the type cannot read it, for example a date that is not in the calendar |
|
|
63
|
+
| `value_too_long` | longer than the column's length, counted in characters |
|
|
64
|
+
| `value_out_of_range` | a number too large or too small for the column |
|
|
65
|
+
| `value_not_allowed` | not one of the labels of the enum |
|
|
66
|
+
|
|
67
|
+
## What the step promises
|
|
68
|
+
|
|
69
|
+
- Each value is measured as what would be loaded: your mapping first, then your model's cast, then the limits of the column.
|
|
70
|
+
- Every distinct value is listed. The lists are not cut short.
|
|
71
|
+
- A value that is empty, or that your mapping turns into `nil` or an empty text, counts as `empty` and is not listed. `false` and `0` are not empty.
|
|
72
|
+
- Only the file and the structure of your model are read. No row of your table is read and nothing is written.
|
|
73
|
+
- The file is read again on every call, so you can edit a mapping and measure again.
|
|
74
|
+
- Mistakes in the mapped columns are reported together, so you fix them in one round.
|
|
75
|
+
|
|
76
|
+
## What it does not check
|
|
77
|
+
|
|
78
|
+
It judges one value at a time against its column. It does not run your validations, look for repeated values, or check that a parent row exists. A [dry run](running-the-migration.md) does all three.
|
|
79
|
+
|
|
80
|
+
## The cast is your model's
|
|
81
|
+
|
|
82
|
+
Pipeforge casts a value the way your model would, so that this step and the run agree. ActiveRecord is lenient, and some results may surprise you:
|
|
83
|
+
|
|
84
|
+
| Column type | Text in the file | What it becomes |
|
|
85
|
+
|---|---|---|
|
|
86
|
+
| integer | `thirty` | 0 |
|
|
87
|
+
| integer | `1,000` | 1 |
|
|
88
|
+
| decimal | `N/A` | 0.0 |
|
|
89
|
+
| boolean | `no` | `true` |
|
|
90
|
+
| boolean | `false` | `false` |
|
|
91
|
+
| date | `Feb 3, 2024` | 2024-02-03 |
|
|
92
|
+
| date | `2023-02-30` | cannot be read |
|
|
93
|
+
| json | `{oops` | cannot be read |
|
|
94
|
+
|
|
95
|
+
The first four count as compatible. Before you trust a count, look for values where `casted_to` is not what the person who wrote the file meant:
|
|
96
|
+
|
|
97
|
+
```ruby
|
|
98
|
+
salary = analysis.column_summaries["salary"].distinct_values
|
|
99
|
+
salary.compatible.each { |listed| puts "#{listed.value} -> #{listed.casted_to}" }
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
An enum column accepts its labels and nothing else. The number the database stores for a label, such as `1`, is not accepted; map it to the label.
|
|
103
|
+
|
|
104
|
+
## Errors
|
|
105
|
+
|
|
106
|
+
| Raised | When |
|
|
107
|
+
|---|---|
|
|
108
|
+
| `ArgumentError` | the mapped columns have the wrong shape: not a Hash, an entry that is not a Hash, an unknown key, a value mapping that is not a Hash |
|
|
109
|
+
| `Pipeforge::InvalidMappedColumnsError` | `target_column_not_in_schema`, `column_missing_source_column`, `mapped_column_not_in_file`, `required_column_unmapped` |
|
|
110
|
+
| `Pipeforge::Source::Error` | a row cannot be parsed (`malformed_row`) or has more cells than the header (`source_row_too_long`) |
|
|
111
|
+
|
|
112
|
+
## Getting the most from it
|
|
113
|
+
|
|
114
|
+
- Build your value mappings from the lists: every `incompatible` value needs a decision.
|
|
115
|
+
- A value with `mapping_applied` false still needs a mapping. One with `mapping_applied` true has a mapping that does not fit.
|
|
116
|
+
- Map a column the file writes as `Engineering` to the parent's id yourself. Parents are never looked up by name.
|
|
117
|
+
- Mapping a text to `nil` on a column with a default means the default is loaded, not an empty value.
|
|
118
|
+
- For a JSON column, a mapped text is read as JSON. To load the JSON string `unknown`, map to `"\"unknown\""`.
|
|
119
|
+
- Memory grows with the number of distinct values, so leave out free-text columns you do not need to inspect.
|
|
120
|
+
|
|
121
|
+
Previous: [matching columns](matching-columns.md). Next: [running the migration](running-the-migration.md).
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Running the migration: `run`
|
|
2
|
+
|
|
3
|
+
This step loads the rows, and tells you for every row that was not loaded what was wrong with it. It does not need the earlier steps to have been called.
|
|
4
|
+
|
|
5
|
+
## Terms
|
|
6
|
+
|
|
7
|
+
- Dry run: the same checks and writes as a real run, inside one transaction that is rolled back at the end.
|
|
8
|
+
- Chunk: a group of rows that are read and processed together.
|
|
9
|
+
- Failed row: a row that was not loaded.
|
|
10
|
+
|
|
11
|
+
## What you give
|
|
12
|
+
|
|
13
|
+
```ruby
|
|
14
|
+
totals = migration.run(mode: :dry_run, mapped_columns: mapped_columns) do |chunk_result|
|
|
15
|
+
chunk_result.failed_rows.each { |failed_row| puts failed_row.to_h }
|
|
16
|
+
end
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
| Argument | Meaning |
|
|
20
|
+
|---|---|
|
|
21
|
+
| `mode:` | `:dry_run` or `:run` |
|
|
22
|
+
| `mapped_columns:` | as for [measuring values](measuring-values.md) |
|
|
23
|
+
| `offset:` | rows to skip before the first one processed; 0 by default |
|
|
24
|
+
| `chunk_size:` | rows in a chunk; the `execution.chunk_size` setting (2000) by default |
|
|
25
|
+
| block | called with the result of each chunk; optional |
|
|
26
|
+
|
|
27
|
+
A wrong `mode`, `offset` or `chunk_size` raises one `ArgumentError` that names each of them. The mapped columns are checked next. Nothing is read or written until both pass.
|
|
28
|
+
|
|
29
|
+
## What you get back
|
|
30
|
+
|
|
31
|
+
`run` returns a `Pipeforge::RunProcessor::RunResult`. The block receives a `ChunkResult` for each chunk, as soon as the chunk is done:
|
|
32
|
+
|
|
33
|
+
```text
|
|
34
|
+
RunResult (returned by run)
|
|
35
|
+
├── processed_rows_count Integer
|
|
36
|
+
├── valid_rows_count Integer, the rows loaded
|
|
37
|
+
└── failed_rows_count Integer
|
|
38
|
+
|
|
39
|
+
ChunkResult (given to the block)
|
|
40
|
+
├── first_row_number Integer, the first data row of the chunk
|
|
41
|
+
├── last_row_number Integer, the last data row of the chunk
|
|
42
|
+
├── processed_rows_count Integer
|
|
43
|
+
├── valid_rows_count Integer
|
|
44
|
+
├── failed_rows_count Integer
|
|
45
|
+
└── failed_rows Array of FailedRow, in file order
|
|
46
|
+
├── row_number Integer
|
|
47
|
+
├── attributes Hash: column name => value, after casting and callbacks;
|
|
48
|
+
│ every column of the record, also those you did not map
|
|
49
|
+
└── errors Array of ColumnError, at most one for each column
|
|
50
|
+
├── origin Symbol: :schema, :model or :database
|
|
51
|
+
├── code Symbol, for example :value_too_long
|
|
52
|
+
├── kind Symbol, or nil
|
|
53
|
+
├── target_column_name String, or nil for the row as a whole and for a database error
|
|
54
|
+
├── message String, for a person; its wording can change
|
|
55
|
+
├── details Hash of facts, for example {length: 34, max_length: 20}
|
|
56
|
+
└── value the value that failed, or nil
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Ruby holds `origin`, `code` and `kind` as Symbols. `to_h` keeps the Symbols, and JSON turns them into text. `failed_row.to_h` is what you store.
|
|
60
|
+
|
|
61
|
+
A row number counts data rows from the start of the file, the first one being 1. The header, blank lines and rows with only empty cells are not counted, so it can differ from the line number.
|
|
62
|
+
|
|
63
|
+
## What the step promises
|
|
64
|
+
|
|
65
|
+
- Every processed row is either loaded or given to your block as a failed row.
|
|
66
|
+
- A dry run reports the same failed rows and totals as a real run, for the same data and the same table.
|
|
67
|
+
- A dry run leaves your tables as they were.
|
|
68
|
+
- Rows are processed one at a time, in file order, so your uniqueness validation also catches two rows of the file with the same value.
|
|
69
|
+
- One chunk is held in memory. Pipeforge writes no report file.
|
|
70
|
+
|
|
71
|
+
## What happens to a row
|
|
72
|
+
|
|
73
|
+
1. Your value mapping is applied. A column whose value is empty is left out, so the column's default fills it.
|
|
74
|
+
2. The values are cast and given to a new record of your model. A value the model cannot be given is an error for its column.
|
|
75
|
+
3. Your validations and callbacks run (`valid?`).
|
|
76
|
+
4. The limits of the table are checked on what the record now holds.
|
|
77
|
+
5. A row with no error is saved with `save!(validate: false)` in its own transaction. A row the database refuses becomes a failed row, and the run goes on.
|
|
78
|
+
|
|
79
|
+
Parent rows are loaded once for each chunk, so `belongs_to` checks do not query once for each row.
|
|
80
|
+
|
|
81
|
+
## Reading an error
|
|
82
|
+
|
|
83
|
+
Each error has seven fields: `origin`, `code`, `kind`, `target_column_name`, `message`, `details` and `value`.
|
|
84
|
+
|
|
85
|
+
| `origin` | Who refused the row |
|
|
86
|
+
|---|---|
|
|
87
|
+
| `schema` | Pipeforge, from the table and your enums |
|
|
88
|
+
| `model` | your model, when building, validating or saving the record |
|
|
89
|
+
| `database` | the database, when writing |
|
|
90
|
+
|
|
91
|
+
| `code` | Meaning | `kind` |
|
|
92
|
+
|---|---|---|
|
|
93
|
+
| `value_missing` | required but empty | |
|
|
94
|
+
| `value_not_accepted` | the model cannot be given the value | `not_castable`, `enum_label`, `polymorphic_type` |
|
|
95
|
+
| `value_too_long` | longer than allowed | |
|
|
96
|
+
| `value_out_of_range` | outside what the column holds | `bytes`, `digits`, `float` |
|
|
97
|
+
| `value_not_unique` | a row with the same value exists | |
|
|
98
|
+
| `parent_missing` | the parent row does not exist | |
|
|
99
|
+
| `value_invalid` | another validation refused it | `save_stopped` when a callback stopped the save |
|
|
100
|
+
| `database_error` | any other refusal of the database | |
|
|
101
|
+
|
|
102
|
+
Some mistakes have another name here than in the value analysis: a text the type cannot read is `value_not_castable` there and `value_not_accepted` here, and so is a value that is not an enum label (`value_not_allowed` there).
|
|
103
|
+
|
|
104
|
+
Build your code on `code` and `kind`. `message` is for a person and its wording can change. `details` holds the facts, and `native_message` in it is your model's or your database's own text.
|
|
105
|
+
|
|
106
|
+
An error from the database has no `target_column_name`.
|
|
107
|
+
|
|
108
|
+
## Errors that stop the run
|
|
109
|
+
|
|
110
|
+
| Raised | When |
|
|
111
|
+
|---|---|
|
|
112
|
+
| `ArgumentError` | a wrong `mode`, `offset` or `chunk_size`, or mapped columns of the wrong shape |
|
|
113
|
+
| `Pipeforge::InvalidMappedColumnsError` | the mapped columns do not fit the file or the table |
|
|
114
|
+
| `Pipeforge::Source::Error` | a row cannot be parsed or has more cells than the header |
|
|
115
|
+
|
|
116
|
+
A bad row of the file can stop a run part of the way. In a `:run`, the rows of earlier chunks stay in the table. In a dry run, nothing stays.
|
|
117
|
+
|
|
118
|
+
## Continuing a run
|
|
119
|
+
|
|
120
|
+
Keep the last row number of each chunk. If the run stops, pass the last one you kept as `offset:`:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
last_row_number = 0
|
|
124
|
+
migration.run(mode: :run, mapped_columns: mapped_columns) do |chunk_result|
|
|
125
|
+
last_row_number = chunk_result.last_row_number
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
migration.run(mode: :run, mapped_columns: mapped_columns, offset: last_row_number)
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
Rows are committed one at a time, so a run stopped inside a chunk has already stored some rows of it. When you continue, those fail as repeats if your model or table has a unique rule, and are stored twice if neither has one.
|
|
132
|
+
|
|
133
|
+
## What a dry run still does
|
|
134
|
+
|
|
135
|
+
- Your `before_save` and `after_save` callbacks run. A callback that calls another service will really call it.
|
|
136
|
+
- Your block runs inside the transaction that is rolled back, so anything it writes to the database is rolled back too.
|
|
137
|
+
- The database still hands out ids, which are not given back by a rollback.
|
|
138
|
+
|
|
139
|
+
## Getting the most from it
|
|
140
|
+
|
|
141
|
+
- Dry run until the failed rows are the ones you expect, then run.
|
|
142
|
+
- Store failed rows in your block if a person needs to fix them; they are not kept for you.
|
|
143
|
+
- Use a smaller `chunk_size` to see progress sooner and to lose less work if a run stops.
|
|
144
|
+
|
|
145
|
+
Previous: [measuring values](measuring-values.md).
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pipeforge
|
|
4
|
+
# The adapters the gem knows: for each name, the class of its target and the file that holds it.
|
|
5
|
+
# Resolving a name requires that file and gives the class.
|
|
6
|
+
#
|
|
7
|
+
# @api private
|
|
8
|
+
module AdapterRegistry
|
|
9
|
+
ADAPTERS = {
|
|
10
|
+
"activerecord" => {class_name: "Pipeforge::ActiveRecordTarget", path: "pipeforge/adapters/activerecord/adapter"}
|
|
11
|
+
}.freeze
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# @return [Array<String>] the names of the adapters
|
|
15
|
+
def names
|
|
16
|
+
ADAPTERS.keys
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Requires the file of an adapter and gives its target class.
|
|
20
|
+
#
|
|
21
|
+
# @param name [String] one of {names}
|
|
22
|
+
# @return [Class]
|
|
23
|
+
# @raise [LoadError] the file of the adapter, or a library it needs, cannot be loaded
|
|
24
|
+
def resolve(name:)
|
|
25
|
+
adapter = ADAPTERS.fetch(name)
|
|
26
|
+
|
|
27
|
+
begin
|
|
28
|
+
require adapter.fetch(:path)
|
|
29
|
+
rescue LoadError => load_error
|
|
30
|
+
raise LoadError, ArgumentMessages::MESSAGES.fetch(:adapter_not_loaded).call(name: name, reason: load_error.message)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
Object.const_get(adapter.fetch(:class_name))
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The entry file of the ActiveRecord adapter, required by the adapter registry. The version range is
|
|
4
|
+
# stated here because the gem does not declare ActiveRecord as a dependency.
|
|
5
|
+
gem "activerecord", ">= 8.0", "< 9"
|
|
6
|
+
require "active_record"
|
|
7
|
+
require_relative "target"
|