schema_reaper 1.0.15 → 1.0.16

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: 6b327fade324c6f2e9a871da57ae46814c1b077a1aca3aec64ca6681f270ffe6
4
- data.tar.gz: d63156d86d7dc44e92204475c3d23ffa7e6b87730d0a436dc9aefd041ea8b843
3
+ metadata.gz: df6a3127bc5e379e183a6f525870c5726bbf016223b6c8531b1be8cb97da8ca5
4
+ data.tar.gz: 2810a69e8b382c10ecf52bc35717efd53fc9b826b8aae580b15773f4ffb8e246
5
5
  SHA512:
6
- metadata.gz: cb3fe82c18f9f198ec544089305b183f2a413123093edd6f437a04c8d0b32879c123d1e4d1c338a1e03539a8eb16847406bb21f4eab5932ed6b4aba4fcdf461e
7
- data.tar.gz: 1c1cf99e43ff66383f5b3bb0c42d5cfec209cbc8dab3bb986e734b6dcf75706c5ed1ecbf09d61f116ce2883957630f0ceeff7a102fb49793fe73cd93cd8283ad
6
+ metadata.gz: f4dc2789fcdd897e213a4b050ef98a05d94f6c213e145bdc6ac6e8a5168d355b7fc86a2b18aa0799b76ff1183255e64b5d6bbc2789c9d5a063dac91c9fcabdf9
7
+ data.tar.gz: f8026dcfb27f4a9a828f519bef9a78f8e626522f8cadb5082ee39b393652d5ff0224a669fc8a4b7d109f20f5b6bc910bcea336971de0603a85af4d1c4ad2280a
data/.rubocop.yml CHANGED
@@ -44,7 +44,7 @@ Metrics/MethodLength:
44
44
  Max: 20
45
45
 
46
46
  Metrics/ClassLength:
47
- Max: 130
47
+ Max: 135
48
48
 
49
49
  Metrics/CyclomaticComplexity:
50
50
  Max: 12
