railbow 0.8.0 → 0.8.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: 9ec5f893665ab78ed905fe1ca38169a82c466177db5db58f3ced482451e85b48
4
- data.tar.gz: 9854f71484e1268819bd07947d7a07bd94e29df677a0cf57a363d392457c7730
3
+ metadata.gz: 713d22cda25eb21afdfb4ac61e708e969afbc280c18512524e84a1f40a1c72ee
4
+ data.tar.gz: 17129635e6ee556a82f60c2089892de13f9bc26833db3f8ad932056f572d943c
5
5
  SHA512:
6
- metadata.gz: 1bafaeae1f304d088b5013349b927d36d11dc35f6ce18e80cb34e9e5deb4bf09a1b0f8ea26f2baa3545ce0f398a2fd1379a4ae3bdcb03209e3aace8517af7fa5
7
- data.tar.gz: 5e32f6c1741bdfa49ae85c3daf9554c6bab161c4be42ec71ff1aee7c950c8303e5214db9290fae756d0542065939a1d0c776790d7b510fa8327197021eaff5bd
6
+ metadata.gz: 78029054749a650f9ac6317cc9bddd164c16ae75f5f8db65805731ba8431abe50750feb5113e120226bf60a114b80596afa1d41b6caef518e42180787b31f132
7
+ data.tar.gz: 77dd5665755697e5f8008300d65385adb0ca65f21aa916b5ace0aa216e43953cacf19248d9c4e1683a50cb09135795caea69177c74f4d8db498948f4d1dbf094
data/CHANGELOG.md CHANGED
@@ -5,6 +5,25 @@ All notable changes to this project will be documented in this file.
5
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
6
  and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
7
 
8
+ ## [0.8.1] - 2026-11-11
9
+
10
+ ### Fixed
11
+
12
+ - The `⎇` branch badge in `db:migrate:status` no longer appears on a
13
+ base-branch migration while a rename of it is staged, for example one
14
+ renamed back to its original timestamp after a merge. An uncommitted new
15
+ file, staged or not, now counts as the branch's only when the merge-base
16
+ does not already hold it, so the uncommitted state and the committed state
17
+ agree. The same rule keeps such a migration out of a summarized database's
18
+ "on this branch" table.
19
+ - The Tables column shows a schema-qualified table as `reporting.events`
20
+ instead of just the schema, in the migration DSL (single-quoted strings
21
+ too) and in raw SQL, quoted parts included. A table in `public` shows by its bare name.
22
+ - Table detection now reads `CREATE INDEX ... ON [ONLY] <table>`,
23
+ `CREATE [MATERIALIZED] VIEW <name>` and `ALTER TABLE [IF EXISTS] [ONLY]
24
+ <table>` in raw SQL, so a migration made only of those no longer shows an
25
+ empty Tables column.
26
+
8
27
  ## [0.8.0] - 2026-11-11
9
28
 
10
29
  ### Added
data/README.md CHANGED
@@ -103,7 +103,7 @@ Out of the box you get:
103
103
  - **Created At** - timestamp parsed from the migration ID
104
104
  - **Landing dates** - `⤻ Mar 04` badge when a migration was merged to main after its creation
105
105
  - **Branch badges** - `⎇ PS-142` showing the source branch/ticket
106
- - **Affected tables** - color-coded table names extracted from migration files
106
+ - **Affected tables** - color-coded table names extracted from migration files (a schema-qualified one shows as `schema.table`)
107
107
  - **Time filtering** - only the last 90 days shown by default (`since: 90d`), as a soft limit: never fewer than 10 migrations, however old (`since_min: 10`)
108
108
  - **Your migrations highlighted** - rows authored by you are visually distinct
109
109
  - **Pending migrations greyed out** - a `down` row keeps its status glyph but loses its colors
@@ -18,26 +18,47 @@ module Railbow
18
18
  add_foreign_key remove_foreign_key
19
19
  ].freeze
20
20
 
