scenic_oracle_enhanced_adapter 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 12ca6e91010fefc44df470e59229c911e608d6f5f6ea050e4c21c6ac6e4d7198
4
+ data.tar.gz: 260fbde5b27fad913f07c36287c40dabf71f0406070a15108cabffdfdd8eb642
5
+ SHA512:
6
+ metadata.gz: d0fff688ccefa1a2ece721a45bfb300e9d52b8c24184460c82fc638327859dc31feb410922da25a844940ad598256de70b3b583e64cf230396a5cbbd62485f57
7
+ data.tar.gz: 518440fb6bb3851161df8beb6ea4911428f45f72fb4736e16465b0081648371c0db7a17fcc5662d35bd1d7c4475ef287954bedf2882b5b18639d4d48a8a6bded
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
13
+ .idea/*
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.15.4
data/CHANGELOG ADDED
@@ -0,0 +1,4 @@
1
+ v0.0.2
2
+ - Make `create_view` and `create_materialized_view` reversible
3
+ v0.0.3
4
+ - Allow `create materialized view` to create a view without running it
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in scenic_oracle_enhanced_adapter.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Stephen Margheim
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,46 @@
1
+ # ScenicOracleEnhancedAdapter
2
+
3
+ This gem provides an adapter for the [Scenic gem](https://github.com/thoughtbot/scenic) for use with the [Oracle Enhanced Adapter](https://github.com/rsim/oracle-enhanced/).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'scenic_oracle_enhanced_adapter'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Then add a Scenic initializer (or modify your existing initializer) to make Scenic use this adapter:
18
+
19
+ ```
20
+ # config/initializers/scenic.rb
21
+
22
+ Scenic.configure do |config|
23
+ config.database = Scenic::Adapters::OracleEnhanced.new
24
+ end
25
+ ```
26
+
27
+ ## Switching from rails_sql_views to Scenic
28
+
29
+ In Oracle 12, replacing a view has issues when the view refers to objects outside its own schema; use update_view, which will drop and create. The replace_view method can still be used for views referring to objects in their own schema.
30
+
31
+ 1. Run `bundle exec rails g scenic:view <full_view_name>` (if you use a prefix for the view, include it).
32
+ 1. Move the view's SQL definition from the original migration into the newly-created file in db/views.
33
+ 1. In the original migration, delete any drop_view call.
34
+ 1. Copy the create_view or update_view line from the scenic migration and substitute it for the old create_view method and block.
35
+ 1. Delete the scenic migration file
36
+ 1. For the down method, copy the update_view line into the down (replacing any drop_ and create_view methods) and make the version: argument to the same number as the revert_to_version: argument (e.g. `update_view :complex_data_view, version: 2, revert_to_version: 1` becomes `update_view :complex_data_view, version: 1, revert_to_version: 1`)
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/PMACS/scenic_oracle_enhanced_adapter.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'scenic_oracle_enhanced_adapter'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,80 @@
1
+ module Scenic
2
+ module Adapters
3
+ class OracleEnhanced
4
+ class Views
5
+ def initialize(connection)
6
+ @connection = connection
7
+ @current_schema = fetch_current_schema
8
+ end
9
+
10
+ def all
11
+ views_from_oracle.map(&method(:to_scenic_view))
12
+ end
13
+
14
+ private
15
+
16
+ attr_reader :connection,
17
+ :current_schema
18
+
19
+ def fetch_current_schema
20
+ ActiveRecord::Base.connection
21
+ .execute("select sys_context('userenv', 'current_schema') from dual")
22
+ .fetch
23
+ .first
24
+ end
25
+
26
+ def views_from_oracle
27
+ # NOTE: Oracle cannot `UNION` on `LONG` fields,
28
+ # and the `all_mviews.query` field and `all_views.text` field
29
+ # are both `LONG` fields. Thus, we must use `UNION ALL`.
30
+ # Luckily, we are not here concerned about duplicates.
31
+ cursor = ActiveRecord::Base.connection.select_all(<<-SQL)
32
+ SELECT 'm' AS KIND,
33
+ mview_name AS VIEWNAME,
34
+ owner AS NAMESPACE,
35
+ query AS DEFINITION
36
+ FROM all_mviews
37
+ WHERE owner = '#{@current_schema}'
38
+ UNION ALL
39
+ SELECT 'v' AS KIND,
40
+ view_name AS VIEWNAME,
41
+ owner AS NAMESPACE,
42
+ text AS DEFINITION
43
+ FROM all_views
44
+ WHERE owner = '#{@current_schema}'
45
+ SQL
46
+ end
47
+
48
+ def to_scenic_view(result)
49
+ namespace, viewname = result['namespace'], result['viewname']
50
+
51
+ if namespace != 'SYS'
52
+ namespaced_viewname = "#{oracle_identifier(namespace)}.#{oracle_identifier(viewname)}"
53
+ else
54
+ namespaced_viewname = oracle_identifier(viewname)
55
+ end
56
+
57
+ Scenic::View.new(
58
+ name: namespaced_viewname,
59
+ definition: result['definition'].strip,
60
+ materialized: result['kind'] == 'm'
61
+ )
62
+ end
63
+
64
+ def oracle_identifier(name)
65
+ # We've copied this method from the PostgreSQL Scenic adapter.
66
+ # If the name makes it past the guard clause, the quoted Oracle identifiers
67
+ # will end up causing the OEA's `describe` method to generate
68
+ # invalid, improperly quoted object names.
69
+ #
70
+ # We don't know where this return value is used so, until we have
71
+ # an actual use case for a name that makes it past this guard, we
72
+ # are going to leave this as it is.
73
+ return name if name =~ /^[a-zA-Z_][a-zA-Z0-9_]*$/
74
+
75
+ connection.quote_column_name_or_expression(name)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,73 @@
1
+ require_relative 'oracle_enhanced/views'
2
+
3
+ module Scenic
4
+ module Adapters
5
+ # An adapter for managing Oracle views.
6
+ class OracleEnhanced
7
+ def initialize(connectable = ActiveRecord::Base)
8
+ @connectable = connectable
9
+ end
10
+
11
+ def views
12
+ Views.new(connection).all
13
+ end
14
+
15
+ def create_view(name, sql_definition)
16
+ execute "CREATE VIEW #{quote_table_name(name)} AS #{sql_definition}"
17
+ end
18
+
19
+ def update_view(name, sql_definition)
20
+ drop_view(name)
21
+ create_view(name, sql_definition)
22
+ end
23
+
24
+ def replace_view(name, sql_definition)
25
+ execute "CREATE OR REPLACE VIEW #{quote_table_name(name)} AS #{sql_definition}"
26
+ end
27
+
28
+ # `_sql_definition` argument allows `create_view` to be reversed in a migration rollback
29
+ def drop_view(name, _sql_definition = nil)
30
+ execute "DROP VIEW #{quote_table_name(name)}"
31
+ end
32
+
33
+ def create_materialized_view(name, sql_definition, no_data: false)
34
+ execute <<~SQL
35
+ CREATE MATERIALIZED VIEW #{quote_table_name(name)}
36
+ #{"BUILD DEFERRED\n" if no_data}AS
37
+ #{sql_definition}
38
+ SQL
39
+ end
40
+
41
+ def update_materialized_view(name, sql_definition, no_data: false)
42
+ # IndexReapplication.new(connection: connection).on(name) do
43
+ drop_materialized_view(name)
44
+ create_materialized_view(name, sql_definition, no_data: no_data)
45
+ # end
46
+ end
47
+
48
+ # `_sql_definition` argument allows `create_materialized_view` to be reversed in a migration rollback
49
+ def drop_materialized_view(name, _sql_definition = nil)
50
+ execute "DROP MATERIALIZED VIEW #{quote_table_name(name)}"
51
+ end
52
+
53
+ def refresh_materialized_view(name, concurrently: false, cascade: false)
54
+ # refresh_dependencies_for(name) if cascade
55
+
56
+ if concurrently
57
+ execute "REFRESH MATERIALIZED VIEW CONCURRENTLY #{quote_table_name(name)}"
58
+ else
59
+ execute "REFRESH MATERIALIZED VIEW #{quote_table_name(name)}"
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :connectable
66
+ delegate :execute, :quote_table_name, to: :connection
67
+
68
+ def connection
69
+ connectable.connection
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ require 'active_record'
2
+ require 'scenic'
3
+ require 'scenic/adapters/oracle_enhanced'
@@ -0,0 +1,37 @@
1
+
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'scenic_oracle_enhanced_adapter'
7
+ spec.version = '0.0.4'
8
+ spec.authors = ['Stephen Margheim']
9
+ spec.email = ['margheim@mail.med.upenn.edu']
10
+
11
+ spec.summary = 'OracleEnhanced adapter for Scenic gem.'
12
+ spec.homepage = 'https://github.com/PMACS/scenic_oracle_enhanced_adapter'
13
+ spec.license = 'MIT'
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
+ else
20
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
21
+ 'public gem pushes.'
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
25
+ f.match(%r{^(test|spec|features)/})
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_development_dependency 'bundler', '>= 2.2.10'
32
+ spec.add_development_dependency 'rake', '~> 12.3'
33
+ spec.add_development_dependency 'rspec', '~> 3.0'
34
+
35
+ spec.add_dependency 'activerecord', '>= 4.0.0'
36
+ spec.add_dependency 'scenic', '>= 1.4.0'
37
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: scenic_oracle_enhanced_adapter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Margheim
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-05-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.2.10
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.2.10
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '12.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '12.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: scenic
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.4.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.4.0
83
+ description:
84
+ email:
85
+ - margheim@mail.med.upenn.edu
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CHANGELOG
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/scenic/adapters/oracle_enhanced.rb
101
+ - lib/scenic/adapters/oracle_enhanced/views.rb
102
+ - lib/scenic_oracle_enhanced_adapter.rb
103
+ - scenic_oracle_enhanced_adapter.gemspec
104
+ homepage: https://github.com/PMACS/scenic_oracle_enhanced_adapter
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ allowed_push_host: https://rubygems.org
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubygems_version: 3.0.3
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: OracleEnhanced adapter for Scenic gem.
128
+ test_files: []