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
This commit is contained in:
David Heinemeier Hansson
2006-03-20 06:05:16 +00:00
parent f340df798e
commit df62dea1ff
2 changed files with 16 additions and 2 deletions

View File

@@ -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]

View File

@@ -135,6 +135,13 @@ module ActiveRecord
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 @@ module ActiveRecord
#
# 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