scenic-jets 1.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +78 -0
  3. data/.gitignore +19 -0
  4. data/.hound.yml +2 -0
  5. data/.rubocop.yml +129 -0
  6. data/.yardopts +4 -0
  7. data/CHANGELOG.md +223 -0
  8. data/CODE_OF_CONDUCT.md +76 -0
  9. data/CONTRIBUTING.md +24 -0
  10. data/Gemfile +16 -0
  11. data/LICENSE.txt +22 -0
  12. data/README.md +5 -0
  13. data/Rakefile +29 -0
  14. data/SECURITY.md +14 -0
  15. data/bin/rake +17 -0
  16. data/bin/rspec +17 -0
  17. data/bin/setup +18 -0
  18. data/bin/yard +16 -0
  19. data/lib/generators/scenic/generators.rb +12 -0
  20. data/lib/generators/scenic/materializable.rb +31 -0
  21. data/lib/generators/scenic/model/USAGE +12 -0
  22. data/lib/generators/scenic/model/model_generator.rb +52 -0
  23. data/lib/generators/scenic/model/templates/model.erb +3 -0
  24. data/lib/generators/scenic/view/USAGE +20 -0
  25. data/lib/generators/scenic/view/templates/db/migrate/create_view.erb +5 -0
  26. data/lib/generators/scenic/view/templates/db/migrate/update_view.erb +12 -0
  27. data/lib/generators/scenic/view/view_generator.rb +127 -0
  28. data/lib/scenic.rb +31 -0
  29. data/lib/scenic/adapters/postgres.rb +256 -0
  30. data/lib/scenic/adapters/postgres/connection.rb +57 -0
  31. data/lib/scenic/adapters/postgres/errors.rb +26 -0
  32. data/lib/scenic/adapters/postgres/index_reapplication.rb +71 -0
  33. data/lib/scenic/adapters/postgres/indexes.rb +53 -0
  34. data/lib/scenic/adapters/postgres/refresh_dependencies.rb +116 -0
  35. data/lib/scenic/adapters/postgres/views.rb +74 -0
  36. data/lib/scenic/command_recorder.rb +52 -0
  37. data/lib/scenic/command_recorder/statement_arguments.rb +51 -0
  38. data/lib/scenic/configuration.rb +37 -0
  39. data/lib/scenic/definition.rb +35 -0
  40. data/lib/scenic/index.rb +36 -0
  41. data/lib/scenic/schema_dumper.rb +44 -0
  42. data/lib/scenic/statements.rb +163 -0
  43. data/lib/scenic/version.rb +3 -0
  44. data/lib/scenic/view.rb +54 -0
  45. data/scenic.gemspec +36 -0
  46. data/spec/acceptance/user_manages_views_spec.rb +88 -0
  47. data/spec/acceptance_helper.rb +33 -0
  48. data/spec/dummy/.gitignore +16 -0
  49. data/spec/dummy/Rakefile +13 -0
  50. data/spec/dummy/app/models/application_record.rb +5 -0
  51. data/spec/dummy/bin/bundle +3 -0
  52. data/spec/dummy/bin/rails +4 -0
  53. data/spec/dummy/bin/rake +4 -0
  54. data/spec/dummy/config.ru +4 -0
  55. data/spec/dummy/config/application.rb +15 -0
  56. data/spec/dummy/config/boot.rb +5 -0
  57. data/spec/dummy/config/database.yml +14 -0
  58. data/spec/dummy/config/environment.rb +5 -0
  59. data/spec/dummy/db/migrate/.keep +0 -0
  60. data/spec/dummy/db/views/.keep +0 -0
  61. data/spec/generators/scenic/model/model_generator_spec.rb +36 -0
  62. data/spec/generators/scenic/view/view_generator_spec.rb +57 -0
  63. data/spec/integration/revert_spec.rb +74 -0
  64. data/spec/scenic/adapters/postgres/connection_spec.rb +79 -0
  65. data/spec/scenic/adapters/postgres/refresh_dependencies_spec.rb +82 -0
  66. data/spec/scenic/adapters/postgres/views_spec.rb +37 -0
  67. data/spec/scenic/adapters/postgres_spec.rb +209 -0
  68. data/spec/scenic/command_recorder/statement_arguments_spec.rb +41 -0
  69. data/spec/scenic/command_recorder_spec.rb +111 -0
  70. data/spec/scenic/configuration_spec.rb +27 -0
  71. data/spec/scenic/definition_spec.rb +62 -0
  72. data/spec/scenic/schema_dumper_spec.rb +115 -0
  73. data/spec/scenic/statements_spec.rb +199 -0
  74. data/spec/spec_helper.rb +22 -0
  75. data/spec/support/generator_spec_setup.rb +14 -0
  76. data/spec/support/view_definition_helpers.rb +10 -0
  77. metadata +307 -0
