exwiw 0.3.0 → 0.3.1
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 -1
- data/README.md +3 -3
- data/lib/exwiw/adapter/mysql2_adapter.rb +8 -6
- data/lib/exwiw/adapter/postgresql_adapter.rb +8 -6
- data/lib/exwiw/adapter/sqlite3_adapter.rb +8 -6
- data/lib/exwiw/belongs_to.rb +4 -3
- data/lib/exwiw/cli.rb +9 -9
- data/lib/exwiw/mongoid_schema_generator.rb +6 -4
- data/lib/exwiw/query_ast.rb +5 -5
- data/lib/exwiw/query_ast_builder.rb +14 -13
- data/lib/exwiw/schema_generator.rb +24 -21
- data/lib/exwiw/table_config.rb +6 -5
- data/lib/exwiw/version.rb +1 -1
- 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: 55e0eecbd5d7117c263f00fb43c36e2bcc31c75eb4b7ef0255402bec2ac108dc
|
|
4
|
+
data.tar.gz: '0913f0804ad33023661b947f88cc86adfeb98ef6b047a93f0ba1f09cea52cec9'
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bb152c10da5489d005660f458ee8cc526a199a15618e41a625694df6d8cca5df623916c14996f03df1ee969b1c51e43b12fc1b7a8676f2754ae01c7211deb251
|
|
7
|
+
data.tar.gz: 55ae136ec956f3a3e15d522e3d71388765ab206911e5662e653522d35e3126fb1d67711051bfd19d8cc2a7457194f84065ecc49dde38d7faaca67fddfb70da1f
|
data/CHANGELOG.md
CHANGED
|
@@ -2,13 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.3.1] - 2026-05-31
|
|
6
|
+
|
|
7
|
+
### Changed
|
|
8
|
+
|
|
9
|
+
- **Breaking:** the `dump` subcommand is renamed to `export` to match the gem name (Export What I Want). Invoke `exwiw export ...` (or omit the subcommand, which now defaults to `export`) instead of `exwiw dump ...`. There is no `dump` alias.
|
|
10
|
+
|
|
5
11
|
## [0.3.0] - 2026-05-31
|
|
6
12
|
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- New `--ids-column=COLUMN` CLI option matches `--ids` against an arbitrary column on the target table instead of its primary key (e.g. `--target-table=users --ids=alice@example.com --ids-column=email`). This is the SQL-adapter (mysql2/postgresql/sqlite3) counterpart of the mongodb `--ids-field`; the two are mutually exclusive and each is rejected by the other adapter family (`--ids-field` is mongodb-only, `--ids-column` is sql-only), mirroring the existing `--target-table` / `--target-collection` split. Related tables are still extracted correctly: rather than propagating `--ids` directly onto foreign keys (which would be wrong when filtering on a non-primary-key column), each foreign key is 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 and direct / indirect / polymorphic relations all extract correctly. Note: if `COLUMN` is itself masked, re-running `delete-*` against an already-imported (masked) dump won't match, so prefer a stable natural key. ([#47](https://github.com/heyinc/exwiw/pull/47))
|
|
16
|
+
|
|
7
17
|
## [0.2.9] - 2026-05-31
|
|
8
18
|
|
|
9
19
|
### Added
|
|
10
20
|
|
|
11
|
-
- New `--ids-field=FIELD` CLI option matches `--ids` against an arbitrary field on the target collection instead of its primary key (e.g. `--target-collection=users --ids=a@example.com --ids-field=email`). Only the target collection's filter changes — downstream foreign-key propagation still keys off the primary key. Unlike the primary-key path, the supplied ids are **not** type-coerced (a custom field's stored type is unknown, so values are passed through as-is).
|
|
21
|
+
- New `--ids-field=FIELD` CLI option matches `--ids` against an arbitrary field on the target collection instead of its primary key (e.g. `--target-collection=users --ids=a@example.com --ids-field=email`). Only the target collection's filter changes — downstream foreign-key propagation still keys off the primary key. Unlike the primary-key path, the supplied ids are **not** type-coerced (a custom field's stored type is unknown, so values are passed through as-is). This flag is **mongodb-only**.
|
|
12
22
|
- New `--target-collection=COLLECTION` CLI option, a mongodb-only alias of `--target-table`. Specifying both, or using `--target-collection` with a non-mongodb adapter, is rejected at validation time.
|
|
13
23
|
- New rake task `exwiw:schema:generate_mongoid` (backed by `Exwiw::MongoidSchemaGenerator`) generates `MongodbCollectionConfig` files by introspecting Mongoid document models — a separate task/class from the ActiveRecord `schema:generate` because the ORMs expose different metadata. It derives the collection name, the `_id` primary key, `fields` (including referenced `belongs_to` foreign keys), `belongs_tos` from referenced `belongs_to` associations, and `embedded_in` from `embedded_in` / `embeds_many` / `embeds_one` associations (each embedded config names its immediate parent collection and `store_as` document key; nested embedding is emitted as a chain — `comments` embedded_in `posts`, `posts` embedded_in `users` — so the adapter can recurse through both array and Hash subdocuments). Regeneration preserves hand-edited `replace_with` / `filter` / `skip` / `bulk_insert_chunk_size`. Polymorphic `belongs_to` is not yet expanded. Models in an inheritance hierarchy whose subclasses share the base's collection (Mongoid STI, `_type` discriminator) collapse into a single config: subclasses are discovered via `descendants` (Mongoid registers only the base in `Mongoid.models`) and every class's `fields` / `belongs_tos` are unioned, so subclass-only fields and associations are preserved. A referenced `belongs_to` declared on an *embedded* document (e.g. `Comment embedded_in :post, belongs_to :author`) is dropped from the embedded config's `belongs_tos` (cross-collection refs from inside embedded subdocuments are unsupported and rejected on load), while its foreign-key column is still kept as an ordinary field. A `has_and_belongs_to_many` association is likewise dropped from `belongs_tos` (its foreign keys are stored as an array field such as `tag_ids`, which exwiw cannot follow as a single-valued foreign key), while that `*_ids` array column is kept as an ordinary field. A *polymorphic* `embedded_in` (`embedded_in :addressable, polymorphic: true`) has no single embedding parent collection and cannot be expressed as an `embedded_in` config, so the generator raises a clear, actionable error rather than crashing on the unresolvable parent class. A *self-referential / cyclic* embedding (Mongoid's `recursively_embeds_many` / `recursively_embeds_one`) makes a collection both top-level and embedded inside documents of its own type; since exwiw represents a collection as either top-level or embedded (not both), the generator likewise raises a clear error rather than emit an `embedded_in` config that would silently make the collection undumpable. The `created_at` / `updated_at` columns added by `include Mongoid::Timestamps` are tracked as ordinary fields, and their BSON `ObjectId` / `Date` values (the shape a live `find` returns) serialize as MongoDB Extended JSON (`$oid` / `$date`) through the dump path — now covered end-to-end against the generated configs. An aliased field (`field :ctry, as: :country`) is emitted by its **stored** document key (`ctry`), never the Ruby accessor (`country`), so masking and projection target the key that actually appears in the document; the accessor is additionally surfaced as `mongoid_field_name` on that field so the otherwise cryptic short key stays understandable (association aliases such as `shop => shop_id` and the built-in `id => _id` are not field renames and are not annotated).
|
|
14
24
|
|
data/README.md
CHANGED
|
@@ -46,10 +46,10 @@ gem install exwiw
|
|
|
46
46
|
|
|
47
47
|
exwiw has two subcommands:
|
|
48
48
|
|
|
49
|
-
- `
|
|
50
|
-
- `explain` — print the compiled SQL and its `EXPLAIN` output for each query that `
|
|
49
|
+
- `export` (default) — generate INSERT/COPY SQL files. If the subcommand is omitted, `export` is assumed.
|
|
50
|
+
- `explain` — print the compiled SQL and its `EXPLAIN` output for each query that `export` would run, without executing the SELECTs.
|
|
51
51
|
|
|
52
|
-
### `exwiw
|
|
52
|
+
### `exwiw export`
|
|
53
53
|
|
|
54
54
|
```bash
|
|
55
55
|
# dump & masking all records from database to dump.sql based on schema.json
|
|
@@ -138,10 +138,11 @@ module Exwiw
|
|
|
138
138
|
subquery_sql = compile_ast(subquery_ast)
|
|
139
139
|
sql += "\nWHERE #{select_query_ast.from_table_name}.#{foreign_key} IN (#{subquery_sql})"
|
|
140
140
|
|
|
141
|
-
# first_join.base_where_clauses
|
|
142
|
-
# (from_table_name)
|
|
143
|
-
#
|
|
144
|
-
#
|
|
141
|
+
# first_join.base_where_clauses holds conditions on the outer
|
|
142
|
+
# delete-target table (from_table_name), such as a polymorphic type
|
|
143
|
+
# column. They are not part of the subquery, so add them to the outer
|
|
144
|
+
# WHERE. This prevents deleting rows that belong to a different
|
|
145
|
+
# polymorphic type.
|
|
145
146
|
first_join.base_where_clauses.each do |where|
|
|
146
147
|
next unless where.is_a?(Exwiw::QueryAst::WhereClause)
|
|
147
148
|
|
|
@@ -171,8 +172,9 @@ module Exwiw
|
|
|
171
172
|
sql += " AND #{compiled_where_condition}"
|
|
172
173
|
end
|
|
173
174
|
|
|
174
|
-
# base_where_clauses
|
|
175
|
-
#
|
|
175
|
+
# base_where_clauses is compiled against the joined-from table
|
|
176
|
+
# (base_table_name), e.g. the type-column filter on a polymorphic
|
|
177
|
+
# source table.
|
|
176
178
|
join.base_where_clauses.each do |where|
|
|
177
179
|
compiled_where_condition = compile_where_condition(where, join.base_table_name)
|
|
178
180
|
sql += " AND #{compiled_where_condition}"
|
|
@@ -180,10 +180,11 @@ module Exwiw
|
|
|
180
180
|
subquery_sql = compile_ast(subquery_ast)
|
|
181
181
|
sql += "\nWHERE #{select_query_ast.from_table_name}.#{foreign_key} IN (#{subquery_sql})"
|
|
182
182
|
|
|
183
|
-
# first_join.base_where_clauses
|
|
184
|
-
# (from_table_name)
|
|
185
|
-
#
|
|
186
|
-
#
|
|
183
|
+
# first_join.base_where_clauses holds conditions on the outer
|
|
184
|
+
# delete-target table (from_table_name), such as a polymorphic type
|
|
185
|
+
# column. They are not part of the subquery, so add them to the outer
|
|
186
|
+
# WHERE. This prevents deleting rows that belong to a different
|
|
187
|
+
# polymorphic type.
|
|
187
188
|
first_join.base_where_clauses.each do |where|
|
|
188
189
|
next unless where.is_a?(Exwiw::QueryAst::WhereClause)
|
|
189
190
|
|
|
@@ -213,8 +214,9 @@ module Exwiw
|
|
|
213
214
|
sql += " AND #{compiled_where_condition}"
|
|
214
215
|
end
|
|
215
216
|
|
|
216
|
-
# base_where_clauses
|
|
217
|
-
#
|
|
217
|
+
# base_where_clauses is compiled against the joined-from table
|
|
218
|
+
# (base_table_name), e.g. the type-column filter on a polymorphic
|
|
219
|
+
# source table.
|
|
218
220
|
join.base_where_clauses.each do |where|
|
|
219
221
|
compiled_where_condition = compile_where_condition(where, join.base_table_name)
|
|
220
222
|
sql += " AND #{compiled_where_condition}"
|
|
@@ -125,10 +125,11 @@ module Exwiw
|
|
|
125
125
|
subquery_sql = compile_ast(subquery_ast)
|
|
126
126
|
sql += "\nWHERE #{select_query_ast.from_table_name}.#{foreign_key} IN (#{subquery_sql})"
|
|
127
127
|
|
|
128
|
-
# first_join.base_where_clauses
|
|
129
|
-
# (from_table_name)
|
|
130
|
-
#
|
|
131
|
-
#
|
|
128
|
+
# first_join.base_where_clauses holds conditions on the outer
|
|
129
|
+
# delete-target table (from_table_name), such as a polymorphic type
|
|
130
|
+
# column. They are not part of the subquery, so add them to the outer
|
|
131
|
+
# WHERE. This prevents deleting rows that belong to a different
|
|
132
|
+
# polymorphic type.
|
|
132
133
|
first_join.base_where_clauses.each do |where|
|
|
133
134
|
next unless where.is_a?(Exwiw::QueryAst::WhereClause)
|
|
134
135
|
|
|
@@ -158,8 +159,9 @@ module Exwiw
|
|
|
158
159
|
sql += " AND #{compiled_where_condition}"
|
|
159
160
|
end
|
|
160
161
|
|
|
161
|
-
# base_where_clauses
|
|
162
|
-
#
|
|
162
|
+
# base_where_clauses is compiled against the joined-from table
|
|
163
|
+
# (base_table_name), e.g. the type-column filter on a polymorphic
|
|
164
|
+
# source table.
|
|
163
165
|
join.base_where_clauses.each do |where|
|
|
164
166
|
compiled_where_condition = compile_where_condition(where, join.base_table_name)
|
|
165
167
|
sql += " AND #{compiled_where_condition}"
|
data/lib/exwiw/belongs_to.rb
CHANGED
|
@@ -6,9 +6,10 @@ module Exwiw
|
|
|
6
6
|
|
|
7
7
|
attribute :foreign_key, String
|
|
8
8
|
attribute :table_name, String
|
|
9
|
-
# polymorphic
|
|
10
|
-
# (
|
|
11
|
-
#
|
|
9
|
+
# Set only for a polymorphic association. `foreign_type` is the name of the
|
|
10
|
+
# column storing the type (e.g. `reviewable_type`), and `type_value` is the
|
|
11
|
+
# value held in that column (e.g. `"Product"`). Both are nil for a
|
|
12
|
+
# non-polymorphic belongs_to.
|
|
12
13
|
attribute :foreign_type, optional(String), skip_serializing_if_nil: true
|
|
13
14
|
attribute :type_value, optional(String), skip_serializing_if_nil: true
|
|
14
15
|
|
data/lib/exwiw/cli.rb
CHANGED
|
@@ -10,7 +10,7 @@ require 'exwiw'
|
|
|
10
10
|
|
|
11
11
|
module Exwiw
|
|
12
12
|
class CLI
|
|
13
|
-
KNOWN_SUBCOMMANDS = %w[
|
|
13
|
+
KNOWN_SUBCOMMANDS = %w[export explain].freeze
|
|
14
14
|
|
|
15
15
|
def self.start(argv)
|
|
16
16
|
new(argv).run
|
|
@@ -23,7 +23,7 @@ module Exwiw
|
|
|
23
23
|
if !@argv.empty? && !@argv.first.start_with?("-") && KNOWN_SUBCOMMANDS.include?(@argv.first)
|
|
24
24
|
@argv.shift
|
|
25
25
|
else
|
|
26
|
-
"
|
|
26
|
+
"export"
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
@help = @argv.empty?
|
|
@@ -75,7 +75,7 @@ module Exwiw
|
|
|
75
75
|
logger = build_logger
|
|
76
76
|
|
|
77
77
|
case @subcommand
|
|
78
|
-
when "
|
|
78
|
+
when "export"
|
|
79
79
|
Runner.new(
|
|
80
80
|
connection_config: connection_config,
|
|
81
81
|
output_dir: @output_dir,
|
|
@@ -132,7 +132,7 @@ module Exwiw
|
|
|
132
132
|
exit 1
|
|
133
133
|
end
|
|
134
134
|
|
|
135
|
-
if @subcommand == "
|
|
135
|
+
if @subcommand == "export"
|
|
136
136
|
@output_dir ||= "dump"
|
|
137
137
|
@output_format ||= "insert"
|
|
138
138
|
@insert_only = @insert_only ? true : false
|
|
@@ -306,7 +306,7 @@ module Exwiw
|
|
|
306
306
|
Usage: exwiw [SUBCOMMAND] [options]
|
|
307
307
|
|
|
308
308
|
Subcommands:
|
|
309
|
-
|
|
309
|
+
export Generate INSERT/COPY SQL files (default when omitted).
|
|
310
310
|
explain Print EXPLAIN output for each extraction query to stdout.
|
|
311
311
|
(not yet supported for the mongodb adapter)
|
|
312
312
|
BANNER
|
|
@@ -315,7 +315,7 @@ module Exwiw
|
|
|
315
315
|
opts.on("-h", "--host=HOST", "Target database host") { |v| @database_host = v }
|
|
316
316
|
opts.on("-p", "--port=PORT", "Target database port") { |v| @database_port = v }
|
|
317
317
|
opts.on("-u", "--user=USERNAME", "Target database user") { |v| @database_user = v }
|
|
318
|
-
opts.on("-o", "--output-dir=[DUMP_DIR_PATH]", "Output file path. default is dump/ (
|
|
318
|
+
opts.on("-o", "--output-dir=[DUMP_DIR_PATH]", "Output file path. default is dump/ (export subcommand only)") do |v|
|
|
319
319
|
v = v.end_with?("/") ? v[0..-2] : v
|
|
320
320
|
@output_dir = File.expand_path(v)
|
|
321
321
|
end
|
|
@@ -330,9 +330,9 @@ module Exwiw
|
|
|
330
330
|
opts.on("--ids=[IDS]", "Comma-separated list of identifiers. Required when --target-table is given.") { |v| @ids = v.split(',') }
|
|
331
331
|
opts.on("--ids-field=[FIELD]", "Field on the target collection that --ids is matched against. Defaults to the primary key. (mongodb adapter only)") { |v| @ids_field = v }
|
|
332
332
|
opts.on("--ids-column=[COLUMN]", "Column on the target table that --ids is matched against. Defaults to the primary key. (sql adapters only)") { |v| @ids_column = v }
|
|
333
|
-
opts.on("--output-format=[FORMAT]", "Output format: insert (default) or copy (PostgreSQL only,
|
|
334
|
-
opts.on("--insert-only", "Do not generate DELETE SQL files (
|
|
335
|
-
opts.on("--after-insert-hook=PATH", "Path to a .rb or .sh post-processing hook executed after all insert/delete files are written (
|
|
333
|
+
opts.on("--output-format=[FORMAT]", "Output format: insert (default) or copy (PostgreSQL only, export subcommand only)") { |v| @output_format = v }
|
|
334
|
+
opts.on("--insert-only", "Do not generate DELETE SQL files (export subcommand only)") { @insert_only = true }
|
|
335
|
+
opts.on("--after-insert-hook=PATH", "Path to a .rb or .sh post-processing hook executed after all insert/delete files are written (export subcommand only)") do |v|
|
|
336
336
|
@after_insert_hook_path = File.expand_path(v)
|
|
337
337
|
end
|
|
338
338
|
opts.on("--log-level=LEVEL", "Log level (debug, info). default is info") { |v| @log_level = v.to_sym }
|
|
@@ -159,11 +159,13 @@ module Exwiw
|
|
|
159
159
|
end
|
|
160
160
|
end
|
|
161
161
|
|
|
162
|
-
# polymorphic belongs_to (`belongs_to :reviewable, polymorphic: true`)
|
|
163
|
-
#
|
|
164
|
-
#
|
|
162
|
+
# A polymorphic belongs_to (`belongs_to :reviewable, polymorphic: true`)
|
|
163
|
+
# has no single target collection, so it is not supported yet. Exclude it
|
|
164
|
+
# here to avoid emitting an incorrect FK (leaving room to expand it later,
|
|
165
|
+
# like the ActiveRecord version does).
|
|
165
166
|
#
|
|
166
|
-
#
|
|
167
|
+
# In an inheritance hierarchy the base class and its subclasses carry the
|
|
168
|
+
# same belongs_to twice, so uniq them.
|
|
167
169
|
belongs_to_assocs
|
|
168
170
|
.reject(&:polymorphic?)
|
|
169
171
|
.map { |assoc| { table_name: assoc.klass.collection_name.to_s, foreign_key: assoc.foreign_key } }
|
data/lib/exwiw/query_ast.rb
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
module Exwiw
|
|
4
4
|
module QueryAst
|
|
5
5
|
class JoinClause
|
|
6
|
-
# `where_clauses`
|
|
7
|
-
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
10
|
-
#
|
|
6
|
+
# `where_clauses` is compiled against this join's join_table_name (the
|
|
7
|
+
# joined-to table). `base_where_clauses`, on the other hand, is compiled
|
|
8
|
+
# against base_table_name (the joined-from table). The latter is used for
|
|
9
|
+
# the case where the source table polymorphically belongs_to the joined-to
|
|
10
|
+
# table and the type column (foreign_type) lives on the source table.
|
|
11
11
|
attr_reader :base_table_name, :foreign_key, :join_table_name, :primary_key, :where_clauses, :base_where_clauses
|
|
12
12
|
|
|
13
13
|
def initialize(base_table_name:, foreign_key:, join_table_name:, primary_key:, where_clauses: [], base_where_clauses: [])
|
|
@@ -58,12 +58,12 @@ module Exwiw
|
|
|
58
58
|
base_where_clauses: []
|
|
59
59
|
)
|
|
60
60
|
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
# (foreign_type)
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
61
|
+
# When this hop itself is a polymorphic belongs_to (e.g. comments
|
|
62
|
+
# polymorphically belongs_to posts as commentable), the type column
|
|
63
|
+
# (foreign_type) lives on the source table (from_table = base_table_name).
|
|
64
|
+
# The foreign key alone is not enough — a value like reviewable_id=1 can
|
|
65
|
+
# collide with rows of another model — so add the type condition to
|
|
66
|
+
# base_where_clauses to narrow down the source table.
|
|
67
67
|
if relation.polymorphic?
|
|
68
68
|
join_clause.base_where_clauses.push QueryAst::WhereClause.new(
|
|
69
69
|
column_name: relation.foreign_type,
|
|
@@ -75,10 +75,11 @@ module Exwiw
|
|
|
75
75
|
if relation_to_dump_target
|
|
76
76
|
join_clause.where_clauses.push dump_target_fk_clause(relation_to_dump_target.foreign_key)
|
|
77
77
|
|
|
78
|
-
#
|
|
79
|
-
#
|
|
80
|
-
# (= join_table_name)
|
|
81
|
-
#
|
|
78
|
+
# When the intermediate table polymorphically belongs_to the dump
|
|
79
|
+
# target, also add the type column (foreign_type) to the join
|
|
80
|
+
# condition. The type column lives on to_table (= join_table_name), so
|
|
81
|
+
# it rides on the existing mechanism where a JoinClause's where_clauses
|
|
82
|
+
# are compiled against join_table_name.
|
|
82
83
|
if relation_to_dump_target.polymorphic?
|
|
83
84
|
join_clause.where_clauses.push QueryAst::WhereClause.new(
|
|
84
85
|
column_name: relation_to_dump_target.foreign_type,
|
|
@@ -121,9 +122,9 @@ module Exwiw
|
|
|
121
122
|
|
|
122
123
|
clauses.push dump_target_fk_clause(belongs_to.foreign_key)
|
|
123
124
|
|
|
124
|
-
# polymorphic belongs_to
|
|
125
|
-
# (
|
|
126
|
-
#
|
|
125
|
+
# For a polymorphic belongs_to the foreign key alone cannot distinguish the
|
|
126
|
+
# type (e.g. reviewable_id=1 could be a Product or another model), so add a
|
|
127
|
+
# condition filtering the type column (foreign_type) by type_value.
|
|
127
128
|
if belongs_to.polymorphic?
|
|
128
129
|
clauses.push Exwiw::QueryAst::WhereClause.new(
|
|
129
130
|
column_name: belongs_to.foreign_type,
|
|
@@ -78,11 +78,12 @@ module Exwiw
|
|
|
78
78
|
representative = model_group.first
|
|
79
79
|
primary_key = representative.primary_key
|
|
80
80
|
|
|
81
|
-
#
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
81
|
+
# Tables with a composite primary key (`representative.primary_key` is an
|
|
82
|
+
# Array) are not supported yet. Emit them with `primary_key` omitted,
|
|
83
|
+
# `skip: true`, and a `type` that marks them as unsupported — the `type`
|
|
84
|
+
# acts as a signpost for adding support later. The config file itself is
|
|
85
|
+
# still generated so a user can manually remove `skip` and wire it up when
|
|
86
|
+
# needed.
|
|
86
87
|
if primary_key.is_a?(Array)
|
|
87
88
|
TableConfig.from_symbol_keys(
|
|
88
89
|
name: table_name,
|
|
@@ -110,12 +111,13 @@ module Exwiw
|
|
|
110
111
|
@models.reject(&:abstract_class?).select(&:table_exists?)
|
|
111
112
|
end
|
|
112
113
|
|
|
113
|
-
# rails-managed
|
|
114
|
-
#
|
|
115
|
-
# multi-DB
|
|
116
|
-
#
|
|
117
|
-
#
|
|
118
|
-
# (
|
|
114
|
+
# The rails-managed tables (`schema_migrations` / `ar_internal_metadata`)
|
|
115
|
+
# have no model class, so they cannot be picked up from
|
|
116
|
+
# `ActiveRecord::Base.descendants`. In a multi-DB setup each connection has
|
|
117
|
+
# its own migration history table, so we take the target connection and only
|
|
118
|
+
# emit an entry when the table actually exists on that connection. The table
|
|
119
|
+
# name itself (including any prefix/suffix) comes from the global settings
|
|
120
|
+
# (`ActiveRecord::Base.schema_migrations_table_name`, etc.).
|
|
119
121
|
private def build_rails_managed_tables(conn)
|
|
120
122
|
result = []
|
|
121
123
|
|
|
@@ -151,11 +153,11 @@ module Exwiw
|
|
|
151
153
|
.reject(&:polymorphic?)
|
|
152
154
|
.map { |assoc| { table_name: assoc.table_name, foreign_key: assoc.foreign_key } }
|
|
153
155
|
|
|
154
|
-
# polymorphic
|
|
155
|
-
#
|
|
156
|
-
# `has_many/has_one ..., as: <association_name
|
|
157
|
-
#
|
|
158
|
-
# (`
|
|
156
|
+
# A polymorphic belongs_to (`belongs_to :reviewable, polymorphic: true`)
|
|
157
|
+
# has no single target table. The candidate tables are found by looking up
|
|
158
|
+
# the other models that declare `has_many/has_one ..., as: <association_name>`.
|
|
159
|
+
# For each candidate table, expand one belongs_to entry carrying the type
|
|
160
|
+
# column (`foreign_type`) and the stored type value (`type_value`).
|
|
159
161
|
polymorphic = belongs_to_assocs
|
|
160
162
|
.select(&:polymorphic?)
|
|
161
163
|
.flat_map do |assoc|
|
|
@@ -172,11 +174,12 @@ module Exwiw
|
|
|
172
174
|
(non_polymorphic + polymorphic).uniq
|
|
173
175
|
end
|
|
174
176
|
|
|
175
|
-
#
|
|
176
|
-
# `
|
|
177
|
-
# `
|
|
178
|
-
# Ruby
|
|
179
|
-
#
|
|
177
|
+
# Enumerate the concrete models that can be targets of the polymorphic
|
|
178
|
+
# association `association_name`, by looking them up from every model's
|
|
179
|
+
# `has_many` / `has_one` `as:` option. The order of `concrete_models` depends
|
|
180
|
+
# on `ActiveRecord::Base.descendants`, which can vary by Ruby version, so sort
|
|
181
|
+
# by `table_name` to return a deterministic order and keep the generated
|
|
182
|
+
# belongs_to ordering stable.
|
|
180
183
|
private def polymorphic_target_models(association_name)
|
|
181
184
|
concrete_models.select do |model|
|
|
182
185
|
(model.reflect_on_all_associations(:has_many) +
|
data/lib/exwiw/table_config.rb
CHANGED
|
@@ -11,9 +11,10 @@ module Exwiw
|
|
|
11
11
|
RAILS_MANAGED_INTERNAL_METADATA,
|
|
12
12
|
].freeze
|
|
13
13
|
|
|
14
|
-
# exwiw
|
|
15
|
-
# schema:generate
|
|
16
|
-
#
|
|
14
|
+
# type marking a table with a composite primary key, which exwiw does not
|
|
15
|
+
# support yet. schema:generate attaches it together with skip:true. Unlike
|
|
16
|
+
# rails-managed tables, columns/belongs_tos are retained so it can serve as a
|
|
17
|
+
# signpost for adding support later.
|
|
17
18
|
UNSUPPORTED_COMPOSITE_PRIMARY_KEY = "unsupported_composite_primary_key"
|
|
18
19
|
|
|
19
20
|
attribute :name, String
|
|
@@ -143,8 +144,8 @@ module Exwiw
|
|
|
143
144
|
"Table '#{name}' has type=#{type}; columns must not be defined."
|
|
144
145
|
end
|
|
145
146
|
else
|
|
146
|
-
# skip:true
|
|
147
|
-
# (
|
|
147
|
+
# A skip:true table is not extracted, so primary_key is not required
|
|
148
|
+
# (e.g. a composite-primary-key table that exwiw does not support).
|
|
148
149
|
if primary_key.nil? && !skip
|
|
149
150
|
raise ArgumentError, "Table '#{name}' requires primary_key."
|
|
150
151
|
end
|
data/lib/exwiw/version.rb
CHANGED