exwiw 0.4.11 → 0.5.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 +4 -4
- data/CHANGELOG.md +11 -0
- data/README.md +44 -11
- data/lib/exwiw/after_insert_hook.rb +1 -1
- data/lib/exwiw/cli.rb +133 -13
- data/lib/exwiw/explain_runner.rb +3 -3
- data/lib/exwiw/runner.rb +3 -3
- data/lib/exwiw/version.rb +1 -1
- data/lib/tasks/exwiw.rake +3 -3
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 224bdc1d3b0f94e08463ad9e42a6e67d0592d902388b5873f5840226dbdbd3fe
|
|
4
|
+
data.tar.gz: de9ddd4a625565e0bcd28ff3f74df8da06092c443ad1f170d41c5858a24c4802
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '08f564c07c09561a4b9b825bb7f6ca43a076df5b8262f165addad471639084fa5b5074330215edd36951ce0c427f510533f39d03e0632c1306ba4e9054391b33'
|
|
7
|
+
data.tar.gz: 2c161f236a676a15774fb097a7a2c4d66f95f38be8c465dfc29788b4c45165aa3b13dee2b1da771e317672e63f089d2496e10cf4fe2ebf16f97885e0e1c49c76
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.5.0] - 2026-06-16
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
|
|
9
|
+
- A YAML **config file** (`exwiw.yml`) can now hold any option except the database connection settings, so they no longer have to be repeated on every invocation. Pass it with `--config=PATH`; when `--config` is omitted, `exwiw.yml` (or `exwiw.yaml`) is loaded automatically from the current directory if present. **Options passed on the CLI take precedence** over the file (the file only fills in options not given on the CLI). Connection settings — `host`, `port`, `user`, `database`, `uri`, `password` — are **rejected** in the file (they must come from the CLI/environment); `adapter` is the one connection-related key allowed. Relative paths in the file (`schema_dir`, `output_dir`, `after_insert_hook`) are resolved relative to the config file's own directory (so a root-level `exwiw.yml` with `schema_dir: exwiw/schema` reads naturally and an absolute `--config` works from any directory). Unknown keys are rejected to catch typos, and export-only keys (`output_dir`, `output_format`, `insert_only`, `after_insert_hook`) are ignored under `explain` so one file can be shared by both subcommands.
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- **BREAKING**: the `export`/`explain` CLI option `--config-dir` has been renamed to `--schema-dir` to distinguish the directory of schema JSON files from the new `--config` config file. Its short form `-c` is now `--config` (the config file); `--schema-dir` has no short form. The hook contract is renamed to match: the shell-hook environment variable `EXWIW_CONFIG_DIR` is now `EXWIW_SCHEMA_DIR`, and the Ruby-hook `cli_options[:config_dir]` is now `cli_options[:schema_dir]`. Update invocations, scripts, and hooks accordingly (`--config-dir` no longer exists). `--schema-dir` is still required and has no default unless `schema_dir` is set in the config file.
|
|
14
|
+
- **BREAKING**: the env var that overrides where `schema:generate`, `schema:tidy`, and `schema:generate_mongoid` write their config has been renamed from `OUTPUT_DIR_PATH` to `EXWIW_SCHEMA_DIR_PATH`, and the default output directory is now `exwiw/schema` (previously `exwiw`). The new name disambiguates it from the dump-side `--output-dir`, and the dedicated `schema/` subdirectory leaves `exwiw/` free for other artifacts (hooks, dumps). `OUTPUT_DIR_PATH` is no longer read. Existing repositories should set `EXWIW_SCHEMA_DIR_PATH` (e.g. `EXWIW_SCHEMA_DIR_PATH=exwiw` to preserve the old flat layout) and/or move their config under `exwiw/schema/`; otherwise a `generate` run will write a fresh copy into `exwiw/schema/` and leave the old files stale. The `export`/`explain` CLI is unaffected, but examples now point at `exwiw/schema`.
|
|
15
|
+
|
|
5
16
|
## [0.4.11] - 2026-06-15
|
|
6
17
|
|
|
7
18
|
### Fixed
|
data/README.md
CHANGED
|
@@ -72,7 +72,7 @@ exwiw \
|
|
|
72
72
|
--port=3306 \
|
|
73
73
|
--user=reader \
|
|
74
74
|
--database=app_production \
|
|
75
|
-
--
|
|
75
|
+
--schema-dir=exwiw/schema \
|
|
76
76
|
--target-table=shops \
|
|
77
77
|
--ids=1 \ # comma separated ids
|
|
78
78
|
--output-dir=dump \
|
|
@@ -81,7 +81,7 @@ exwiw \
|
|
|
81
81
|
|
|
82
82
|
By default `--ids` are matched against the target table's primary key. `--ids-column=COLUMN` matches them against a different column instead (e.g. `--target-table=users --ids=alice@example.com --ids-column=email`). Related tables are still extracted correctly: their foreign keys are resolved through the target via a subquery (`WHERE fk IN (SELECT pk FROM target WHERE COLUMN IN (...))`), so only the target table's filter column changes. This is the SQL-adapter counterpart of the mongodb `--ids-field`; the two are mutually exclusive and each is rejected by the other adapter family. Note: if `COLUMN` is itself masked, re-running `delete-*` against an already-imported (masked) dump won't match, so prefer a stable natural key.
|
|
83
83
|
|
|
84
|
-
When `--target-table` and `--ids` are omitted, exwiw dumps all tables defined in `--
|
|
84
|
+
When `--target-table` and `--ids` are omitted, exwiw dumps all tables defined in `--schema-dir`:
|
|
85
85
|
|
|
86
86
|
```bash
|
|
87
87
|
# dump all tables
|
|
@@ -91,7 +91,7 @@ exwiw \
|
|
|
91
91
|
--port=5432 \
|
|
92
92
|
--user=reader \
|
|
93
93
|
--database=app_production \
|
|
94
|
-
--
|
|
94
|
+
--schema-dir=exwiw/schema \
|
|
95
95
|
--output-dir=dump
|
|
96
96
|
```
|
|
97
97
|
|
|
@@ -123,25 +123,58 @@ exwiw explain \
|
|
|
123
123
|
--adapter=postgresql \
|
|
124
124
|
--host=localhost --port=5432 --user=reader \
|
|
125
125
|
--database=app_production \
|
|
126
|
-
--
|
|
126
|
+
--schema-dir=exwiw/schema \
|
|
127
127
|
--target-table=shops --ids=1
|
|
128
128
|
```
|
|
129
129
|
|
|
130
130
|
The `--output-dir`, `--output-format`, `--insert-only`, and `--after-insert-hook` options are dump-specific and rejected when used with `explain`.
|
|
131
131
|
|
|
132
|
+
### Config file (`exwiw.yml`)
|
|
133
|
+
|
|
134
|
+
Options you would otherwise repeat on every run can be kept in a YAML config file. Pass it with `--config=PATH`; when `--config` is omitted, exwiw automatically loads `exwiw.yml` (or `exwiw.yaml`) from the current directory if present.
|
|
135
|
+
|
|
136
|
+
**Options passed on the CLI always take precedence over the config file** — the config only fills in options you did not pass. This lets you commit the stable settings (which schema to read, output format, ...) while still varying the environment-specific connection details per invocation.
|
|
137
|
+
|
|
138
|
+
```yaml
|
|
139
|
+
# exwiw.yml — keep at the project root, alongside exwiw/schema/
|
|
140
|
+
adapter: postgresql
|
|
141
|
+
schema_dir: exwiw/schema
|
|
142
|
+
output_dir: dump
|
|
143
|
+
output_format: insert # insert | copy
|
|
144
|
+
insert_only: false
|
|
145
|
+
after_insert_hook: hooks/seed.rb
|
|
146
|
+
log_level: info # debug | info
|
|
147
|
+
# target_table / ids / ids_field / ids_column may also be set here
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
With the file above, only the connection details need to be supplied on the CLI:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
DATABASE_PASSWORD=... exwiw \
|
|
154
|
+
--host=localhost --port=5432 --user=reader --database=app_production \
|
|
155
|
+
--target-table=shops --ids=1
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
Notes:
|
|
159
|
+
|
|
160
|
+
- **Database connection settings stay on the CLI/environment.** `host`, `port`, `user`, `database`, `uri`, and `password` are **rejected** in the config file (exwiw exits with an error). `adapter` is the one connection-related key that *is* allowed in the file.
|
|
161
|
+
- **Relative paths in the config (`schema_dir`, `output_dir`, `after_insert_hook`) are resolved relative to the config file's own directory**, not the current working directory. So with the config at the project root, `schema_dir: exwiw/schema` reads naturally, and an absolute `--config=/path/to/exwiw.yml` works no matter where you run from. (CLI path flags remain relative to the current directory — each source resolves relative to where it is written.) Absolute paths are used as-is.
|
|
162
|
+
- Unknown keys are rejected so a typo surfaces immediately.
|
|
163
|
+
- Export-only keys (`output_dir`, `output_format`, `insert_only`, `after_insert_hook`) are ignored when running `explain`, so a single config file can be shared by both subcommands.
|
|
164
|
+
|
|
132
165
|
### Generator
|
|
133
166
|
|
|
134
167
|
The config generator is provided as a Rake task.
|
|
135
168
|
|
|
136
169
|
```bash
|
|
137
|
-
# generate table schema under exwiw/
|
|
170
|
+
# generate table schema under exwiw/schema/
|
|
138
171
|
bundle exec rake exwiw:schema:generate
|
|
139
172
|
```
|
|
140
173
|
|
|
141
|
-
By default, the schema files will be saved in the `exwiw` directory. You can specify a different output directory by setting the `
|
|
174
|
+
By default, the schema files will be saved in the `exwiw/schema` directory. You can specify a different output directory by setting the `EXWIW_SCHEMA_DIR_PATH` environment variable:
|
|
142
175
|
|
|
143
176
|
```sh
|
|
144
|
-
|
|
177
|
+
EXWIW_SCHEMA_DIR_PATH=custom_directory bundle exec rake exwiw:schema:generate
|
|
145
178
|
```
|
|
146
179
|
|
|
147
180
|
#### Tidying stale config (`schema:tidy`)
|
|
@@ -159,14 +192,14 @@ bundle exec rake exwiw:schema:tidy
|
|
|
159
192
|
|
|
160
193
|
Because it reads the database directly, a table that still exists in the database but has lost (or never had) an ActiveRecord model is **kept** — only a table that is genuinely gone is removed. (This is the deliberate counterpart to `generate`, which is model-driven and only ever adds what the models know about.)
|
|
161
194
|
|
|
162
|
-
It respects `
|
|
195
|
+
It respects `EXWIW_SCHEMA_DIR_PATH` and the per-database subdirectory layout in the same way as `schema:generate`. Unlike `generate`, `tidy` never adds or regenerates entries — every surviving table/column (including hand-edited `comment` / `ignore` / `replace_with`) is left untouched, so it is safe to run on a customized config. The task prints which tables and columns it removed (or that the config was already tidy). Stale `belongs_tos` are not pruned by `tidy`; rerun `schema:generate` to refresh those.
|
|
163
196
|
|
|
164
197
|
#### Multiple databases
|
|
165
198
|
|
|
166
199
|
If the application uses Rails' multiple-database support (`connects_to`), `schema:generate` buckets models by the database they connect to and writes each database's config files into its own subdirectory of the output directory, named after the database config name (`primary`, `analytics`, ...):
|
|
167
200
|
|
|
168
201
|
```
|
|
169
|
-
exwiw/
|
|
202
|
+
exwiw/schema/
|
|
170
203
|
primary/
|
|
171
204
|
shops.json
|
|
172
205
|
users.json
|
|
@@ -267,7 +300,7 @@ This is an example of the one table schema:
|
|
|
267
300
|
}
|
|
268
301
|
```
|
|
269
302
|
|
|
270
|
-
`--
|
|
303
|
+
`--schema-dir` will use all json files in the specified directory.
|
|
271
304
|
|
|
272
305
|
### Output format
|
|
273
306
|
|
|
@@ -307,7 +340,7 @@ SQL
|
|
|
307
340
|
|
|
308
341
|
**Shell hook**: anything other than `.rb` is exec'd as a child process. It is a pure side-effect hook — exwiw does not capture its stdout. The hook receives these env vars and inherits `DATABASE_PASSWORD` from the parent:
|
|
309
342
|
|
|
310
|
-
- `EXWIW_OUTPUT_DIR`, `
|
|
343
|
+
- `EXWIW_OUTPUT_DIR`, `EXWIW_SCHEMA_DIR`
|
|
311
344
|
- `EXWIW_DATABASE_ADAPTER`, `EXWIW_DATABASE_HOST`, `EXWIW_DATABASE_PORT`, `EXWIW_DATABASE_USER`, `EXWIW_DATABASE_NAME`
|
|
312
345
|
- `EXWIW_TARGET_TABLE`, `EXWIW_IDS` (comma-separated), `EXWIW_OUTPUT_FORMAT`
|
|
313
346
|
|
|
@@ -31,7 +31,7 @@ module Exwiw
|
|
|
31
31
|
def self.run_shell(path:, cli_options:, output_dir:, logger:)
|
|
32
32
|
env = {
|
|
33
33
|
'EXWIW_OUTPUT_DIR' => output_dir,
|
|
34
|
-
'
|
|
34
|
+
'EXWIW_SCHEMA_DIR' => cli_options[:schema_dir].to_s,
|
|
35
35
|
'EXWIW_DATABASE_ADAPTER' => cli_options[:database_adapter].to_s,
|
|
36
36
|
'EXWIW_DATABASE_HOST' => cli_options[:database_host].to_s,
|
|
37
37
|
'EXWIW_DATABASE_PORT' => cli_options[:database_port].to_s,
|
data/lib/exwiw/cli.rb
CHANGED
|
@@ -5,6 +5,7 @@ require 'optparse'
|
|
|
5
5
|
require 'pathname'
|
|
6
6
|
|
|
7
7
|
require 'json'
|
|
8
|
+
require 'yaml'
|
|
8
9
|
|
|
9
10
|
require 'exwiw'
|
|
10
11
|
|
|
@@ -12,6 +13,39 @@ module Exwiw
|
|
|
12
13
|
class CLI
|
|
13
14
|
KNOWN_SUBCOMMANDS = %w[export explain].freeze
|
|
14
15
|
|
|
16
|
+
# Config file loaded automatically when --config is omitted, if one exists in
|
|
17
|
+
# the current directory. Kept at the project root (rather than under exwiw/)
|
|
18
|
+
# so that config-relative paths like `schema_dir: exwiw/schema` read naturally.
|
|
19
|
+
# Both extensions are accepted; .yml wins when both are present.
|
|
20
|
+
DEFAULT_CONFIG_PATHS = %w[exwiw.yml exwiw.yaml].freeze
|
|
21
|
+
|
|
22
|
+
# Keys accepted in the config file. Anything outside this set is rejected so
|
|
23
|
+
# a typo surfaces immediately instead of being silently ignored. These mirror
|
|
24
|
+
# the non-connection CLI options (plus `adapter`).
|
|
25
|
+
ALLOWED_CONFIG_KEYS = %w[
|
|
26
|
+
adapter
|
|
27
|
+
schema_dir
|
|
28
|
+
output_dir
|
|
29
|
+
output_format
|
|
30
|
+
insert_only
|
|
31
|
+
after_insert_hook
|
|
32
|
+
log_level
|
|
33
|
+
target_table
|
|
34
|
+
target_collection
|
|
35
|
+
ids
|
|
36
|
+
ids_field
|
|
37
|
+
ids_column
|
|
38
|
+
].freeze
|
|
39
|
+
|
|
40
|
+
# Database connection settings are environment-specific (and sometimes
|
|
41
|
+
# secret-adjacent), so they must be passed via CLI/env, never the committed
|
|
42
|
+
# config file. `adapter` is the one connection-ish key allowed in config.
|
|
43
|
+
REJECTED_CONNECTION_KEYS = %w[host port user database uri password].freeze
|
|
44
|
+
|
|
45
|
+
# Keys that only make sense for `export`. They are skipped when merging config
|
|
46
|
+
# for `explain` so a shared config file does not trip validate_explain_only!.
|
|
47
|
+
EXPORT_ONLY_CONFIG_KEYS = %w[output_dir output_format insert_only after_insert_hook].freeze
|
|
48
|
+
|
|
15
49
|
def self.start(argv)
|
|
16
50
|
new(argv).run
|
|
17
51
|
end
|
|
@@ -34,7 +68,8 @@ module Exwiw
|
|
|
34
68
|
@database_password = ENV["DATABASE_PASSWORD"]
|
|
35
69
|
@connection_uri = nil
|
|
36
70
|
@output_dir = nil
|
|
37
|
-
@
|
|
71
|
+
@schema_dir = nil
|
|
72
|
+
@config_file_path = nil
|
|
38
73
|
@database_adapter = nil
|
|
39
74
|
@database_name = nil
|
|
40
75
|
@target_table_name = nil
|
|
@@ -45,7 +80,9 @@ module Exwiw
|
|
|
45
80
|
@output_format = nil
|
|
46
81
|
@insert_only = nil
|
|
47
82
|
@after_insert_hook_path = nil
|
|
48
|
-
|
|
83
|
+
# nil (not :info) so we can tell "user passed --log-level" from the default,
|
|
84
|
+
# letting a config-file value fill in; the :info default is applied later.
|
|
85
|
+
@log_level = nil
|
|
49
86
|
|
|
50
87
|
parser.parse!(@argv)
|
|
51
88
|
end
|
|
@@ -82,7 +119,7 @@ module Exwiw
|
|
|
82
119
|
Runner.new(
|
|
83
120
|
connection_config: connection_config,
|
|
84
121
|
output_dir: @output_dir,
|
|
85
|
-
|
|
122
|
+
schema_dir: @schema_dir,
|
|
86
123
|
dump_target: dump_target,
|
|
87
124
|
output_format: @output_format,
|
|
88
125
|
insert_only: @insert_only,
|
|
@@ -93,7 +130,7 @@ module Exwiw
|
|
|
93
130
|
when "explain"
|
|
94
131
|
ExplainRunner.new(
|
|
95
132
|
connection_config: connection_config,
|
|
96
|
-
|
|
133
|
+
schema_dir: @schema_dir,
|
|
97
134
|
dump_target: dump_target,
|
|
98
135
|
logger: logger,
|
|
99
136
|
io: $stdout,
|
|
@@ -102,6 +139,14 @@ module Exwiw
|
|
|
102
139
|
end
|
|
103
140
|
|
|
104
141
|
private def validate_options!
|
|
142
|
+
# Fill in any options not given on the CLI from the config file. Done first
|
|
143
|
+
# so a config-provided `adapter` is in place before normalization below.
|
|
144
|
+
# CLI values always win (the merge only fills nil/empty ivars).
|
|
145
|
+
apply_config_file!
|
|
146
|
+
|
|
147
|
+
# Default log level once CLI and config have both had their say.
|
|
148
|
+
@log_level ||= :info
|
|
149
|
+
|
|
105
150
|
# Fold driver/Rails adapter spellings (mysql2, sqlite3) into exwiw's
|
|
106
151
|
# canonical names up front, so every check below — and the
|
|
107
152
|
# EXWIW_DATABASE_ADAPTER passed to hooks — sees the canonical name.
|
|
@@ -163,18 +208,18 @@ module Exwiw
|
|
|
163
208
|
end
|
|
164
209
|
end
|
|
165
210
|
|
|
166
|
-
if @
|
|
167
|
-
$stderr.puts "
|
|
211
|
+
if @schema_dir.nil?
|
|
212
|
+
$stderr.puts "Schema dir is required (pass --schema-dir or set schema_dir in the config file)"
|
|
168
213
|
exit 1
|
|
169
214
|
end
|
|
170
215
|
|
|
171
|
-
unless Dir.exist?(@
|
|
172
|
-
$stderr.puts "
|
|
216
|
+
unless Dir.exist?(@schema_dir)
|
|
217
|
+
$stderr.puts "Schema dir does not exist: #{@schema_dir}"
|
|
173
218
|
exit 1
|
|
174
219
|
end
|
|
175
220
|
|
|
176
|
-
if Dir.glob(File.join(@
|
|
177
|
-
$stderr.puts "
|
|
221
|
+
if Dir.glob(File.join(@schema_dir, "*.json")).empty?
|
|
222
|
+
$stderr.puts "Schema dir contains no .json files: #{@schema_dir}"
|
|
178
223
|
exit 1
|
|
179
224
|
end
|
|
180
225
|
|
|
@@ -202,6 +247,78 @@ module Exwiw
|
|
|
202
247
|
end
|
|
203
248
|
end
|
|
204
249
|
|
|
250
|
+
# Merge settings from the config file (YAML) into any options the user did
|
|
251
|
+
# not pass on the CLI. The CLI always wins: every assignment below only fills
|
|
252
|
+
# an ivar that is still nil/empty after parsing ARGV. Connection settings
|
|
253
|
+
# (except `adapter`) are rejected here — they belong on the CLI/env.
|
|
254
|
+
private def apply_config_file!
|
|
255
|
+
path =
|
|
256
|
+
if @config_file_path
|
|
257
|
+
unless File.file?(@config_file_path)
|
|
258
|
+
$stderr.puts "Config file not found: #{@config_file_path}"
|
|
259
|
+
exit 1
|
|
260
|
+
end
|
|
261
|
+
@config_file_path
|
|
262
|
+
else
|
|
263
|
+
DEFAULT_CONFIG_PATHS.map { |p| File.expand_path(p) }.find { |p| File.file?(p) }
|
|
264
|
+
end
|
|
265
|
+
return if path.nil?
|
|
266
|
+
|
|
267
|
+
# Paths inside the config file are resolved relative to the file's own
|
|
268
|
+
# directory (not cwd), so `schema_dir: exwiw/schema` reads naturally with the
|
|
269
|
+
# config kept at the project root, and an absolute --config works from any
|
|
270
|
+
# cwd. (CLI path flags stay cwd-relative — each source resolves relative to
|
|
271
|
+
# where it is written.) `path` is always absolute here.
|
|
272
|
+
base = File.dirname(path)
|
|
273
|
+
|
|
274
|
+
config = YAML.safe_load(File.read(path)) || {}
|
|
275
|
+
unless config.is_a?(Hash)
|
|
276
|
+
$stderr.puts "Config file must be a YAML mapping (key: value): #{path}"
|
|
277
|
+
exit 1
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
config.each_key do |key|
|
|
281
|
+
if REJECTED_CONNECTION_KEYS.include?(key)
|
|
282
|
+
$stderr.puts "'#{key}' is a database connection setting and must be passed via the CLI/environment, not the config file (#{path})"
|
|
283
|
+
exit 1
|
|
284
|
+
end
|
|
285
|
+
unless ALLOWED_CONFIG_KEYS.include?(key)
|
|
286
|
+
$stderr.puts "Unknown config key '#{key}' in #{path}. Allowed keys: #{ALLOWED_CONFIG_KEYS.join(', ')}"
|
|
287
|
+
exit 1
|
|
288
|
+
end
|
|
289
|
+
end
|
|
290
|
+
|
|
291
|
+
# For `explain`, drop export-only keys so a config shared with `export`
|
|
292
|
+
# does not make validate_explain_only! reject the run.
|
|
293
|
+
config = config.reject { |k, _| EXPORT_ONLY_CONFIG_KEYS.include?(k) } if @subcommand == "explain"
|
|
294
|
+
|
|
295
|
+
@database_adapter ||= config["adapter"]
|
|
296
|
+
@schema_dir ||= expand_dir(config["schema_dir"], base)
|
|
297
|
+
@output_dir ||= expand_dir(config["output_dir"], base)
|
|
298
|
+
@after_insert_hook_path ||= (File.expand_path(config["after_insert_hook"], base) if config["after_insert_hook"])
|
|
299
|
+
@output_format ||= config["output_format"]
|
|
300
|
+
@insert_only = config["insert_only"] if @insert_only.nil? && config.key?("insert_only")
|
|
301
|
+
@log_level ||= config["log_level"]&.to_sym
|
|
302
|
+
@target_table_name ||= config["target_table"]
|
|
303
|
+
@target_collection_name ||= config["target_collection"]
|
|
304
|
+
if @ids.empty? && config.key?("ids")
|
|
305
|
+
raw = config["ids"]
|
|
306
|
+
# Accept either a YAML list or a "1,2" string; coerce to strings to match
|
|
307
|
+
# the CLI's `--ids=1,2` -> ["1", "2"] shape.
|
|
308
|
+
@ids = (raw.is_a?(String) ? raw.split(",") : Array(raw)).map(&:to_s)
|
|
309
|
+
end
|
|
310
|
+
@ids_field ||= config["ids_field"]
|
|
311
|
+
@ids_column ||= config["ids_column"]
|
|
312
|
+
end
|
|
313
|
+
|
|
314
|
+
# Strip a trailing slash (like the CLI's dir options) and expand relative to
|
|
315
|
+
# `base` (the config file's directory). Returns nil for a nil value.
|
|
316
|
+
private def expand_dir(value, base)
|
|
317
|
+
return nil if value.nil?
|
|
318
|
+
value = value.end_with?("/") ? value[0..-2] : value
|
|
319
|
+
File.expand_path(value, base)
|
|
320
|
+
end
|
|
321
|
+
|
|
205
322
|
# `--target-collection` is a mongodb-only alias of `--target-table`. Fold it
|
|
206
323
|
# into @target_table_name (the single field the rest of the CLI/runner uses)
|
|
207
324
|
# after rejecting the misuses: combining it with --target-table, or using it
|
|
@@ -319,7 +436,7 @@ module Exwiw
|
|
|
319
436
|
database_user: @database_user,
|
|
320
437
|
database_password: @database_password,
|
|
321
438
|
output_dir: @output_dir,
|
|
322
|
-
|
|
439
|
+
schema_dir: @schema_dir,
|
|
323
440
|
database_adapter: @database_adapter,
|
|
324
441
|
database_name: @database_name,
|
|
325
442
|
target_table: @target_table_name,
|
|
@@ -368,9 +485,12 @@ module Exwiw
|
|
|
368
485
|
v = v.end_with?("/") ? v[0..-2] : v
|
|
369
486
|
@output_dir = File.expand_path(v)
|
|
370
487
|
end
|
|
371
|
-
opts.on("
|
|
488
|
+
opts.on("--schema-dir=SCHEMA_DIR_PATH", "Directory of schema JSON files. (or set schema_dir in the config file)") do |v|
|
|
372
489
|
v = v.end_with?("/") ? v[0..-2] : v
|
|
373
|
-
@
|
|
490
|
+
@schema_dir = File.expand_path(v)
|
|
491
|
+
end
|
|
492
|
+
opts.on("-c", "--config=CONFIG_FILE_PATH", "Path to the exwiw config YAML. Defaults to ./#{DEFAULT_CONFIG_PATHS.first} (or .#{File.extname(DEFAULT_CONFIG_PATHS.last)}) when present. CLI options take precedence; paths inside the file are resolved relative to the file.") do |v|
|
|
493
|
+
@config_file_path = File.expand_path(v)
|
|
374
494
|
end
|
|
375
495
|
opts.on("-a", "--adapter=ADAPTER", "Database adapter: mysql, sqlite, postgresql, mongodb (aliases: mysql2, sqlite3)") { |v| @database_adapter = v }
|
|
376
496
|
opts.on("--uri=URI", "Full MongoDB connection URI (mongodb:// or mongodb+srv://). mongodb adapter only; takes precedence over --host/--port/--user. TLS, replicaSet, authSource and credentials are read from the URI.") { |v| @connection_uri = v }
|
data/lib/exwiw/explain_runner.rb
CHANGED
|
@@ -4,13 +4,13 @@ module Exwiw
|
|
|
4
4
|
class ExplainRunner
|
|
5
5
|
def initialize(
|
|
6
6
|
connection_config:,
|
|
7
|
-
|
|
7
|
+
schema_dir:,
|
|
8
8
|
dump_target:,
|
|
9
9
|
logger:,
|
|
10
10
|
io: $stdout
|
|
11
11
|
)
|
|
12
12
|
@connection_config = connection_config
|
|
13
|
-
@
|
|
13
|
+
@schema_dir = schema_dir
|
|
14
14
|
@dump_target = dump_target
|
|
15
15
|
@logger = logger
|
|
16
16
|
@io = io
|
|
@@ -53,7 +53,7 @@ module Exwiw
|
|
|
53
53
|
end
|
|
54
54
|
|
|
55
55
|
private def load_table_config(klass)
|
|
56
|
-
Dir[File.join(@
|
|
56
|
+
Dir[File.join(@schema_dir, "*.json")].map do |file|
|
|
57
57
|
json = JSON.parse(File.read(file))
|
|
58
58
|
klass.from(json).reject_ignored_members!
|
|
59
59
|
end
|
data/lib/exwiw/runner.rb
CHANGED
|
@@ -7,7 +7,7 @@ module Exwiw
|
|
|
7
7
|
def initialize(
|
|
8
8
|
connection_config:,
|
|
9
9
|
output_dir:,
|
|
10
|
-
|
|
10
|
+
schema_dir:,
|
|
11
11
|
dump_target:,
|
|
12
12
|
logger:,
|
|
13
13
|
output_format: 'insert',
|
|
@@ -17,7 +17,7 @@ module Exwiw
|
|
|
17
17
|
)
|
|
18
18
|
@connection_config = connection_config
|
|
19
19
|
@output_dir = output_dir
|
|
20
|
-
@
|
|
20
|
+
@schema_dir = schema_dir
|
|
21
21
|
@dump_target = dump_target
|
|
22
22
|
@output_format = output_format
|
|
23
23
|
@insert_only = insert_only
|
|
@@ -159,7 +159,7 @@ module Exwiw
|
|
|
159
159
|
end
|
|
160
160
|
|
|
161
161
|
private def load_table_config(klass)
|
|
162
|
-
Dir[File.join(@
|
|
162
|
+
Dir[File.join(@schema_dir, "*.json")].map do |file|
|
|
163
163
|
json = JSON.parse(File.read(file))
|
|
164
164
|
# Drop belongs_tos/columns(fields) flagged ignore:true so they are not
|
|
165
165
|
# considered during extraction. Done here (after loading from file)
|
data/lib/exwiw/version.rb
CHANGED
data/lib/tasks/exwiw.rake
CHANGED
|
@@ -7,7 +7,7 @@ namespace :exwiw do
|
|
|
7
7
|
require "exwiw"
|
|
8
8
|
|
|
9
9
|
Exwiw::SchemaGenerator.from_rails_application(
|
|
10
|
-
output_dir: ENV["
|
|
10
|
+
output_dir: ENV["EXWIW_SCHEMA_DIR_PATH"] || "exwiw/schema",
|
|
11
11
|
).generate!
|
|
12
12
|
end
|
|
13
13
|
|
|
@@ -16,7 +16,7 @@ namespace :exwiw do
|
|
|
16
16
|
require "exwiw"
|
|
17
17
|
|
|
18
18
|
result = Exwiw::SchemaGenerator.from_rails_application(
|
|
19
|
-
output_dir: ENV["
|
|
19
|
+
output_dir: ENV["EXWIW_SCHEMA_DIR_PATH"] || "exwiw/schema",
|
|
20
20
|
).tidy!
|
|
21
21
|
|
|
22
22
|
if result.empty?
|
|
@@ -47,7 +47,7 @@ namespace :exwiw do
|
|
|
47
47
|
require "exwiw"
|
|
48
48
|
|
|
49
49
|
Exwiw::MongoidSchemaGenerator.from_rails_application(
|
|
50
|
-
output_dir: ENV["
|
|
50
|
+
output_dir: ENV["EXWIW_SCHEMA_DIR_PATH"] || "exwiw/schema",
|
|
51
51
|
skip_unsupported: ENV["EXWIW_SKIP_UNSUPPORTED"] == "1",
|
|
52
52
|
).generate!
|
|
53
53
|
end
|