21
+ # A table name with an optional schema: `events` or `reporting.events`.
22
+ QUALIFIED_NAME = '(?:\w+\.)?\w+'
23
+
24
+ # A table argument: a symbol, or a string that may carry a schema. Two
25
+ # captures, one of them nil.
26
+ TABLE_ARG = /(?::(\w+)|["'](#{QUALIFIED_NAME}))/
27
+
21
28
  # Dual-table methods are included here too: their first argument is always a
22
29
  # table, even when the second is passed as `to_table:` instead of positionally.
23
- SINGLE_TABLE_PATTERN = /\b(?:#{(SINGLE_TABLE_METHODS + DUAL_TABLE_METHODS).join("|")})\s+[:"](\w+)/
24
- DUAL_TABLE_PATTERN = /\b(?:#{DUAL_TABLE_METHODS.join("|")})\s+[:"](\w+)["\s,]+[:"](\w+)/
30
+ SINGLE_TABLE_PATTERN = /\b(?:#{(SINGLE_TABLE_METHODS + DUAL_TABLE_METHODS).join("|")})\s+#{TABLE_ARG}/
31
+ DUAL_TABLE_PATTERN = /\b(?:#{DUAL_TABLE_METHODS.join("|")})\s+#{TABLE_ARG}["'\s,]+#{TABLE_ARG}/
32
+
33
+ # One part of a SQL name, quoted or bare. The backslash is the one a Ruby
34
+ # string puts before an inner quote. A closing quote is only taken with its
35
+ # opening one, so `"... FROM events".squish` does not read as a schema.
36
+ SQL_NAME_PART = /(?:\\?["'`]\w+\\?["'`]|\\?["'`]?\w+)/
37
+ SQL_TABLE = /(#{SQL_NAME_PART}(?:\.#{SQL_NAME_PART})?)/
38
+ SQL_QUOTING = "\\\\\"'`"
25
39
 
26
40
  SQL_TABLE_PATTERNS = [
27
- /\bUPDATE\s+["']?(\w+)["']?/i,
28
- /\bINSERT\s+INTO\s+["']?(\w+)["']?/i,
29
- /\bDELETE\s+FROM\s+["']?(\w+)["']?/i,
30
- /\bALTER\s+TABLE\s+["']?(\w+)["']?/i,
31
- /\bTRUNCATE\s+(?:TABLE\s+)?["']?(\w+)["']?/i,
32
- /\bDROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?["']?(\w+)["']?/i,
33
- /\bCREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?["']?(\w+)["']?/i
41
+ /\bUPDATE\s+#{SQL_TABLE}/i,
42
+ /\bINSERT\s+INTO\s+#{SQL_TABLE}/i,
43
+ /\bDELETE\s+FROM\s+#{SQL_TABLE}/i,
44
+ /\bALTER\s+TABLE\s+(?:IF\s+EXISTS\s+)?(?:ONLY\s+)?#{SQL_TABLE}/i,
45
+ /\bTRUNCATE\s+(?:TABLE\s+)?#{SQL_TABLE}/i,
46
+ /\bDROP\s+TABLE\s+(?:IF\s+EXISTS\s+)?#{SQL_TABLE}/i,
47
+ /\bCREATE\s+TABLE\s+(?:IF\s+NOT\s+EXISTS\s+)?#{SQL_TABLE}/i,
48
+ # The index name is optional in Postgres, hence the optional word before ON.
49
+ /\bCREATE\s+(?:UNIQUE\s+)?INDEX\s+(?:CONCURRENTLY\s+)?(?:IF\s+NOT\s+EXISTS\s+)?(?:\S+\s+)?ON\s+(?:ONLY\s+)?#{SQL_TABLE}/i,
50
+ /\bCREATE\s+(?:OR\s+REPLACE\s+)?(?:TEMP(?:ORARY)?\s+)?(?:MATERIALIZED\s+)?VIEW\s+(?:IF\s+NOT\s+EXISTS\s+)?#{SQL_TABLE}/i
34
51
  ].freeze
35
52
 
36
53
  # A table named by keyword argument, which catches project-level helpers
37
54
  # such as `convert_to_monthly_partitions(table: :apples)` whatever their
38
55
  # name. Not bare `from:`/`to:`: `change_column_default :users, :status,
39
- # from: nil, to: "active"` would yield phantom tables.
40
- KEYWORD_TABLE_PATTERN = /\b(?:table|to_table|from_table):\s*[:"']?(\w+)/
56
+ # from: nil, to: "active"` would yield phantom tables. Only a quoted value
57
+ # may carry a schema, so `table: apples.to_s` still reads as `apples`.
58
+ KEYWORD_TABLE_PATTERN = /\b(?:table|to_table|from_table):\s*(?:["'](#{QUALIFIED_NAME})|:?(\w+))/
59
+
60
+ # Postgres resolves a bare name to this schema, so the prefix says nothing.
61
+ DEFAULT_SCHEMA_PREFIX = "public."
41
62
 
42
63
  # Ruby `#` comments up to end of line, but not `#{}` interpolation.
43
64
  COMMENT_PATTERN = /#(?!\{).*/
@@ -75,12 +96,13 @@ module Railbow
75
96
 
76
97
  tables = []
77
98
 
78
- content.scan(SINGLE_TABLE_PATTERN) { |match| tables << match[0] }
79
- content.scan(DUAL_TABLE_PATTERN) { |match| tables.concat(match) }
99
+ content.scan(SINGLE_TABLE_PATTERN) { |match| tables.concat(match.compact) }
100
+ content.scan(DUAL_TABLE_PATTERN) { |match| tables.concat(match.compact) }
80
101
  SQL_TABLE_PATTERNS.each do |pattern|
81
- content.scan(pattern) { |match| tables << match[0] }
102
+ content.scan(pattern) { |match| tables << match[0].delete(SQL_QUOTING) }
82
103
  end
83
- content.scan(KEYWORD_TABLE_PATTERN) { |match| tables << match[0] }
104
+ content.scan(KEYWORD_TABLE_PATTERN) { |match| tables.concat(match.compact) }
105
+ tables.map! { |table| table.delete_prefix(DEFAULT_SCHEMA_PREFIX) }
84
106
  tables.uniq!
85
107
 
86
108
  # Model references are a guess (a constant answering these methods need
@@ -90,7 +112,8 @@ module Railbow
90
112
  content.scan(MODEL_REFERENCE_PATTERN) do |match|
91
113
  break if tables.size >= 2
92
114
  table = ActiveSupport::Inflector.tableize(match[0].split("::").last)
93
- tables << table unless tables.include?(table)
115
+ # A model has no schema, so it matches a found table of any schema.
116
+ tables << table unless tables.any? { |found| found.split(".").last == table }
94
117
  end
95
118
  end
96
119
 
@@ -13,6 +13,7 @@ module Railbow
13
13
  # authors, landing dates, originating branches and uncommitted files.
14
14
  class GitData
15
15
  EMPTY_AUTHORS = {names: {}, emails: {}}.freeze
16
+ PORCELAIN_KINDS = {"??" => :untracked, "A " => :added, "AM" => :added, "M " => :edited}.freeze
16
17
 
17
18
  attr_reader :migrate_dir, :author_names, :author_emails, :landed_dates,
18
19
  :git_email, :git_name, :branch_origins, :uncommitted_files,
@@ -50,7 +51,7 @@ module Railbow
50
51
  def self.branch_files(paths, base_override: "")
51
52
  run_cached([:branch_files, paths, base_override]) do
52
53
  files = Set.new(committed_branch_paths(paths, base_override).map { |f| File.basename(f) })
53
- files.merge(uncommitted_files(paths, additions_only: true))
54
+ files.merge(uncommitted_branch_files(paths, base_override: base_override, additions_only: true))
54
55
  end
55
56
  end
56
57
 
@@ -58,10 +59,7 @@ module Railbow
58
59
  # base. Shared with the origin badge, so the badge and the branch rows
59
60
  # agree on which migrations are the branch's.
60
61
  def self.committed_branch_paths(paths, base_override)
61
- merge_base = run_cached([:merge_base, base_override]) do
62
- out, status = Railbow::GitUtils.capture2("merge-base", "HEAD", default_branch(base_override))
63
- status.success? ? out.strip : nil
64
- end
62
+ merge_base = merge_base(base_override)
65
63
  return [] unless merge_base
66
64
 
67
65
  diff_out, diff_status = Railbow::GitUtils.capture2(
@@ -72,6 +70,13 @@ module Railbow
72
70
  diff_out.each_line.filter_map { |l| name_status_path(l.strip) }
73
71
  end
74
72
 
73
+ def self.merge_base(base_override)
74
+ run_cached([:merge_base, base_override]) do
75
+ out, status = Railbow::GitUtils.capture2("merge-base", "HEAD", default_branch(base_override))
76
+ status.success? ? out.strip : nil
77
+ end
78
+ end
79
+
75
80
  # Memoized for the rest of a multi-database run, so per-database calls
76
81
  # share what does not depend on the database. Outside a run, no cache.
77
82
  def self.run_cached(key)
@@ -116,33 +121,70 @@ module Railbow
116
121
  filepath && File.basename(filepath)
117
122
  end
118
123
 
119
- # A staged edit ("M ") belongs to the current branch for the badge, but
120
- # with additions_only it is not a migration the branch adds: an edit to
121
- # an old migration would otherwise count until it is committed.
122
- def self.uncommitted_files(paths, additions_only: false)
124
+ # Every migration with an uncommitted change, whoever it belongs to.
125
+ # Files an in-progress merge brings in from MERGE_HEAD show up as staged
126
+ # additions, so they are left out.
127
+ def self.uncommitted_files(paths, entries: porcelain_entries(paths))
128
+ Set.new(entries.map(&:last)) - incoming_merge_files(paths)
129
+ end
130
+
131
+ # The uncommitted migrations that are the branch's own work. A new file,
132
+ # staged or untracked, counts only when the merge-base does not hold it:
133
+ # a base-branch migration renamed back to its own name is new against
134
+ # HEAD, but adds nothing to the branch. A staged edit belongs to the
135
+ # branch for the badge, but with additions_only it is not a migration the
136
+ # branch adds.
137
+ def self.uncommitted_branch_files(paths, base_override: "", additions_only: false, entries: porcelain_entries(paths))
138
+ in_base = base_files(paths, base_override) if entries.any? { |kind, _| kind != :edited }
139
+
140
+ mine = entries.filter_map do |kind, basename|
141
+ keep =
142
+ case kind
143
+ when :edited then !additions_only
144
+ # Without a merge-base there is nothing to compare against.
145
+ else in_base.nil? || !in_base.include?(basename)
146
+ end
147
+ basename if keep
148
+ end
149
+
150
+ Set.new(mine) - incoming_merge_files(paths)
151
+ end
152
+
153
+ # [kind, basename] per `git status --porcelain` line worth knowing about:
154
+ # :untracked, :added (staged addition or rename) or :edited (staged edit).
155
+ def self.porcelain_entries(paths)
123
156
  output, status = Railbow::GitUtils.capture2("status", "--porcelain", "--", *paths)
124
- return Set.new unless status.success?
157
+ return [] unless status.success?
125
158
 
126
- result = Set.new
127
- output.each_line do |line|
159
+ output.each_line.filter_map do |line|
128
160
  code = line[0..1]
129
161
 
130
162
  if code[0] == "R"
131
- # Rename: "R old -> new" or "R100 old -> new". Take the destination.
163
+ # Rename: "R old -> new". Take the destination.
132
164
  parts = line[3..].split(" -> ", 2)
165
+ kind = :added
133
166
  filepath = (parts[1] || parts[0]).strip
134
- elsif ["??", "A ", "AM"].include?(code) || (code == "M " && !additions_only)
135
- filepath = line[3..].strip
136
167
  else
137
- next
168
+ kind = PORCELAIN_KINDS[code]
169
+ next unless kind
170
+
171
+ filepath = line[3..].strip
138
172
  end
139
173
 
140
- result << File.basename(filepath) unless filepath.empty?
174
+ [kind, File.basename(filepath)] unless filepath.empty?
141
175
  end
176
+ end
177
+
178
+ # Basenames of the migrations the merge-base holds, or nil when there is
179
+ # no merge-base to compare with.
180
+ def self.base_files(paths, base_override)
181
+ merge_base = merge_base(base_override)
182
+ return nil unless merge_base
142
183
 
143
- # During an in-progress merge, files from MERGE_HEAD (e.g. main) show up
144
- # as staged additions. Exclude them so they are not tagged as ours.
145
- result - incoming_merge_files(paths)
184
+ run_cached([:base_files, merge_base, paths]) do
185
+ out, status = Railbow::GitUtils.capture2("ls-tree", "-r", "--name-only", merge_base, "--", *paths)
186
+ status.success? ? Set.new(out.each_line.map { |l| File.basename(l.strip) }) : nil
187
+ end
146
188
  end
147
189
 
148
190
  def self.incoming_merge_files(paths)
@@ -198,12 +240,16 @@ module Railbow
198
240
  @branch_origins = branch_migration_origins(base_branch, branch_mask)
199
241
  @incoming_merge_files = incoming_merge_files_from_git
200
242
  @merge_source_label = detect_merge_source_label(branch_mask) if @incoming_merge_files.any?
201
- @uncommitted_files = uncommitted_migration_files
243
+ entries = self.class.porcelain_entries([migrate_dir])
244
+ @uncommitted_files = self.class.uncommitted_files([migrate_dir], entries: entries)
245
+
246
+ # The branch's uncommitted files have no commit to attribute, so they
247
+ # belong to whatever branch is checked out right now.
248
+ mine = self.class.uncommitted_branch_files([migrate_dir], base_override: base_branch, entries: entries)
249
+ return if mine.empty?
202
250
 
203
- # Uncommitted files have no commit to attribute, so they belong to
204
- # whatever branch is checked out right now.
205
251
  current_branch = current_branch_name(branch_mask)
206
- @uncommitted_files.each { |f| @branch_origins[f] ||= current_branch }
252
+ mine.each { |f| @branch_origins[f] ||= current_branch }
207
253
  end
208
254
 
209
255
  def migration_authors
@@ -357,10 +403,6 @@ module Railbow
357
403
  end
358
404
  end
359
405
 
360
- def uncommitted_migration_files
361
- self.class.uncommitted_files([migrate_dir])
362
- end
363
-
364
406
  def detect_merge_source_label(branch_mask)
365
407
  merge_head, _, status = Railbow::GitUtils.capture3("rev-parse", "MERGE_HEAD")
366
408
  return nil unless status.success?
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Railbow
4
- VERSION = "0.8.0"
4
+ VERSION = "0.8.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railbow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene M