Skip to content

Commit

Permalink
Updated to work with AR5
Browse files Browse the repository at this point in the history
  • Loading branch information
boazy committed Sep 13, 2016
1 parent 792351d commit 5245c35
Show file tree
Hide file tree
Showing 18 changed files with 43 additions and 70 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
---
sudo: false
rvm:
- 2.1.5
- 2.3.1
gemfile:
- gemfiles/activerecord-4.2/Gemfile.mysql2
- gemfiles/activerecord-4.2/Gemfile.postgresql
- gemfiles/activerecord-4.2/Gemfile.sqlite3
- gemfiles/activerecord-5.0/Gemfile.mysql2
- gemfiles/activerecord-5.0/Gemfile.postgresql
- gemfiles/activerecord-5.0/Gemfile.sqlite3
env: POSTGRESQL_DB_USER=postgres MYSQL_DB_USER=travis
addons:
postgresql: '9.4'
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ SchemaPlus::Views is tested on:

<!-- SCHEMA_DEV: MATRIX - begin -->
<!-- These lines are auto-generated by schema_dev based on schema_dev.yml -->
* ruby **2.1.5** with activerecord **4.2**, using **mysql2**, **sqlite3** or **postgresql**
* ruby **2.3.1** with activerecord **5.0**, using **mysql2**, **sqlite3** or **postgresql**

<!-- SCHEMA_DEV: MATRIX - end -->

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
eval File.read File.expand_path('../../Gemfile.base', __FILE__)

gem "activerecord", "~> 4.2.6"
gem "activerecord", "~> 5.0.0"
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require "pathname"
eval(Pathname.new(__FILE__).dirname.join("Gemfile.base").read, binding)

platform :ruby do
gem "mysql2", '>= 0.3.18', '< 0.5'
gem "mysql2"
end

platform :jruby do
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module SchemaPlus::Views
module ActiveRecord
module ConnectionAdapters
module Mysql2Adapter

def views(name = nil)
SchemaMonkey::Middleware::Schema::Views.start(connection: self, query_name: name, views: []) { |env|
select_all("SELECT table_name FROM information_schema.views WHERE table_schema = SCHEMA()", env.query_name).each do |row|
env.views << row["table_name"]
end
# Views is now natively supported by AR5.
# This definition just wraps the native view implementation with a
# middleware
def views
SchemaMonkey::Middleware::Schema::Views.start(connection: self, views: []) { |env|
env.views += super
}.views
end

Expand All @@ -26,7 +26,6 @@ def view_definition(view_name, name = nil)
end
}.definition
end

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@ module SchemaPlus::Views
module ActiveRecord
module ConnectionAdapters
module PostgresqlAdapter

def views(name = nil) #:nodoc:
SchemaMonkey::Middleware::Schema::Views.start(connection: self, query_name: name, views: []) { |env|
sql = <<-SQL
SELECT viewname
FROM pg_views
WHERE schemaname = ANY (current_schemas(false))
AND viewname NOT LIKE 'pg\_%'
SQL
sql += " AND schemaname != 'postgis'" if adapter_name == 'PostGIS'
env.views += env.connection.query(sql, env.query_name).map { |row| row[0] }
# Views is now natively supported by AR5.
# This definition just wraps the native view implementation with a
# middleware
def views
SchemaMonkey::Middleware::Schema::Views.start(connection: self, views: []) { |env|
env.views += super
}.views
end

Expand All @@ -28,7 +23,6 @@ def view_definition(view_name, name = nil) #:nodoc:
env.definition = row.first.chomp(';').strip unless row.nil?
}.definition
end

end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ module SchemaPlus::Views
module ActiveRecord
module ConnectionAdapters
module Sqlite3Adapter

def views(name = nil)
SchemaMonkey::Middleware::Schema::Views.start(connection: self, query_name: name, views: []) { |env|
env.views += env.connection.execute("SELECT name FROM sqlite_master WHERE type='view'", env.query_name).collect{|row| row["name"]}
# Views is now natively supported by AR5.
# This definition just wraps the native view implementation with a
# middleware
def views
SchemaMonkey::Middleware::Schema::Views.start(connection: self, views: []) { |env|
env.views += super
}.views
end

Expand All @@ -16,7 +18,6 @@ def view_definition(view_name, name = nil)
env.definition = sql
}.definition
end

end
end
end
Expand Down
24 changes: 1 addition & 23 deletions lib/schema_plus/views/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,12 @@ def assemble(stream)
end
end

module Schema
module Tables

module Mysql
def after(env)
Tables.filter_out_views(env)
end
end

