scenic-cascade 0.1.2 → 0.1.4

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: 9d210342197b949abc4156e8fcdc63ad74b04f1fde40fb207e35dc15b13e1efd
4
- data.tar.gz: 60c19c6113bc29ebc57c068ef16d744661f33f9bdd06a763807b807e41d3f66f
3
+ metadata.gz: 88a122c88ce1ff74a3dcad133235b02ba2d0c2a7f981088c3372bd52f6a6db23
4
+ data.tar.gz: f768ffa8187c32107726e6784e3622a46679cdb98220d121a8abe68d170da124
5
5
  SHA512:
6
- metadata.gz: 9040765c0fdadb9778139f634883a09466ddf80d8b63bf28c8b358150e0d4e6eb45556bb3d926c9e458c7e3fd8bf9deb7b8863d6a80694163f6b73643654ee09
7
- data.tar.gz: 05ea8516e444b9c07cba0a8813af3985295897ce11c90790ca407eb5aa81d5d250e937d334269f5b87c797ee932a2ae7e1e09cff92d82c9a7041f89c35cbd06e
6
+ metadata.gz: 37a6f0c06c9ac08373f047a2cd6d6b7c57e1a2e568786f35c9a9e5d33c0c2c9d8037c65181b42a56f6012a87e7ad8bb27931a52da6b139676f7f0c38db54c748
7
+ data.tar.gz: 3b1484414f3d89ca87b100a8a0d82d1ddc63d37eb1c95b308dc0989ac23d83fc542848a42b70d3738da2a43bd0a0f7f6ea10ac2b423ba70f8f8faa80f128d17a
data/.rubocop.yml CHANGED
@@ -3,9 +3,15 @@ require:
3
3
  - rubocop-rake
4
4
  - rubocop-rspec
5
5
 
6
+ inherit_mode:
7
+ merge:
8
+ - Exclude
9
+
6
10
  AllCops:
7
11
  TargetRubyVersion: 2.6
8
12
  NewCops: enable
