Skip to content

Commit

Permalink
introduce connection.supports_views? and basic view tests.
Browse files Browse the repository at this point in the history
`AbstractAdapter#supports_views?` defaults to `false` so we have to turn it on
in adapter subclasses. Currently the flag only controls test execution.

/cc @yahonda
  • Loading branch information
senny committed Sep 9, 2014
1 parent 2dbfd77 commit ae9412e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 4 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Introduce `connection.supports_views?` to check wether the current adapter
has support for SQL views. Connection adapters should define this method.

*Yves Senn*

* Allow included modules to override association methods.

Fixes #16684.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ def supports_foreign_keys?
false
end

# Does this adapter support views?
def supports_views?
false
end

# This is meant to be implemented by the adapters that support extensions
def disable_extension(name)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ def supports_foreign_keys?
true
end

def supports_views?
version[0] >= 5
end

def native_database_types
NATIVE_DATABASE_TYPES
end
Expand Down Expand Up @@ -779,10 +783,6 @@ def mariadb?
full_version =~ /mariadb/i
end

def supports_views?
version[0] >= 5
end

def supports_rename_index?
mariadb? ? false : (version[0] == 5 && version[1] >= 7) || version[0] >= 6
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ def supports_foreign_keys?
true
end

def supports_views?
true
end

def index_algorithms
{ concurrently: 'CONCURRENTLY' }
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ def supports_add_column?
true
end

def supports_views?
true
end

def active?
@active != false
end
Expand Down
42 changes: 42 additions & 0 deletions activerecord/test/cases/view_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require "cases/helper"
require "models/book"

if ActiveRecord::Base.connection.supports_views?
class ViewWithPrimaryKeyTest < ActiveRecord::TestCase
fixtures :books

class Ebook < ActiveRecord::Base
self.primary_key = "id"
end

setup do
@connection = ActiveRecord::Base.connection
@connection.execute <<-SQL
CREATE VIEW ebooks
AS SELECT id, name, status FROM books WHERE format = 'ebook'
SQL
end

teardown do
@connection.execute "DROP VIEW IF EXISTS ebooks"

This comment has been minimized.

Copy link
@yahonda

yahonda Sep 9, 2014

Member

@senny Thanks for the update and notify. Oracle database supports Views so this flag will be set to true.
Unfortunately Oracle does not support drop if exists statement. #16778 I'm wondering if there is view_exists? or similar method.

This comment has been minimized.

Copy link
@senny

senny Sep 9, 2014

Author Member

@yahonda oh I didn't know about that. I'll update the test-case accordingly.

end

def test_reading
books = Ebook.all
assert_equal [books(:rfr).id], books.map(&:id)
assert_equal ["Ruby for Rails"], books.map(&:name)
end

def test_table_exists
skip "SQLite does not currently treat views as tables" if current_adapter?(:SQLite3Adapter)
view_name = Ebook.table_name
assert @connection.table_exists?(view_name), "'#{view_name}' table should exist"
end

def test_column_definitions
assert_equal([["id", :integer],
["name", :string],
["status", :integer]], Ebook.columns.map { |c| [c.name, c.type] })
end
end
end

0 comments on commit ae9412e

Please sign in to comment.