diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-01-01 09:33:03 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-01-01 09:33:03 -0700 |
commit | 3fd78fc569077b3b844fea2cfc0e3115fef4c2ac (patch) | |
tree | 329ea3953920990b22b6bf37b47643bb0066d710 /activerecord/lib | |
parent | c5e5dbed1c2f9af86f737e672ec9af1415571165 (diff) | |
download | rails-3fd78fc569077b3b844fea2cfc0e3115fef4c2ac.tar.gz rails-3fd78fc569077b3b844fea2cfc0e3115fef4c2ac.tar.bz2 rails-3fd78fc569077b3b844fea2cfc0e3115fef4c2ac.zip |
Don't load an entire table into memory to copy it on SQLite
SQL has mechanisms we can use to copy data from one table into another.
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb | 19 |
1 files changed, 4 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb index 67b2c6b1e6..f3d2b25bfe 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite3_adapter.rb @@ -577,23 +577,12 @@ module ActiveRecord rename.each { |a| column_mappings[a.last] = a.first } from_columns = columns(from).collect(&:name) columns = columns.find_all{|col| from_columns.include?(column_mappings[col])} + from_columns_to_copy = columns.map { |col| column_mappings[col] } quoted_columns = columns.map { |col| quote_column_name(col) } * ',' + quoted_from_columns = from_columns_to_copy.map { |col| quote_column_name(col) } * ',' - quoted_to = quote_table_name(to) - - raw_column_mappings = Hash[columns(from).map { |c| [c.name, c] }] - - exec_query("SELECT * FROM #{quote_table_name(from)}").each do |row| - sql = "INSERT INTO #{quoted_to} (#{quoted_columns}) VALUES (" - - column_values = columns.map do |col| - quote(row[column_mappings[col]], raw_column_mappings[col]) - end - - sql << column_values * ', ' - sql << ')' - exec_query sql - end + exec_query("INSERT INTO #{quote_table_name(to)} (#{quoted_columns}) + SELECT #{quoted_from_columns} FROM #{quote_table_name(from)}") end def sqlite_version |