@@ -0,0 +1,76 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, sex characteristics, gender identity and
9
+ expression, level of experience, education, socio-economic status, nationality,
10
+ personal appearance, race, religion, or sexual identity and orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at derekprior@gmail.com. All complaints
59
+ will be reviewed and investigated and will result in a response that is deemed
60
+ necessary and appropriate to the circumstances. The project team is obligated to
61
+ maintain confidentiality with regard to the reporter of an incident. Further
62
+ details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
71
+ version 1.4, available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,24 @@
1
+ # Contributing
2
+
3
+ We love contributions from everyone. By participating in this project, you
4
+ agree to abide by our [code of conduct].
5
+
6
+ [code of conduct]: CODE_OF_CONDUCT.md
7
+
8
+ ## Contributing Code
9
+
10
+ 1. Fork the repository.
11
+ 2. Run `bin/setup`, which will install dependencies and create the dummy
12
+ application database.
13
+ 3. Run `rake` to verify that the tests pass against the version of Rails you are
14
+ running locally.
15
+ 4. Make your change with new passing tests, following existing style.
16
+ 5. Write a [good commit message], push your fork, and submit a pull request.
17
+ 6. CI will run the test suite on all configured versions of Ruby and Rails.
18
+ Address any failures.
19
+
20
+ [good commit message]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
21
+
22
+ Others will give constructive feedback. This is a time for discussion and
23
+ improvements, and making the necessary changes will be required before we can
24
+ merge the contribution.
data/Gemfile ADDED
@@ -0,0 +1,16 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in scenic.gemspec
4
+ gemspec
5
+
6
+ rails_version = ENV.fetch("RAILS_VERSION", "6.0")
7
+
8
+ if rails_version == "master"
9
+ rails_constraint = { github: "rails/rails" }
10
+ else
11
+ rails_constraint = "~> #{rails_version}.0"
12
+ end
13
+
14
+ gem "rails", rails_constraint
15
+ gem "sprockets", "< 4.0.0"
16
+ gem "pg", "~> 1.1"
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014-2020 Derek Prior, Caleb Hearth, and thoughtbot.
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,5 @@
1
+ # Scenic
2
+
3
+ Version of Scenic gem for Ruby on Jets
4
+
5
+ u need to add Scenic.load to your initializer
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :smoke do
7
+ exec "spec/smoke"
8
+ end
9
+
10
+ namespace :dummy do
11
+ require_relative "spec/dummy/config/application"
12
+ Dummy::Application.load_tasks
13
+ end
14
+
15
+ task(:spec).clear
16
+ desc "Run specs other than spec/acceptance"
17
+ RSpec::Core::RakeTask.new("spec") do |task|
18
+ task.exclude_pattern = "spec/acceptance/**/*_spec.rb"
19
+ task.verbose = false
20
+ end
21
+
22
+ desc "Run acceptance specs in spec/acceptance"
23
+ RSpec::Core::RakeTask.new("spec:acceptance") do |task|
24
+ task.pattern = "spec/acceptance/**/*_spec.rb"
25
+ task.verbose = false
26
+ end
27
+
28
+ desc "Run the specs and acceptance tests"
29
+ task default: %w(spec spec:acceptance)
data/SECURITY.md ADDED
@@ -0,0 +1,14 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Scenic maintainenance is a volunteer effort. We will do our best to fix
6
+ forward but do not offer backported fixes. As such, the only "supported" version of Scenic is whichever was most recently released.
7
+
8
+ ## Reporting a Vulnerability
9
+
10
+ Please report any discovered security vulnerabilities to Scenic's primary
11
+ volunteer maintainers, derekprior@gmail.com and caleb@calebhearth.com.
12
+
13
+ We will respond as soon as possible with any follow-up questions or details
14
+ on how we plan to handle the issue.
data/bin/rake ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rake", "rake")
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require "pathname"
11
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require "rubygems"
15
+ require "bundler/setup"
16
+
17
+ load Gem.bin_path("rspec-core", "rspec")
data/bin/setup ADDED
@@ -0,0 +1,18 @@
1
+ #!/bin/sh
2
+
3
+ set -e
4
+
5
+ # CI-specific setup
6
+ if [ -n "$GITHUB_ACTIONS" ]; then
7
+ bundle config path vendor/bundle
8
+ bundle config jobs 4
9
+ bundle config retry 3
10
+ git config --global user.name 'GitHub Actions'
11
+ git config --global user.email 'github-actions@example.com'
12
+ fi
13
+
14
+ gem install bundler --conservative
15
+ bundle check || bundle install
16
+
17
+ bundle exec rake dummy:db:drop
18
+ bundle exec rake dummy:db:create
data/bin/yard ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'yard' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('yard', 'yard')
@@ -0,0 +1,12 @@
1
+ module Scenic
2
+ # Scenic provides generators for creating and updating views and ActiveRecord
3
+ # models that are backed by views.
4
+ #
5
+ # See:
6
+ #
7
+ # * {file:lib/generators/scenic/model/USAGE Model Generator}
8
+ # * {file:lib/generators/scenic/view/USAGE View Generator}
9
+ # * {file:README.md README}
10
+ module Generators
11
+ end
12
+ end
@@ -0,0 +1,31 @@
1
+ module Scenic
2
+ module Generators
3
+ # @api private
4
+ module Materializable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ class_option :materialized,
9
+ type: :boolean,
10
+ required: false,
11
+ desc: "Makes the view materialized",
12
+ default: false
13
+ class_option :no_data,
14
+ type: :boolean,
15
+ required: false,
16
+ desc: "Adds WITH NO DATA when materialized view creates/updates",
17
+ default: false
18
+ end
19
+
20
+ private
21
+
22
+ def materialized?
23
+ options[:materialized]
24
+ end
25
+
26
+ def no_data?
27
+ options[:no_data]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,12 @@
1
+ Description:
2
+ Create a new database view and ActiveRecord::Base subclass for your
3
+ application.
4
+
5
+ To create a materialized view, pass the '--materialized' option.
6
+
7
+ Examples:
8
+ rails generate scenic:model search
9
+
10
+ create: app/models/search.rb
11
+ create: db/views/searches_v1.sql
12
+ create: db/migrate/20140803191158_create_searches.rb
@@ -0,0 +1,52 @@
1
+ require "rails/generators"
2
+ require "rails/generators/rails/model/model_generator"
3
+ require "generators/scenic/view/view_generator"
4
+ require "generators/scenic/materializable"
5
+
6
+ module Scenic
7
+ module Generators
8
+ # @api private
9
+ class ModelGenerator < Rails::Generators::NamedBase
10
+ include Scenic::Generators::Materializable
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ def invoke_rails_model_generator
14
+ invoke "model",
15
+ [file_path.singularize],
16
+ options.merge(
17
+ fixture_replacement: false,
18
+ migration: false,
19
+ )
20
+ end
21
+
22
+ def inject_model_methods
23
+ if materialized? && generating?
24
+ inject_into_class "app/models/#{file_path.singularize}.rb", class_name do
25
+ evaluate_template("model.erb")
26
+ end
27
+ end
28
+ end
29
+
30
+ def invoke_view_generator
31
+ invoke "scenic:view", [table_name], options
32
+ end
33
+
34
+ private
35
+
36
+ def evaluate_template(source)
37
+ source = File.expand_path(find_in_source_paths(source.to_s))
38
+ context = instance_eval("binding", __FILE__, __LINE__)
39
+ ERB.new(
40
+ ::File.binread(source),
41
+ nil,
42
+ "-",
43
+ "@output_buffer",
44
+ ).result(context)
45
+ end
46
+
47
+ def generating?
48
+ behavior != :revoke
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,3 @@
1
+ def self.refresh
2
+ Scenic.database.refresh_materialized_view(table_name, concurrently: false, cascade: false)
3
+ end
@@ -0,0 +1,20 @@
1
+ Description:
2
+ Create a new database view for your application. This will create a new
3
+ view definition file and the accompanying migration.
4
+
5
+ If a view of the given name already exists, create a new version of the view
6
+ and a migration to replace the old version with the new.
7
+
8
+ To create a materialized view, pass the '--materialized' option.
9
+ To create a materialized view with NO DATA, pass '--no-data' option.
10
+
11
+ Examples:
12
+ rails generate scenic:view searches
13
+
14
+ create: db/views/searches_v01.sql
15
+ create: db/migrate/20140803191158_create_searches.rb
16
+
17
+ rails generate scenic:view searches
18
+
19
+ create: db/views/searches_v02.sql
20
+ create: db/migrate/20140804191158_update_searches_to_version_2.rb
@@ -0,0 +1,5 @@
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
+ def change
3
+ create_view <%= formatted_plural_name %><%= create_view_options %>
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ class <%= migration_class_name %> < <%= activerecord_migration_class %>
2
+ def change
3
+ <%- if materialized? -%>
4
+ update_view <%= formatted_plural_name %>,
5
+ version: <%= version %>,
6
+ revert_to_version: <%= previous_version %>,
7
+ materialized: <%= no_data? ? "{ no_data: true }" : true %>
8
+ <%- else -%>
9
+ update_view <%= formatted_plural_name %>, version: <%= version %>, revert_to_version: <%= previous_version %>
10
+ <%- end -%>
11
+ end
12
+ end
@@ -0,0 +1,127 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+ require "generators/scenic/materializable"
4
+
5
+ module Scenic
6
+ module Generators
7
+ # @api private
8
+ class ViewGenerator < Rails::Generators::NamedBase
9
+ include Rails::Generators::Migration
10
+ include Scenic::Generators::Materializable
11
+ source_root File.expand_path("templates", __dir__)
12
+
13
+ def create_views_directory
14
+ unless views_directory_path.exist?
15
+ empty_directory(views_directory_path)
16
+ end
17
+ end
18
+
19
+ def create_view_definition
20
+ if creating_new_view?
21
+ create_file definition.path
22
+ else
23
+ copy_file previous_definition.full_path, definition.full_path
24
+ end
25
+ end
26
+
27
+ def create_migration_file
28
+ if creating_new_view? || destroying_initial_view?
29
+ migration_template(
30
+ "db/migrate/create_view.erb",
31
+ "db/migrate/create_#{plural_file_name}.rb",
32
+ )
33
+ else
34
+ migration_template(
35
+ "db/migrate/update_view.erb",
36
+ "db/migrate/update_#{plural_file_name}_to_version_#{version}.rb",
37
+ )
38
+ end
39
+ end
40
+
41
+ def self.next_migration_number(dir)
42
+ ::ActiveRecord::Generators::Base.next_migration_number(dir)
43
+ end
44
+
45
+ no_tasks do
46
+ def previous_version
47
+ @previous_version ||=
48
+ Dir.entries(views_directory_path)
49
+ .map { |name| version_regex.match(name).try(:[], "version").to_i }
50
+ .max
51
+ end
52
+
53
+ def version
54
+ @version ||= destroying? ? previous_version : previous_version.next
55
+ end
56
+
57
+ def migration_class_name
58
+ if creating_new_view?
59
+ "Create#{class_name.tr('.', '').pluralize}"
60
+ else
61
+ "Update#{class_name.pluralize}ToVersion#{version}"
62
+ end
63
+ end
64
+
65
+ def activerecord_migration_class
66
+ if ActiveRecord::Migration.respond_to?(:current_version)
67
+ "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
68
+ else
69
+ "ActiveRecord::Migration"
70
+ end
71
+ end
72
+ end
73
+
74
+ private
75
+
76
+ alias singular_name file_name
77
+
78
+ def file_name
79
+ super.tr(".", "_")
80
+ end
81
+
82
+ def views_directory_path
83
+ @views_directory_path ||= Jets.root.join("db", "views")
84
+ end
85
+
86
+ def version_regex
87
+ /\A#{plural_file_name}_v(?<version>\d+)\.sql\z/
88
+ end
89
+
90
+ def creating_new_view?
91
+ previous_version.zero?
92
+ end
93
+
94
+ def definition
95
+ Scenic::Definition.new(plural_file_name, version)
96
+ end
97
+
98
+ def previous_definition
99
+ Scenic::Definition.new(plural_file_name, previous_version)
100
+ end
101
+
102
+ def destroying?
103
+ behavior == :revoke
104
+ end
105
+
106
+ def formatted_plural_name
107
+ if plural_name.include?(".")
108
+ "\"#{plural_name}\""
109
+ else
110
+ ":#{plural_name}"
111
+ end
112
+ end
113
+
114
+ def create_view_options
115
+ if materialized?
116
+ ", materialized: #{no_data? ? '{ no_data: true }' : true}"
117
+ else
118
+ ""
119
+ end
120
+ end
121
+
122
+ def destroying_initial_view?
123
+ destroying? && version == 1
124
+ end
125
+ end
126
+ end
127
+ end