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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e62483afc43054ae026c232502f40f6c1a92381a9c2c833ef1f7d603f6b845ae
4
- data.tar.gz: 660a006272b9156bbc98af2a0c288f7943bdd40523adffc45de713b36a5d0426
3
+ metadata.gz: 55e0eecbd5d7117c263f00fb43c36e2bcc31c75eb4b7ef0255402bec2ac108dc
4
+ data.tar.gz: '0913f0804ad33023661b947f88cc86adfeb98ef6b047a93f0ba1f09cea52cec9'
5
5
  SHA512:
6
- metadata.gz: 988813923c5e8a2aa1fd499c3af3a2d5467fc9a77b60664b1cd8d245ed1837a1cf43dc88a872bbbcfb7951902624e6b61fb50d290af6716a06c1fe8bb376ae6a
7
- data.tar.gz: d8f751b794c20658c643ab2aeca769b01f7b50e9acc46c9605a4d7121d0d5fa8041bfbdad8021ede6bbfefc8cb36a937509120f25b7bdd059aaf6989f4c94e00
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). Currently **mongodb-only**: the SQL adapters (mysql2/postgresql/sqlite3) reject the flag at validation time, and threading `ids_field` through `QueryAstBuilder` for them is left as a TODO.
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
- - `dump` (default) — generate INSERT/COPY SQL files. This is the existing behavior; if the subcommand is omitted, `dump` is assumed for backwards compatibility.
50
- - `explain` — print the compiled SQL and its `EXPLAIN` output for each query that `dump` would run, without executing the SELECTs.
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 dump`
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) 上の条件 (polymorphic 型カラム等)。subquery には
143
- # 含まれないため、外側の WHERE に追加する。これにより、別の
144
- # polymorphic 型に属する行まで削除してしまうのを防ぐ。
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 は結合元テーブル (base_table_name) に対して
175
- # コンパイルする。polymorphic な結合元テーブルの型カラム絞り込み等。
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) 上の条件 (polymorphic 型カラム等)。subquery には
185
- # 含まれないため、外側の WHERE に追加する。これにより、別の
186
- # polymorphic 型に属する行まで削除してしまうのを防ぐ。
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 は結合元テーブル (base_table_name) に対して
217
- # コンパイルする。polymorphic な結合元テーブルの型カラム絞り込み等。
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) 上の条件 (polymorphic 型カラム等)。subquery には
130
- # 含まれないため、外側の WHERE に追加する。これにより、別の
131
- # polymorphic 型に属する行まで削除してしまうのを防ぐ。
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 は結合元テーブル (base_table_name) に対して
162
- # コンパイルする。polymorphic な結合元テーブルの型カラム絞り込み等。
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}"
@@ -6,9 +6,10 @@ module Exwiw
6
6
 
7
7
  attribute :foreign_key, String
8
8
  attribute :table_name, String
9
- # polymorphic 関連の場合のみ設定される。`foreign_type` は型を格納するカラム名
10
- # (例: `reviewable_type`)、`type_value` はそのカラムに入る値 (例: `"Product"`)。
11
- # polymorphic belongs_to では両方とも nil
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[dump explain].freeze
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
- "dump"
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 "dump"
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 == "dump"
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
- dump Generate INSERT/COPY SQL files (default when omitted).
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/ (dump subcommand only)") do |v|
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, dump subcommand only)") { |v| @output_format = v }
334
- opts.on("--insert-only", "Do not generate DELETE SQL files (dump 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 (dump subcommand only)") do |v|
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
- # 単一の対象コレクションを持たないため現状未対応。誤った FK を出力しないよう
164
- # ここでは除外する (将来 ActiveRecord 版と同様に展開する余地を残す)。
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
- # 継承階層では基底クラスとサブクラスが同じ belongs_to を二重に持つため uniq する。
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 } }
@@ -3,11 +3,11 @@
3
3
  module Exwiw
4
4
  module QueryAst
5
5
  class JoinClause
6
- # `where_clauses` はこの join join_table_name (= 結合先テーブル) に対して
7
- # コンパイルされる。一方 `base_where_clauses` base_table_name (= 結合元
8
- # テーブル) に対してコンパイルされる。後者は、結合元テーブルが結合先へ
9
- # polymorphic belongs_to していて型カラム (foreign_type) が結合元テーブル
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
- # この hop 自体が polymorphic belongs_to の場合 (例: comments
62
- # commentable として posts polymorphic belongs_to)、型カラム
63
- # (foreign_type) は結合元テーブル (from_table = base_table_name) 側に
64
- # 存在する。外部キーだけでは reviewable_id=1 のような値が別モデルの
65
- # 行と衝突しうるため、base_where_clauses に型条件を追加して結合元
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
- # 中間テーブルが dump target polymorphic belongs_to している場合は、
79
- # 型カラム (foreign_type) join 条件に追加する。型カラムは to_table
80
- # (= join_table_name) 上に存在するため、JoinClause の where_clauses が
81
- # join_table_name に対してコンパイルされる仕組みにそのまま乗せられる。
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
- # (例: reviewable_id=1 Product なのか別モデルなのか判別できない)
126
- # 型カラム (foreign_type) type_value で絞り込む条件を追加する。
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
- # 複合主キー (`representative.primary_key` Array) のテーブルは現状未対応。
82
- # primary_key を省略し、type で非対応である旨を明示したうえで skip:true
83
- # 付与して出力する。type を付けておくことで将来対応する際の目印になる。
84
- # 利用者が必要に応じて手動で skip を外して設定し直せるよう、設定ファイル
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 テーブル (`schema_migrations` / `ar_internal_metadata`)
114
- # モデルクラスを持たないため `ActiveRecord::Base.descendants` からは拾えない。
115
- # multi-DB 構成では各 connection が独立した migration 履歴テーブルを持つので、
116
- # 対象 connection を受け取り、その connection 上に該当テーブルが存在する場合のみ
117
- # エントリを生成する。テーブル名そのものは prefix/suffix を含むグローバル設定
118
- # (`ActiveRecord::Base.schema_migrations_table_name` ) から得る。
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 belongs_to (`belongs_to :reviewable, polymorphic: true`)
155
- # 単一の対象テーブルを持たない。対象になりうるテーブルは、他モデルで
156
- # `has_many/has_one ..., as: <association_name>` と宣言されている側から逆引き
157
- # する。各候補テーブルごとに、型カラム (`foreign_type`) と格納される型の値
158
- # (`type_value`) を添えた belongs_to 1 件ずつ展開する。
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
- # polymorphic 関連 `association_name` の対象となりうる具象モデルを、全モデルの
176
- # `has_many` / `has_one` `as:` オプションから逆引きして列挙する。
177
- # `concrete_models` の並びは `ActiveRecord::Base.descendants` の順に依存し、
178
- # Ruby バージョンによって変わりうるため、生成される belongs_to の並びが安定する
179
- # よう `table_name` でソートして決定的に返す。
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) +
@@ -11,9 +11,10 @@ module Exwiw
11
11
  RAILS_MANAGED_INTERNAL_METADATA,
12
12
  ].freeze
13
13
 
14
- # exwiw が現状サポートしていない複合主キーのテーブルを表す type。
15
- # schema:generate skip:true と併せて付与する。将来サポートする際の
16
- # 目印になるよう、rails-managed とは異なり columns/belongs_tos は保持する。
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 のテーブルはデータ抽出を行わないため primary_key を要求しない。
147
- # (例: exwiw 非対応の複合主キーテーブル)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Exwiw
4
- VERSION = "0.3.0"
4
+ VERSION = "0.3.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: exwiw
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shia