13
+ Exclude:
14
+ - gemfiles/*.gemfile
9
15
 
10
16
  Style/StringLiterals:
11
17
  Enabled: true
data/CHANGELOG.md CHANGED
@@ -7,6 +7,22 @@ and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.ht
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.1.4] - 2026-11-04
11
+
12
+ ### Added
13
+
14
+ - Verified compatibility with Rails 8.0 and 8.1 while retaining Rails 7.1 and 7.2 coverage.
15
+
16
+ ## [0.1.3] - 2026-11-01
17
+
18
+ ### Fixed
19
+
20
+ - Recreate dependent views when generating a migration with a Rails table name prefix.
21
+ - Discover and recreate dependent views across PostgreSQL schemas without mixing identically named views or their SQL definitions.
22
+ - Remove generated view definitions and partial migrations when migration generation fails, so retries use the correct version.
23
+ - Apply `--replace` and supported `--side-by-side` options when generating view update migrations.
24
+ - Avoid leaving an orphan SQL definition when `--skip` skips a conflicting migration.
25
+
10
26
  ## [0.1.2] - 2026-11-01
11
27
 
12
28
  ### Fixed
@@ -27,7 +43,9 @@ and this project follows [Semantic Versioning](https://semver.org/spec/v2.0.0.ht
27
43
 
28
44
  - Initial release of the cascading Scenic view migration generator.
29
45
 
30
- [Unreleased]: https://github.com/akiomik/scenic-cascade/compare/v0.1.2...HEAD
46
+ [Unreleased]: https://github.com/akiomik/scenic-cascade/compare/v0.1.4...HEAD
47
+ [0.1.4]: https://github.com/akiomik/scenic-cascade/compare/v0.1.3...v0.1.4
48
+ [0.1.3]: https://github.com/akiomik/scenic-cascade/compare/v0.1.2...v0.1.3
31
49
  [0.1.2]: https://github.com/akiomik/scenic-cascade/compare/v0.1.1...v0.1.2
32
50
  [0.1.1]: https://github.com/akiomik/scenic-cascade/compare/v0.1.0...v0.1.1
33
51
  [0.1.0]: https://github.com/akiomik/scenic-cascade/releases/tag/v0.1.0
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in scenic-cascade.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'appraisal', '~> 2.5', require: false
8
9
  gem 'rake', '~> 13.0'
9
10
 
10
11
  # type checking
@@ -12,13 +13,13 @@ gem 'rake', '~> 13.0'
12
13
  gem 'steep', '~> 1.10', require: false
13
14
 
14
15
  # testing
15
- gem 'activerecord', '~> 7.1.0'
16
+ gem 'activerecord', '~> 7.2.0'
16
17
  gem 'ammeter', '~> 1.1'
17
18
  gem 'database_cleaner-active_record', '~> 2.0'
18
19
  # Nokogiri 1.19 drops Ruby 3.1, which remains in the CI matrix.
19
20
  gem 'nokogiri', '~> 1.18.0'
20
21
  gem 'pg', '~> 1.0'
21
- gem 'railties', '~> 7.1.0'
22
+ gem 'railties', '~> 7.2.0'
22
23
  gem 'rspec', '~> 3.0'
23
24
  gem 'simplecov', require: false
24
25
  gem 'simplecov-cobertura', '~> 3.2', require: false
data/Gemfile.lock CHANGED
@@ -1,93 +1,101 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- scenic-cascade (0.1.2)
4
+ scenic-cascade (0.1.4)
5
5
  scenic (~> 1.0)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- actionpack (7.1.6)
11
- actionview (= 7.1.6)
12
- activesupport (= 7.1.6)
10
+ actionpack (7.2.4)
11
+ actionview (= 7.2.4)
12
+ activesupport (= 7.2.4)
13
13
  cgi
14
14
  nokogiri (>= 1.8.5)
15
15
  racc
16
- rack (>= 2.2.4)
16
+ rack (>= 2.2.4, < 3.3)
17
17
  rack-session (>= 1.0.1)
18
18
  rack-test (>= 0.6.3)
19
19
  rails-dom-testing (~> 2.2)
20
20
  rails-html-sanitizer (~> 1.6)
21
- actionview (7.1.6)
22
- activesupport (= 7.1.6)
21
+ useragent (~> 0.16)
22
+ actionview (7.2.4)
23
+ activesupport (= 7.2.4)
23
24
  builder (~> 3.1)
24
25
  cgi
25
26
  erubi (~> 1.11)
26
27
  rails-dom-testing (~> 2.2)
27
28
  rails-html-sanitizer (~> 1.6)
28
- activemodel (7.1.6)
29
- activesupport (= 7.1.6)
30
- activerecord (7.1.6)
31
- activemodel (= 7.1.6)
32
- activesupport (= 7.1.6)
29
+ activemodel (7.2.4)
30
+ activesupport (= 7.2.4)
31
+ activerecord (7.2.4)
32
+ activemodel (= 7.2.4)
33
+ activesupport (= 7.2.4)
33
34
  timeout (>= 0.4.0)
34
- activesupport (7.1.6)
35
+ activesupport (7.2.4)
35
36
  base64
36
37
  benchmark (>= 0.3)
37
38
  bigdecimal
38
- concurrent-ruby (~> 1.0, >= 1.0.2)
39
- connection_pool (>= 2.2.5)
39
+ concurrent-ruby (~> 1.0, >= 1.3.1)
40
+ connection_pool (>= 2.2.5, < 3)
40
41
  drb
41
42
  i18n (>= 1.6, < 2)
42
43
  logger (>= 1.4.2)
43
- minitest (>= 5.1)
44
- mutex_m
44
+ minitest (>= 5.1, < 6)
45
45
  securerandom (>= 0.3)
46
- tzinfo (~> 2.0)
47
- ammeter (1.1.6)
46
+ tzinfo (~> 2.0, >= 2.0.5)
47
+ ammeter (1.1.7)
48
48
  activesupport (>= 3.0)
49
49
  railties (>= 3.0)
50
50
  rspec-rails (>= 2.2)
51
+ appraisal (2.5.0)
52
+ bundler
53
+ rake
54
+ thor (>= 0.14.0)
51
55
  ast (2.4.2)
52
- base64 (0.2.0)
56
+ base64 (0.3.0)
53
57
  benchmark (0.5.0)
54
- bigdecimal (3.1.4)
55
- builder (3.2.4)
58
+ bigdecimal (4.1.3)
59
+ builder (3.3.0)
56
60
  cgi (0.5.2)
57
- concurrent-ruby (1.3.7)
58
- connection_pool (2.4.1)
61
+ concurrent-ruby (1.3.8)
62
+ connection_pool (2.5.5)
59
63
  crass (1.0.8)
60
64
  csv (3.3.7)
61
- database_cleaner-active_record (2.1.0)
65
+ database_cleaner-active_record (2.2.2)
62
66
  activerecord (>= 5.a)
63
- database_cleaner-core (~> 2.0.0)
64
- database_cleaner-core (2.0.1)
65
- diff-lcs (1.5.0)
67
+ database_cleaner-core (~> 2.0)
68
+ database_cleaner-core (2.1.0)
69
+ date (3.6.0)
70
+ diff-lcs (1.6.2)
66
71
  docile (1.4.0)
67
- drb (2.2.0)
68
- ruby2_keywords
69
- erubi (1.12.0)
72
+ drb (2.2.3)
73
+ erb (4.0.4.1)
74
+ cgi (>= 0.3.3)
75
+ erubi (1.13.1)
70
76
  ffi (1.17.4-aarch64-linux-musl)
71
77
  ffi (1.17.4-arm64-darwin)
72
78
  ffi (1.17.4-x86_64-linux-gnu)
73
79
  fileutils (1.8.1)
74
- i18n (1.14.1)
80
+ i18n (1.15.2)
75
81
  concurrent-ruby (~> 1.0)
76
- io-console (0.6.0)
77
- irb (1.10.1)
78
- rdoc
79
- reline (>= 0.3.8)
82
+ io-console (0.9.5)
83
+ irb (1.18.0)
84
+ pp (>= 0.6.0)
85
+ prism (>= 1.3.0)
86
+ rdoc (>= 4.0.0)
87
+ reline (>= 0.4.2)
80
88
  json (2.7.1)
81
89
  language_server-protocol (3.17.0.6)
82
90
  listen (3.10.1)
83
91
  logger
84
92
  rb-fsevent (~> 0.10, >= 0.10.3)
85
93
  rb-inotify (~> 0.9, >= 0.9.10)
86
- logger (1.6.0)
94
+ logger (1.7.0)
87
95
  loofah (2.25.2)
88
96
  crass (~> 1.0.2)
89
97
  nokogiri (>= 1.12.0)
90
- minitest (5.20.0)
98
+ minitest (5.27.0)
91
99
  mutex_m (0.3.0)
92
100
  nokogiri (1.18.10-aarch64-linux-musl)
93
101
  racc (~> 1.4)
@@ -100,61 +108,68 @@ GEM
100
108
  ast (~> 2.4.1)
101
109
  racc
102
110
  pg (1.5.4)
103
- psych (5.1.2)
111
+ pp (0.6.4)
112
+ prettyprint
113
+ prettyprint (0.2.1)
114
+ prism (2.1.0)
115
+ psych (5.5.0)
116
+ date
104
117
  stringio
105
- racc (1.7.3)
106
- rack (3.0.8)
107
- rack-session (2.0.0)
118
+ racc (1.8.1)
119
+ rack (3.2.8)
120
+ rack-session (2.1.2)
121
+ base64 (>= 0.1.0)
108
122
  rack (>= 3.0.0)
109
- rack-test (2.1.0)
123
+ rack-test (2.3.0)
110
124
  rack (>= 1.3)
111
- rackup (2.1.0)
125
+ rackup (2.3.1)
112
126
  rack (>= 3)
113
- webrick (~> 1.8)
114
- rails-dom-testing (2.2.0)
127
+ rails-dom-testing (2.3.0)
115
128
  activesupport (>= 5.0.0)
116
129
  minitest
117
130
  nokogiri (>= 1.6)
118
- rails-html-sanitizer (1.6.0)
119
- loofah (~> 2.21)
120
- nokogiri (~> 1.14)
121
- railties (7.1.6)
122
- actionpack (= 7.1.6)
123
- activesupport (= 7.1.6)
131
+ rails-html-sanitizer (1.7.1)
132
+ loofah (~> 2.25, >= 2.25.2)
133
+ nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
134
+ railties (7.2.4)
135
+ actionpack (= 7.2.4)
136
+ activesupport (= 7.2.4)
124
137
  cgi
125
- irb
138
+ irb (~> 1.13)
126
139
  rackup (>= 1.0.0)
127
140
  rake (>= 12.2)
128
141
  thor (~> 1.0, >= 1.2.2)
129
142
  tsort (>= 0.2)
130
143
  zeitwerk (~> 2.6)
131
144
  rainbow (3.1.1)
132
- rake (13.1.0)
145
+ rake (13.4.2)
133
146
  rb-fsevent (0.11.2)
134
147
  rb-inotify (0.11.1)
135
148
  ffi (~> 1.0)
136
149
  rbs (3.10.4)
137
150
  logger
138
151
  tsort
139
- rdoc (6.6.3.1)
152
+ rdoc (7.2.0)
153
+ erb
140
154
  psych (>= 4.0.0)
155
+ tsort
141
156
  regexp_parser (2.8.3)
142
- reline (0.4.1)
157
+ reline (0.7.0)
143
158
  io-console (~> 0.5)
144
159
  rexml (3.4.4)
145
160
  rspec (3.12.0)
146
161
  rspec-core (~> 3.12.0)
147
162
  rspec-expectations (~> 3.12.0)
148
163
  rspec-mocks (~> 3.12.0)
149
- rspec-core (3.12.2)
164
+ rspec-core (3.12.3)
150
165
  rspec-support (~> 3.12.0)
151
- rspec-expectations (3.12.3)
166
+ rspec-expectations (3.12.4)
152
167
  diff-lcs (>= 1.2.0, < 2.0)
153
168
  rspec-support (~> 3.12.0)
154
- rspec-mocks (3.12.6)
169
+ rspec-mocks (3.12.7)
155
170
  diff-lcs (>= 1.2.0, < 2.0)
156
171
  rspec-support (~> 3.12.0)
157
- rspec-rails (6.1.0)
172
+ rspec-rails (6.1.1)
158
173
  actionpack (>= 6.1)
159
174
  activesupport (>= 6.1)
160
175
  railties (>= 6.1)
@@ -162,7 +177,7 @@ GEM
162
177
  rspec-expectations (~> 3.12)
163
178
  rspec-mocks (~> 3.12)
164
179
  rspec-support (~> 3.12)
165
- rspec-support (3.12.1)
180
+ rspec-support (3.12.2)
166
181
  rubocop (1.58.0)
167
182
  json (~> 2.3)
168
183
  language_server-protocol (>= 3.17.0)
@@ -190,11 +205,10 @@ GEM
190
205
  rubocop-capybara (~> 2.17)
191
206
  rubocop-factory_bot (~> 2.22)
192
207
  ruby-progressbar (1.13.0)
193
- ruby2_keywords (0.0.5)
194
208
  scenic (1.9.0)
195
209
  activerecord (>= 4.0.0)
196
210
  railties (>= 4.0.0)
197
- securerandom (0.3.0)
211
+ securerandom (0.4.1)
198
212
  simplecov (0.22.0)
199
213
  docile (~> 1.1)
200
214
  simplecov-html (~> 0.11)
@@ -225,15 +239,15 @@ GEM
225
239
  strscan (3.1.8)
226
240
  terminal-table (4.0.0)
227
241
  unicode-display_width (>= 1.1.1, < 4)
228
- thor (1.4.0)
229
- timeout (0.4.1)
242
+ thor (1.5.0)
243
+ timeout (0.6.1)
230
244
  tsort (0.2.0)
231
245
  tzinfo (2.0.6)
232
246
  concurrent-ruby (~> 1.0)
233
247
  unicode-display_width (2.5.0)
234
248
  uri (1.1.2)
235
- webrick (1.8.2)
236
- zeitwerk (2.6.12)
249
+ useragent (0.16.11)
250
+ zeitwerk (2.6.18)
237
251
 
238
252
  PLATFORMS
239
253
  aarch64-linux-musl
@@ -241,12 +255,13 @@ PLATFORMS
241
255
  x86_64-linux
242
256
 
243
257
  DEPENDENCIES
244
- activerecord (~> 7.1.0)
258
+ activerecord (~> 7.2.0)
245
259
  ammeter (~> 1.1)
260
+ appraisal (~> 2.5)
246
261
  database_cleaner-active_record (~> 2.0)
247
262
  nokogiri (~> 1.18.0)
248
263
  pg (~> 1.0)
249
- railties (~> 7.1.0)
264
+ railties (~> 7.2.0)
250
265
  rake (~> 13.0)
251
266
  rspec (~> 3.0)
252
267
  rubocop (~> 1.21)
data/README.md CHANGED
@@ -37,6 +37,12 @@ $ bin/rails generate scenic:view:cascade search_results
37
37
  create db/migrate/20220714233704_create_search_results.rb
38
38
  ```
39
39
 
40
+ For a schema-qualified view, pass its database relation name, such as `reporting.orders`.
41
+ If Rails uses a table name prefix, include it after the schema name (for example, `reporting.app_orders`).
42
+ For unqualified names, pass the name without the prefix; Rails applies it in the migration.
43
+ If another schema precedes `public` in PostgreSQL's `search_path` and contains a view with the same name,
44
+ use a schema-qualified Scenic definition for the public dependent view (for example, `public_reports_v01.sql`).
45
+
40
46
  ## How it works
41
47
 
42
48
  Consider a situation where the following three views exist:
@@ -86,6 +92,8 @@ end
86
92
 
87
93
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
88
94
 
95
+ The default bundle tests Rails 7.2. To test another supported version, run `bundle exec appraisal install` followed by `bundle exec appraisal rails-7-1 rspec`, `bundle exec appraisal rails-8-0 rspec`, or `bundle exec appraisal rails-8-1 rspec`.
96
+
89
97
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update `version.rb`, `Gemfile.lock`, and `CHANGELOG.md`, then publish a GitHub Release with a matching `vX.Y.Z` tag. The [publish workflow](.github/workflows/publish.yml) builds and pushes the gem to [rubygems.org](https://rubygems.org).
90
98
 
91
99
  ## Contributing
data/codecov.yml ADDED
@@ -0,0 +1,3 @@
1
+ # Release version changes are metadata and do not exercise application behavior.
2
+ ignore:
3
+ - '^lib/scenic/cascade/version\.rb$'
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'erb'
3
4
  require 'generators/scenic/view/view_generator'
4
5
  require 'scenic/cascade'
5
6
  require 'scenic/cascade/view_with_version'
@@ -12,27 +13,83 @@ module Scenic
12
13
  source_root File.expand_path('templates', __dir__ || '.')
13
14
 
14
15
  def create_view_definition
16
+ @definition_path = @original_definition = nil
17
+ validate_update_options! unless destroying? || creating_new_view?
15
18
  dependents unless destroying? || creating_new_view?
19
+ path = Scenic::Definition.new(plural_file_name, version).full_path.to_s
20
+ @definition_path = path
21
+ @original_definition = File.binread(path) if File.file?(path)
16
22
  super
23
+ rescue StandardError
24
+ restore_definition
25
+ raise
17
26
  end
18
27
 
19
28
  def create_migration_file
20
- if creating_new_view? || destroying_initial_view?
21
- migration_template('db/migrate/create_view.rb.erb',
22
- "db/migrate/create_#{plural_file_name}.rb")
23
- else
24
- migration_template('db/migrate/update_view.rb.erb',
25
- "db/migrate/update_#{plural_file_name}_to_version_#{version}.rb")
26
- end
29
+ template, destination = migration_template_details
30
+ pattern = File.join(destination_root, 'db/migrate', "[0-9]*_#{File.basename(destination)}")
31
+ original_migrations = Dir.glob(pattern).to_h { |path| [path, File.binread(path)] }
32
+ migration_template(template, destination)
33
+ # Thor returns normally for --skip even though it did not create a migration.
34
+ restore_definition if options[:skip] && skipped_migration?(template, pattern, original_migrations)
35
+ rescue StandardError
36
+ restore_migrations(pattern, original_migrations) if pattern && original_migrations
37
+ restore_definition
38
+ raise
27
39
  end
28
40
 
29
41
  private
30
42
 
43
+ def validate_update_options!
44
+ raise ArgumentError, '--replace cannot update a materialized view' if options[:replace] && materialized?
45
+
46
+ validate_side_by_side_option! if options[:side_by_side]
47
+ end
48
+
49
+ def validate_side_by_side_option!
50
+ raise ArgumentError, '--side-by-side requires --materialized' unless materialized?
51
+ raise ArgumentError, '--side-by-side cannot update schema-qualified views' if plural_name.include?('.')
52
+
53
+ raise ArgumentError, '--side-by-side cannot be combined with --no-data' if options[:no_data]
54
+ end
55
+
56
+ def migration_template_details
57
+ return ['db/migrate/create_view.rb.erb', "db/migrate/create_#{plural_file_name}.rb"] if
58
+ creating_new_view? || destroying_initial_view?
59
+
60
+ ['db/migrate/update_view.rb.erb', "db/migrate/update_#{plural_file_name}_to_version_#{version}.rb"]
61
+ end
62
+
63
+ def restore_definition
64
+ return unless (path = @definition_path)
65
+
66
+ if (original = @original_definition)
67
+ restore_file(path, original)
68
+ elsif File.file?(path)
69
+ File.delete(path)
70
+ end
71
+ end
72
+
73
+ def restore_migrations(pattern, originals)
74
+ (Dir.glob(pattern) - originals.keys).each { |path| File.delete(path) }
75
+ originals.each { |path, content| restore_file(path, content) }
76
+ end
77
+
78
+ def restore_file(path, content)
79
+ File.binwrite(path, content) unless File.file?(path) && File.binread(path) == content
80
+ end
81
+
82
+ def skipped_migration?(template, pattern, originals)
83
+ return false unless Dir.glob(pattern).to_h { |path| [path, File.binread(path)] } == originals
84
+
85
+ originals.values.first != ERB.new(File.read(find_in_source_paths(template)), trim_mode: '-').result(binding).b
86
+ end
87
+
31
88
  def dependents
32
89
  cached_dependents = @dependents
33
90
  return cached_dependents unless cached_dependents.nil?
34
91
 
35
- deps = Scenic::Cascade.view_dependents_of(plural_name, recursive: true)
92
+ deps = Scenic::Cascade.view_dependents_of(database_view_name, recursive: true)
36
93
  views = deps.map { |dep| view_with_version_for(dep) }
37
94
  # The last visit follows every prerequisite, even when names sort in the opposite order.
38
95
  @dependents = views.reverse.uniq { |view| [view.name, view.version, view.materialized?] }.reverse
@@ -40,11 +97,48 @@ module Scenic
40
97
 
41
98
  def view_with_version_for(dep)
42
99
  name = dep.from.name
43
- definition = Scenic::Cascade.find_definitions_of(name).last
100
+ definition_name = definition_name_for(name)
101
+ definition = Scenic::Cascade.find_definitions_of(definition_name).last
44
102
  raise ArgumentError, "Dependent view #{name} has no Scenic definition file" if definition.nil?
45
103
 
46
- version = definition.version.to_i
47
- Scenic::Cascade::ViewWithVersion.new(name: name, version: version, materialized: dep.from.materialized?)
104
+ Scenic::Cascade::ViewWithVersion.new(
105
+ name: definition_name, version: definition.version.to_i,
106
+ materialized: dep.from.materialized?, database_name: qualified_name(name)
107
+ )
108
+ end
109
+
110
+ def definition_name_for(name)
111
+ return name if name.include?('.')
112
+
113
+ qualified = qualified_name(name)
114
+ qualified_definitions = Scenic::Cascade.find_definitions_of(qualified)
115
+ bare_definitions = Scenic::Cascade.find_definitions_of(name)
116
+
117
+ if qualified_definitions.any? && bare_definitions.any?
118
+ raise ArgumentError, "Dependent view #{name} has ambiguous Scenic definitions: #{name} and #{qualified}"
119
+ end
120
+
121
+ return qualified if qualified_definitions.any?
122
+ return name if bare_definitions.empty? || bare_name_resolves_to_public?(name)
123
+
124
+ raise ArgumentError, "Dependent view #{name} has no unambiguous Scenic definition file for #{qualified}"
125
+ end
126
+
127
+ def bare_name_resolves_to_public?(name)
128
+ connection = ActiveRecord::Base.connection
129
+ bare = connection.quote(connection.quote_table_name(name))
130
+ qualified = connection.quote(connection.quote_table_name(qualified_name(name)))
131
+ connection.select_value("SELECT to_regclass(#{bare}) = to_regclass(#{qualified})")
132
+ end
133
+
134
+ def qualified_name(name)
135
+ name.include?('.') ? name : "public.#{name}"
136
+ end
137
+
138
+ def database_view_name
139
+ return plural_name if plural_name.include?('.')
140
+
141
+ "#{ActiveRecord::Base.table_name_prefix}#{plural_name}#{ActiveRecord::Base.table_name_suffix}"
48
142
  end
49
143
  end
50
144
  end
@@ -1,26 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class <%= migration_class_name %> < <%= activerecord_migration_class %>
4
+ <%- if dependents.any? || plural_name.include?('.') -%>
5
+ class ExactViewName < String
6
+ alias table_name to_s
7
+ end
8
+
9
+ <%- end -%>
10
+ <%- target_name = plural_name.include?('.') ? "ExactViewName.new(#{plural_name.inspect})" : formatted_plural_name -%>
11
+ <%- update_method = options[:replace] ? 'replace_view' : 'update_view' -%>
12
+ <%- materialized_flags = [] -%>
13
+ <%- materialized_flags << 'no_data: true' if options[:no_data] -%>
14
+ <%- materialized_flags << 'side_by_side: true' if options[:side_by_side] -%>
15
+ <%- materialized_options = materialized_flags.empty? ? 'true' : "{ #{materialized_flags.join(', ')} }" -%>
4
16
  def change
5
17
  <%- dependents.reverse_each do |dep| -%>
6
- drop_view <%= dep.formatted_name %>,
18
+ <%- if dep.database_name == dep.name -%>
19
+ drop_view ExactViewName.new(<%= dep.database_name.inspect %>),
7
20
  revert_to_version: <%= dep.version %>,
8
21
  materialized: <%= dep.materialized? %>
22
+ <%- else -%>
23
+ reversible do |direction|
24
+ direction.up do
25
+ drop_view ExactViewName.new(<%= dep.database_name.inspect %>), materialized: <%= dep.materialized? %>
26
+ end
27
+ direction.down do
28
+ create_view ExactViewName.new(<%= dep.database_name.inspect %>),
29
+ sql_definition: Scenic::Definition.new(<%= dep.name.inspect %>, <%= dep.version %>).to_sql,
30
+ materialized: <%= dep.materialized? %>
31
+ end
32
+ end
33
+ <%- end -%>
9
34
  <%- end -%>
10
35
 
11
36
  <%- if materialized? -%>
12
- update_view <%= formatted_plural_name %>,
37
+ <%= update_method %> <%= target_name %>,
13
38
  version: <%= version %>,
14
39
  revert_to_version: <%= previous_version %>,
15
- materialized: <%= no_data? ? "{ no_data: true }" : true %>
40
+ materialized: <%= materialized_options %>
16
41
  <%- else -%>
17
- update_view <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
42
+ <%= update_method %> <%= target_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
18
43
  <%- end -%>
19
44
 
20
45
  <%- dependents.each do |dep| -%>
21
- create_view <%= dep.formatted_name %>,
46
+ <%- if dep.database_name == dep.name -%>
47
+ create_view ExactViewName.new(<%= dep.database_name.inspect %>),
22
48
  version: <%= dep.version %>,
23
49
  materialized: <%= dep.materialized? %>
50
+ <%- else -%>
51
+ reversible do |direction|
52
+ direction.up do
53
+ create_view ExactViewName.new(<%= dep.database_name.inspect %>),
54
+ sql_definition: Scenic::Definition.new(<%= dep.name.inspect %>, <%= dep.version %>).to_sql,
55
+ materialized: <%= dep.materialized? %>
56
+ end
57
+ direction.down do
58
+ drop_view ExactViewName.new(<%= dep.database_name.inspect %>), materialized: <%= dep.materialized? %>
59
+ end
60
+ end
61
+ <%- end -%>
24
62
  <%- end -%>
25
63
  end
26
64
  end
@@ -14,21 +14,28 @@ module Scenic
14
14
  module ClassMethods
15
15
  DEPENDENCY_SQL = <<-SQL
16
16
  SELECT DISTINCT
17
- dependee.relname AS to
17
+ CASE WHEN dependee_ns.nspname = 'public' THEN dependee.relname
18
+ ELSE dependee_ns.nspname || '.' || dependee.relname END AS to
18
19
  , dependee.relkind = 'm' AS to_materialized
19
- , depender.relname AS from
20
+ , CASE WHEN depender_ns.nspname = 'public' THEN depender.relname
21
+ ELSE depender_ns.nspname || '.' || depender.relname END AS from
20
22
  , depender.relkind = 'm' AS from_materialized
23
+ , format('%I.%I', dependee_ns.nspname, dependee.relname) AS to_lookup
21
24
  FROM pg_depend d
22
25
  JOIN pg_rewrite r
23
26
  ON d.objid = r.oid
24
27
  JOIN pg_class AS dependee
25
28
  ON d.refobjid = dependee.oid
29
+ JOIN pg_namespace AS dependee_ns
30
+ ON dependee.relnamespace = dependee_ns.oid
26
31
  JOIN pg_class AS depender
27
32
  ON r.ev_class = depender.oid
28
- WHERE depender.relname = ?
29
- AND dependee.relname != depender.relname
33
+ JOIN pg_namespace AS depender_ns
34
+ ON depender.relnamespace = depender_ns.oid
35
+ WHERE depender.oid = to_regclass(?)
36
+ AND dependee.oid != depender.oid
30
37
  AND dependee.relkind in ('m', 'v')
31
- ORDER BY dependee.relname;
38
+ ORDER BY to_lookup;
32
39
  SQL
33
40
 
34
41
  private_constant :DEPENDENCY_SQL
@@ -41,8 +48,8 @@ module Scenic
41
48
  return [] if dependencies.empty?
42
49
  return dependencies unless recursive
43
50
 
44
- dependencies.flat_map do |dependency|
45
- [dependency, *view_dependencies_of(dependency.to.name, recursive: true)]
51
+ raw_dependencies.zip(dependencies).flat_map do |row, dependency|
52
+ [dependency, *view_dependencies_of(row.fetch('to_lookup'), recursive: true)]
46
53
  end
47
54
  end
48
55
  end
@@ -14,21 +14,28 @@ module Scenic
14
14
  module ClassMethods
15
15
  DEPENDANT_SQL = <<-SQL
16
16
  SELECT DISTINCT
17
- dependee.relname AS to
17
+ CASE WHEN dependee_ns.nspname = 'public' THEN dependee.relname
18
+ ELSE dependee_ns.nspname || '.' || dependee.relname END AS to
18
19
  , dependee.relkind = 'm' AS to_materialized
19
- , depender.relname AS from
20
+ , CASE WHEN depender_ns.nspname = 'public' THEN depender.relname
21
+ ELSE depender_ns.nspname || '.' || depender.relname END AS from
20
22
  , depender.relkind = 'm' AS from_materialized
23
+ , format('%I.%I', depender_ns.nspname, depender.relname) AS from_lookup
21
24
  FROM pg_depend d
22
25
  JOIN pg_rewrite r
23
26
  ON d.objid = r.oid
24
27
  JOIN pg_class AS dependee
25
28
  ON d.refobjid = dependee.oid
29
+ JOIN pg_namespace AS dependee_ns
30
+ ON dependee.relnamespace = dependee_ns.oid
26
31
  JOIN pg_class AS depender
27
32
  ON r.ev_class = depender.oid
28
- WHERE dependee.relname = ?
29
- AND depender.relname != dependee.relname
33
+ JOIN pg_namespace AS depender_ns
34
+ ON depender.relnamespace = depender_ns.oid
35
+ WHERE dependee.oid = to_regclass(?)
36
+ AND depender.oid != dependee.oid
30
37
  AND depender.relkind in ('m', 'v')
31
- ORDER BY depender.relname;
38
+ ORDER BY from_lookup;
32
39
  SQL
33
40
 
34
41
  private_constant :DEPENDANT_SQL
@@ -41,8 +48,8 @@ module Scenic
41
48
  return [] if dependencies.empty?
42
49
  return dependencies unless recursive
43
50
 
44
- dependencies.flat_map do |dependency|
45
- [dependency, *view_dependents_of(dependency.from.name, recursive: true)]
51
+ raw_dependencies.zip(dependencies).flat_map do |row, dependency|
52
+ [dependency, *view_dependents_of(row.fetch('from_lookup'), recursive: true)]
46
53
  end
47
54
  end
48
55
 
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Scenic
4
4
  module Cascade
5
- VERSION = '0.1.2'
5
+ VERSION = '0.1.4'
6
6
  end
7
7
  end
@@ -6,14 +6,16 @@ module Scenic
6
6
  module Cascade
7
7
  # Represents a view with version
8
8
  class ViewWithVersion < Scenic::Cascade::View
9
- attr_reader :version
9
+ attr_reader :version, :database_name
10
10
 
11
- def initialize(name:, version:, materialized:)
11
+ def initialize(name:, version:, materialized:, database_name: name)
12
12
  super(name: name, materialized: materialized)
13
13
 
14
14
  raise TypeError unless version.is_a?(Integer)
15
+ raise TypeError unless database_name.is_a?(String)
15
16
 
16
17
  @version = version
18
+ @database_name = database_name
17
19
  end
18
20
 
19
21
  def ==(other)
@@ -2,7 +2,7 @@
2
2
  path: ".gem_rbs_collection"
3
3
  gems:
4
4
  - name: actionpack
5
- version: '6.0'
5
+ version: '7.2'
6
6
  source:
7
7
  type: git
8
8
  name: ruby/gem_rbs_collection
@@ -26,7 +26,7 @@ gems:
26
26
  remote: https://github.com/ruby/gem_rbs_collection.git
27
27
  repo_dir: gems
28
28
  - name: activerecord
29
- version: '7.1'
29
+ version: '7.2'
30
30
  source:
31
31
  type: git
32
32
  name: ruby/gem_rbs_collection
@@ -42,25 +42,17 @@ gems:
42
42
  remote: https://github.com/ruby/gem_rbs_collection.git
43
43
  repo_dir: gems
44
44
  - name: base64
45
- version: '0.1'
45
+ version: 0.3.0
46
46
  source:
47
- type: git
48
- name: ruby/gem_rbs_collection
49
- revision: 9e17daa08b1710667699a208981cffd21dbf15f6
50
- remote: https://github.com/ruby/gem_rbs_collection.git
51
- repo_dir: gems
47
+ type: rubygems
52
48
  - name: benchmark
53
49
  version: '0'
54
50
  source:
55
51
  type: stdlib
56
52
  - name: bigdecimal
57
- version: '3.1'
53
+ version: 4.1.3
58
54
  source:
59
- type: git
60
- name: ruby/gem_rbs_collection
61
- revision: 9e17daa08b1710667699a208981cffd21dbf15f6
62
- remote: https://github.com/ruby/gem_rbs_collection.git
63
- repo_dir: gems
55
+ type: rubygems
64
56
  - name: cgi
65
57
  version: '0.5'
66
58
  source:
@@ -145,10 +137,6 @@ gems:
145
137
  version: '0'
146
138
  source:
147
139
  type: stdlib
148
- - name: mutex_m
149
- version: 0.3.0
150
- source:
151
- type: rubygems
152
140
  - name: nokogiri
153
141
  version: '1.11'
154
142
  source:
@@ -165,6 +153,18 @@ gems:
165
153
  version: '0'
166
154
  source:
167
155
  type: stdlib
156
+ - name: pp
157
+ version: '0'
158
+ source:
159
+ type: stdlib
160
+ - name: prettyprint
161
+ version: '0'
162
+ source:
163
+ type: stdlib
164
+ - name: prism
165
+ version: 2.1.0
166
+ source:
167
+ type: rubygems
168
168
  - name: pstore
169
169
  version: '0'
170
170
  source:
@@ -3,14 +3,27 @@ module Scenic
3
3
  module View
4
4
  class CascadeGenerator < Scenic::Generators::ViewGenerator
5
5
  @dependents: Array[Scenic::Cascade::ViewWithVersion]?
6
+ @definition_path: String?
7
+ @original_definition: String?
6
8
 
7
9
  def create_view_definition: -> void
8
10
  def create_migration_file: -> void
9
11
 
10
12
  private
11
13
 
14
+ def validate_update_options!: -> void
15
+ def validate_side_by_side_option!: -> void
12
16
  def dependents: -> Array[Scenic::Cascade::ViewWithVersion]
17
+ def migration_template_details: -> [String, String]
18
+ def restore_definition: -> void
19
+ def restore_migrations: (String, Hash[String, String]) -> void
20
+ def restore_file: (String, String) -> void
21
+ def skipped_migration?: (String, String, Hash[String, String]) -> bool
13
22
  def view_with_version_for: (Scenic::Cascade::Dependency) -> Scenic::Cascade::ViewWithVersion
23
+ def database_view_name: -> String
24
+ def definition_name_for: (String) -> String
25
+ def bare_name_resolves_to_public?: (String) -> bool
26
+ def qualified_name: (String) -> String
14
27
  end
15
28
  end
16
29
  end
@@ -3,12 +3,16 @@ module Scenic
3
3
  class ViewGenerator
4
4
  def self.source_root: (String?) -> void
5
5
  def migration_template: (String, String) -> void
6
+ def destination_root: -> String
7
+ def find_in_source_paths: (String) -> String
8
+ def options: -> Hash[Symbol, untyped]
6
9
 
7
10
  def create_view_definition: -> void
8
11
  def version: -> Integer
9
12
 
10
13
  private
11
14
 
15
+ def materialized?: -> bool
12
16
  def plural_name: -> String
13
17
  def plural_file_name: -> String
14
18
  def creating_new_view?: -> bool
@@ -8,8 +8,9 @@ module Scenic
8
8
 
9
9
  class ViewWithVersion < Scenic::Cascade::View
10
10
  attr_reader version: Integer
11
+ attr_reader database_name: String
11
12
 
12
- def initialize: (name: String, version: Integer, materialized: bool) -> void
13
+ def initialize: (name: String, version: Integer, materialized: bool, ?database_name: String) -> void
13
14
 
14
15
  def ==: (_ViewWithVersionLike) -> bool
15
16
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scenic-cascade
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Akiomi Kamakura
@@ -44,6 +44,7 @@ files:
44
44
  - README.md
45
45
  - Rakefile
46
46
  - Steepfile
47
+ - codecov.yml
47
48
  - docker-compose.yml
48
49
  - lib/generators/scenic/view/cascade/USAGE
49
50
  - lib/generators/scenic/view/cascade/cascade_generator.rb