diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-09-09 11:21:23 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-09-09 11:26:30 +0200 |
commit | ae9412e857a78236b359eb1b636511d07fe45cf3 (patch) | |
tree | aa29021dfe5cdbaa3b3ea72f7c8e52922b5ce393 /activerecord | |
parent | 2dbfd7783277936c71664365a01cc20b77d6326c (diff) | |
download | rails-ae9412e857a78236b359eb1b636511d07fe45cf3.tar.gz rails-ae9412e857a78236b359eb1b636511d07fe45cf3.tar.bz2 rails-ae9412e857a78236b359eb1b636511d07fe45cf3.zip |
introduce `connection.supports_views?` and basic view tests.
`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
Diffstat (limited to 'activerecord')
6 files changed, 64 insertions, 4 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9ff2e2bb56..4fe979750e 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -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. diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 2ee5a88f2a..a0d9086875 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -240,6 +240,11 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index 3a24ecac77..e72055c9e9 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -197,6 +197,10 @@ module ActiveRecord true end + def supports_views? + version[0] >= 5 + end + def native_database_types NATIVE_DATABASE_TYPES end @@ -779,10 +783,6 @@ module ActiveRecord 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 diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 52a03d0e6d..80461f3910 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -158,6 +158,10 @@ module ActiveRecord true end + def supports_views? + true + end + def index_algorithms { concurrently: 'CONCURRENTLY' } end diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 4481df974d..5eb9385a47 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -183,6 +183,10 @@ module ActiveRecord true end + def supports_views? + true + end + def active? @active != false end diff --git a/activerecord/test/cases/view_test.rb b/activerecord/test/cases/view_test.rb new file mode 100644 index 0000000000..aef6ad2296 --- /dev/null +++ b/activerecord/test/cases/view_test.rb @@ -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" + 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 |