Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

SQLite3Adapter now checks for views in table_exists? fixes: 14041 #14056

Merged
merged 1 commit into from

3 participants

@girishso

Added check for database views in table_exists? in SQLite3Adapter

Fixes #14041

@dmathieu
Collaborator

I'm afraid there are a lot of unexpected repercussions here.
What if the view is readonly for example?
Also, what about other database adapters? MySQL takes only tables.

@girishso

@dmathieu MySQL show tables; by default includes the views.
Also, as mentioned in #14041 (comment) the same is true for Postgres.

@senny senny added the activerecord label
@senny senny self-assigned this
@senny senny merged commit bbf6df7 into rails:master

1 check passed

Details default The Travis CI build passed
@senny
Owner

While writing these tests it became obvious that the SQLite3Adapter is behaving different from the other adapters.

@girishso thank you :yellow_heart:

@girishso

Woohoo my first rails commit!! :)

Thanks @senny! wondering how you found this 7 month old pull request!

@senny
Owner

@girishso working through my todo-list. Sadly there's still stuff on it that's 7 months old. Thank you for your patience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
6 activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* SQLite3Adapter now checks for views in `table_exists?`
+
+ Fixes #14041
+
+ *Girish Sonawane*
+
* When inverting add_index use the index name if present instead of
the columns.
View
2  activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb
@@ -369,7 +369,7 @@ def tables(name = nil, table_name = nil) #:nodoc:
sql = <<-SQL
SELECT name
FROM sqlite_master
- WHERE type = 'table' AND NOT name = 'sqlite_sequence'
+ WHERE (type = 'table' OR type = 'view') AND NOT name = 'sqlite_sequence'
SQL
sql << " AND name = #{quote_table_name(table_name)}" if table_name
View
10 activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
@@ -276,6 +276,16 @@ def test_transaction
assert_equal 0, @conn.select_rows(count_sql).first.first
end
+ def test_views
+ assert_equal %w{ items }, @conn.tables
+
+ @conn.execute <<-eosql
+ CREATE VIEW items_view AS
+ select id from items;
+ eosql
+ assert @conn.table_exists?('items_view')
+ end
+
def test_tables
assert_equal %w{ items }, @conn.tables
Something went wrong with that request. Please try again.