module Sqlite3
def after(env)
Tables.filter_out_views(env)
end
end

def self.filter_out_views(env)
env.tables -= env.connection.views(env.query_name)
end
end
end

#
# Define new middleware stacks patterned on SchemaPlus::Core's naming
# for tables

module Schema
module Views
ENV = [:connection, :query_name, :views]
ENV = [:connection, :views]
end
module ViewDefinition
ENV = [:connection, :view_name, :query_name, :definition]
Expand Down
2 changes: 1 addition & 1 deletion lib/schema_plus/views/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module SchemaPlus
module Views
VERSION = "0.3.1"
VERSION = "1.0.0"
end
end
4 changes: 2 additions & 2 deletions schema_dev.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ruby:
- 2.1.5
- 2.3.1
activerecord:
- 4.2
- 5.0
db:
- mysql2
- sqlite3
Expand Down
5 changes: 3 additions & 2 deletions schema_plus_views.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency "activerecord", "~> 4.2"
gem.add_dependency "schema_plus_core", "~> 1.0"
gem.add_dependency "activerecord", "5.0"
gem.add_dependency "schema_plus_core", "~> 2.0"

gem.add_development_dependency "bundler", "~> 1.7"
gem.add_development_dependency "rake", "~> 10.0"
gem.add_development_dependency "rspec", "~> 3.0"
gem.add_development_dependency "schema_plus_compatibility", "~> 0.2"
gem.add_development_dependency "schema_dev", "~> 3.6"
gem.add_development_dependency "simplecov"
gem.add_development_dependency "simplecov-gem-profile"
Expand Down
2 changes: 1 addition & 1 deletion spec/dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def view_re(name, re)

def define_schema_and_data
connection.views.each do |view| connection.drop_view view end
connection.tables.each do |table| connection.drop_table table, cascade: true end
connection.tables_only.each do |table| connection.drop_table table, cascade: true end

schema.define do

Expand Down
6 changes: 3 additions & 3 deletions spec/introspection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class Item < ActiveRecord::Base
end

it "should not be listed as a table" do
expect(connection.tables).not_to include('a_ones')
expect(connection.tables).not_to include('ab_ones')
expect(connection.tables_only).not_to include('a_ones')
expect(connection.tables_only).not_to include('ab_ones')
end

it "should introspect definition" do
Expand Down Expand Up @@ -71,7 +71,7 @@ class Item < ActiveRecord::Base

def define_schema_and_data
connection.views.each do |view| connection.drop_view view end
connection.tables.each do |table| connection.drop_table table, cascade: true end
connection.tables_only.each do |table| connection.drop_table table, cascade: true end

schema.define do

Expand Down
5 changes: 2 additions & 3 deletions spec/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ def after(env)

context TestMiddleware::Middleware::Schema::Views do
it "calls middleware" do
expect(spy_on {connection.views 'qn'}).to eq({
expect(spy_on {connection.views}).to eq({
#connection: connection,
views: ['a_view'],
query_name: 'qn'
views: ['a_view']
})
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/migration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ABOnes < ActiveRecord::Base

describe "rollback" do
it "properly rolls back a create_view" do
m = Class.new ::ActiveRecord::Migration do
m = Class.new ::ActiveRecord::Migration.latest_version do
define_method(:change) {
create_view :copy, "SELECT * FROM items"
}
Expand All @@ -81,7 +81,7 @@ class ABOnes < ActiveRecord::Base
end

it "raises error for drop_view" do
m = Class.new ::ActiveRecord::Migration do
m = Class.new ::ActiveRecord::Migration.latest_version do
define_method(:change) {
drop_view :a_ones
}
Expand All @@ -95,7 +95,7 @@ class ABOnes < ActiveRecord::Base

def define_schema_and_data
connection.views.each do |view| connection.drop_view view end
connection.tables.each do |table| connection.drop_table table, cascade: true end
connection.tables_only.each do |table| connection.drop_table table, cascade: true end

schema.define do

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

require 'rspec'
require 'active_record'
require 'schema_plus_compatibility'
require 'schema_plus_views'
require 'schema_dev/rspec'

Expand All @@ -18,7 +19,7 @@
config.warnings = true
config.around(:each) do |example|
ActiveRecord::Migration.suppress_messages do
ActiveRecord::Base.connection.tables.each do |table|
ActiveRecord::Base.connection.tables_only.each do |table|
ActiveRecord::Migration.drop_table table, force: :cascade
end
ActiveRecord::Base.connection.views.each do |view|
Expand Down

0 comments on commit 5245c35

Please sign in to comment.