From: Shaun McDonald Date: Thu, 6 Nov 2008 17:15:22 +0000 (+0000) Subject: Doing the update version part of the migration in chunks of 10000 instead of all... X-Git-Tag: live~7625^2~221 X-Git-Url: https://git.openstreetmap.org/rails.git/commitdiff_plain/6855b2604cb356b814d7853fb56988924af24eb3 Doing the update version part of the migration in chunks of 10000 instead of all at once so that MySQL doesn't choke with running out of table locks. --- diff --git a/db/migrate/018_move_to_innodb.rb b/db/migrate/018_move_to_innodb.rb index c551b0ef8..d17da8fd5 100644 --- a/db/migrate/018_move_to_innodb.rb +++ b/db/migrate/018_move_to_innodb.rb @@ -19,8 +19,23 @@ class MoveToInnodb < ActiveRecord::Migration @@ver_tbl.each { |tbl| add_column "current_#{tbl}", "version", :bigint, :limit => 20, :null => false - execute "UPDATE current_#{tbl} SET version = " + - "(SELECT max(version) FROM #{tbl} WHERE #{tbl}.id = current_#{tbl}.id)" + # As the initial version of all nodes, ways and relations is 0, we set the + # current version to something less so that we can update the version in + # batches of 10000 + tbl.classify.constantize.update_all("version=-1") + while tbl.classify.constantize.count(:conditions => {:version => -1}) > 0 + tbl.classify.constantize.update_all("version=(SELECT max(version) FROM #{tbl} WHERE #{tbl}.id = current_#{tbl}.id)", {:version => -1}, :limit => 10000) + end + # execute "UPDATE current_#{tbl} SET version = " + + # "(SELECT max(version) FROM #{tbl} WHERE #{tbl}.id = current_#{tbl}.id)" + # The above update causes a MySQL error: + # -- add_column("current_nodes", "version", :bigint, {:null=>false, :limit=>20}) + # -> 1410.9152s + # -- execute("UPDATE current_nodes SET version = (SELECT max(version) FROM nodes WHERE nodes.id = current_nodes.id)") + # rake aborted! + # Mysql::Error: The total number of locks exceeds the lock table size: UPDATE current_nodes SET version = (SELECT max(version) FROM nodes WHERE nodes.id = current_nodes.id) + + # The above rails version will take longer, however will no run out of locks } end