Permalink
Browse files

Fixed that schema changes while the database was open would break any…

… connections to a SQLite database (now we reconnect if that error is throw) [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@3997 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent f340df7 commit df62dea1ff1461322e54bba7bbbee227a3e32b4e @dhh dhh committed Mar 20, 2006
Showing with 16 additions and 2 deletions.
  1. +2 −0 activerecord/CHANGELOG
  2. +14 −2 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
View
@@ -1,5 +1,7 @@
*SVN*
+* Fixed that schema changes while the database was open would break any connections to a SQLite database (now we reconnect if that error is throw) [DHH]
+
* Don't classify the has_one class when eager loading, it is already singular. Add tests. (closes #4117) [jonathan@bluewire.net.nz]
* Quit ignoring default :include options in has_many :through calls [Mark James]
@@ -135,6 +135,13 @@ def quote_column_name(name) #:nodoc:
def execute(sql, name = nil) #:nodoc:
log(sql, name) { @connection.execute(sql) }
+ rescue ActiveRecord::StatementInvalid => exception
+ if exception.message =~ /database schema has changed/
+ reconnect!
+ retry
+ else
+ raise
+ end
end
def update(sql, name = nil) #:nodoc:
@@ -341,13 +348,18 @@ class SQLite2Adapter < SQLiteAdapter # :nodoc:
#
# SELECT COUNT(ArtistID) FROM (SELECT DISTINCT ArtistID FROM CDs);
def execute(sql, name = nil) #:nodoc:
+ super(rewrite_count_distinct_queries(sql), name)
+ end
+
+ def rewrite_count_distinct_queries(sql)
if sql =~ /count\(distinct ([^\)]+)\)( AS \w+)? (.*)/i
distinct_column = $1
distinct_query = $3
column_name = distinct_column.split('.').last
- sql = "SELECT COUNT(#{column_name}) FROM (SELECT DISTINCT #{distinct_column} #{distinct_query})"
+ "SELECT COUNT(#{column_name}) FROM (SELECT DISTINCT #{distinct_column} #{distinct_query})"
+ else
+ sql
end
- log(sql, name) { @connection.execute(sql) }
end
end

0 comments on commit df62dea

Please sign in to comment.