我有两个表,一个与我的 rails 应用程序无关,另一个invtypes
与items
我的应用程序关联。
表invtypes
有 20.000 条记录,我需要移动到items
表中。
现在我的第一次尝试是简单地对 invtypes 进行 ActiveRecord 查询以获取所有条目,然后遍历每个条目并Item
通过实例化新的Item model
添加属性然后调用来创建一个新条目model.save
然而,这也做了验证和其他一些减慢的事情,使这个任务执行了 30 分钟,Rails Console
因为我会这样做几次,我不能让它这么慢。
所以我的第二个想法是用普通的旧查询 no Models 尝试这个,但是现在这给出了一个错误:
←[1m←[35m (27.0ms)←[0m INSERT INTO items (typeID, name, description, volume, price, created_at, updated_at) VALUES (0, '#System', '', 0.0, 0.0, '20
13-03-29 04:06:27 +0100', '2013-03-29 04:06:27 +0100');
ActiveRecord::StatementInvalid: Mysql2::Error: Incorrect datetime value: '2013-03-29 04:06:27 +0100' for column 'created_at' at row 1: INSERT INTO ite
ms (typeID, name, description, volume, price, created_at, updated_at) VALUES (0, '#System', '', 0.0, 0.0, '2013-03-29 04:06:27 +0100', '2013-03-29 04:
06:27 +0100');
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `query'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `block in execute'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in
`block in log'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrume
nt'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in
`log'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `execute'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:214:in `e
xecute'
from (irb):21:in `block in irb_binding'
from (irb):19:in `each'
from (irb):19
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
由于时间.now
result = ActiveRecord::Base.connection.select_all( "SELECT * FROM invtypes" )
time = Time.now
result.each do |row|
sql = "INSERT INTO items (typeID, name, description, volume, price, created_at, updated_at) VALUES (#{row["typeID"]}, '#{row["typeName"]}', '#{row["description"]}', #{row["volume"]}, #{row["basePrice"]}, '#{time}', '#{time}');"
ActiveRecord::Base.connection.execute(sql)
end
我如何使用 Ruby/Rails 来解决这个问题,或者我应该用 Java 编写应用程序吗?C 来执行这个查询?
只执行 10 行然后抛出:
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `query'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `block in execute'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in
`block in log'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrume
nt'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in
`log'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:2
33:in `execute'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:214:in `e
xecute'
from (irb):7:in `block in irb_binding'
from (irb):4:in `each'
from (irb):4
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:47:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands/console.rb:8:in `start'
from C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/railties-3.2.1/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'