data/CHANGELOG.md CHANGED
@@ -1,5 +1,45 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.0.16] - 2026-09-22
4
+
5
+ ### Fixed
6
+ - **A bare index on a polymorphic `*_id` column alone silently suppressed the
7
+ `missing_fk_index` finding it should have raised.** The check tested the
8
+ generic "is this column indexed at all" before checking whether the column
9
+ was part of a polymorphic pair, so an index on `commentable_id` by itself
10
+ — never sufficient, since Rails always queries the pair together — read as
11
+ "covered" and the analyzer's own doc comment went unenforced. Now gates on
12
+ the composite `(type, id)` check whenever a `*_type` column is present.
13
+ (#17, mitkush)
14
+ - **`Postgres#indexes_for` was the one introspection query missing the
15
+ `public`-schema filter** every sibling query (`columns_for`,
16
+ `foreign_keys_for`, `table_names`) already has. In a database with more
17
+ than one schema containing a same-named table, it could pull in and union
18
+ indexes from the wrong table. Verified live against a two-schema database
19
+ with a colliding table name and a planted bogus index. (#17, mitkush)
20
+ - **The `SCHEMA_REAPER_TRACK=1` runtime-tracker initializer could crash a
21
+ host app's entire boot**, not just disable the optional feature, on a
22
+ malformed `.schema_reaper.yml` or an unwritable log directory — nothing
23
+ in the initializer was rescued. Now rescues and logs a warning instead.
24
+ (#18, mitkush)
25
+ - **`schema_reaper scan --format json` could not distinguish a genuinely
26
+ zero-byte reclaim estimate from an unmeasured one.** `Finding#to_h` read
27
+ the zero-defaulted accessor instead of the raw value, silently flattening
28
+ "unknown" into `0` for every CI/tooling consumer of the JSON output, even
29
+ though every other reporter already preserves that distinction. The
30
+ payload now carries the raw value plus an explicit `reclaim_known` flag.
31
+ Verified live: an unmeasured finding now serializes `reclaimable_bytes`
32
+ as `null` with `reclaim_known: false`. (#18, mitkush)
33
+ - **A query-time Postgres error (anything past the initial connection)
34
+ propagated as a raw `PG::Error`** instead of the wrapped `SchemaReaper::Error`
35
+ every other failure path produces, leaking a Ruby backtrace instead of a
36
+ clean CLI error message. `primary_key_for`'s existing fallback-to-nil
37
+ rescue is updated to match. (#18, mitkush)
38
+ - Cosmetic: `MigrationGenerator#model` mangled irregular plural table names
39
+ (`addresses` → "Addresse") in the generated migration's comment text;
40
+ `Reporters::Trend#bytes` printed `-0.0 B` instead of `+0.0 B` for a zero
41
+ delta. (#17, mitkush)
42
+
3
43
  ## [1.0.15] - 2026-09-18
4
44
 
5
45
  ### Fixed
@@ -32,11 +32,15 @@ module SchemaReaper
32
32
 
33
33
  def missing_in(table)
34
34
  fk_columns(table).filter_map do |col|
35
- next if indexed?(table, col)
36
- next if empty_column?(table, col) # never advise indexing a column with no data
37
-
38
35
  type_col = polymorphic_type_for(table, col)
39
- next if type_col && polymorphic_indexed?(table, type_col, col)
36
+
37
+ # A bare index on the id alone doesn't cover a polymorphic pair --
38
+ # only the composite (type, id) index satisfies it. Check that
39
+ # instead of the generic `indexed?`, or a bare id-only index would
40
+ # wrongly suppress this finding.
41
+ covered = type_col ? polymorphic_indexed?(table, type_col, col) : indexed?(table, col)
42
+ next if covered
43
+ next if empty_column?(table, col) # never advise indexing a column with no data
40
44
 
41
45
  finding_for(table, col, type_col, declared: table.foreign_keys.include?(col))
42
46
  end
@@ -50,8 +50,12 @@ module SchemaReaper
50
50
  parts.empty? ? nil : parts.join(" · ")
51
51
  end
52
52
 
53
+ # Keeps the raw (possibly nil) reclaimable_bytes from Struct#to_h rather
54
+ # than the zero-defaulted accessor above, so JSON/SARIF consumers can tell
55
+ # "genuinely zero" apart from "unknown" the same way every other reporter
56
+ # does -- reclaim_known? makes that distinction explicit in the payload.
53
57
  def to_h
54
- super.merge(id: id, reclaimable_bytes: reclaimable_bytes)
58
+ super.merge(id: id, reclaim_known: reclaim_known?)
55
59
  end
56
60
  end
57
61
  end
@@ -8,13 +8,12 @@ module SchemaReaper
8
8
  AVG_TYPE_BYTES = {
9
9
  "boolean" => 1, "smallint" => 2, "integer" => 4, "bigint" => 8,
10
10
  "real" => 4, "double precision" => 8, "numeric" => 8,
11
- "date" => 4, "timestamp without time zone" => 8,
12
- "timestamp with time zone" => 8, "uuid" => 16
11
+ "date" => 4, "timestamp without time zone" => 8, "timestamp with time zone" => 8, "uuid" => 16
13
12
  }.freeze
14
13
 
15
14
  NO_URL = "no database connection found. schema_reaper looks, in order, for: " \
16
- "database_url: in .schema_reaper.yml; the DATABASE_URL env var; " \
17
- "config/database.yml for RAILS_ENV (default: development, Postgres only)."
15
+ "database_url: in .schema_reaper.yml; the DATABASE_URL env var; config/database.yml " \
16
+ "for RAILS_ENV (default: development, Postgres only)."
18
17
 
19
18
  # A record separator that cannot appear inside a plain identifier and is
20
19
  # exceedingly unlikely inside an expression, so splitting the aggregated
@@ -119,7 +118,7 @@ module SchemaReaper
119
118
  JOIN pg_class i ON i.oid = ix.indexrelid
120
119
  JOIN LATERAL generate_series(1, ix.indnkeyatts) AS k(ord) ON TRUE
121
120
  LEFT JOIN pg_stat_user_indexes s ON s.indexrelid = i.oid
122
- WHERE t.relname = $1
121
+ WHERE t.relname = $1 AND t.relnamespace = 'public'::regnamespace
123
122
  GROUP BY i.relname, ix.indisunique, ix.indisprimary, ix.indpred, s.idx_scan
124
123
  SQL
125
124
  Index.new(
@@ -142,7 +141,7 @@ module SchemaReaper
142
141
  WHERE i.indrelid = $1::regclass AND i.indisprimary
143
142
  ORDER BY k.ord
144
143
  SQL
145
- rescue PG::Error
144
+ rescue Error, PG::Error
146
145
  nil
147
146
  end
148
147
 
@@ -172,6 +171,8 @@ module SchemaReaper
172
171
 
173
172
  def exec(sql, params = nil)
174
173
  (params ? @conn.exec_params(sql, params) : @conn.exec(sql)).to_a
174
+ rescue PG::Error => e
175
+ raise Error, "query against the database failed: #{e.message.strip}"
175
176
  end
176
177
  end
177
178
  end
@@ -5,6 +5,14 @@ require "fileutils"
5
5
  module SchemaReaper
6
6
  # Emits a two-step, reversible migration pair for a dead column.
7
7
  class MigrationGenerator
8
+ # Enough of Rails' inflector for table names in this comment text -- a
9
+ # wrong guess doesn't break anything functional, just reads oddly.
10
+ SINGULAR_RULES = [
11
+ [/ies\z/, "y"], # activities -> activity
12
+ [/(ss|sh|ch|x|z)es\z/, '\1'], # addresses -> address, boxes -> box
13
+ [/s\z/, ""] # employees -> employee
14
+ ].freeze
15
+
8
16
  def initialize(table:, column:, dir: "db/migrate")
9
17
  @table = table
10
18
  @column = column
@@ -77,7 +85,12 @@ module SchemaReaper
77
85
  end
78
86
 
79
87
  def model
80
- @table.split("_").map(&:capitalize).join.sub(/s$/, "")
88
+ singular.split("_").map(&:capitalize).join
89
+ end
90
+
91
+ def singular
92
+ rule = SINGULAR_RULES.find { |pattern, _| @table.match?(pattern) }
93
+ rule ? @table.sub(rule[0], rule[1]) : @table
81
94
  end
82
95
  end
83
96
  end
@@ -12,10 +12,17 @@ module SchemaReaper
12
12
  initializer "schema_reaper.runtime_tracker" do
13
13
  next unless ENV["SCHEMA_REAPER_TRACK"] == "1"
14
14
 
15
- config = SchemaReaper::Config.load
16
- store = SchemaReaper::Runtime::Store.new(path: config.runtime_log)
17
- rate = (ENV["SCHEMA_REAPER_SAMPLE"] || "0.05").to_f
18
- SchemaReaper::Runtime::Tracker.install!(store: store, sample_rate: rate)
15
+ begin
16
+ config = SchemaReaper::Config.load
17
+ store = SchemaReaper::Runtime::Store.new(path: config.runtime_log)
18
+ rate = (ENV["SCHEMA_REAPER_SAMPLE"] || "0.05").to_f
19
+ SchemaReaper::Runtime::Tracker.install!(store: store, sample_rate: rate)
20
+ rescue StandardError => e
21
+ # Optional instrumentation must never take the host app down with it --
22
+ # a bad config file or an unwritable log directory disables tracking,
23
+ # it does not abort boot.
24
+ Rails.logger&.warn("[schema_reaper] runtime tracker disabled: #{e.class}: #{e.message}")
25
+ end
19
26
  end
20
27
  end
21
28
  end
@@ -47,7 +47,7 @@ module SchemaReaper
47
47
 
48
48
  def bytes
49
49
  delta = @d[:bytes_change_total].to_i
50
- sign = delta.positive? ? "+" : "-"
50
+ sign = delta.negative? ? "-" : "+"
51
51
  @c.row("reclaimable", Bytes.human(@d.fetch(:latest_bytes, 0)),
52
52
  "#{sign}#{Bytes.human(delta.abs)} since first run")
53
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SchemaReaper
4
- VERSION = "1.0.15"
4
+ VERSION = "1.0.16"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: schema_reaper
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.15
4
+ version: 1.0.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - aksshatt
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2026-09-18 00:00:00.000000000 Z
12
+ date: 2026-09-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prism