Skip to content

Instantly share code, notes, and snippets.

@remi
Created December 5, 2013 13:13
Show Gist options
  • Save remi/7804942 to your computer and use it in GitHub Desktop.
Save remi/7804942 to your computer and use it in GitHub Desktop.
`db:migrate:data` task
# Migrate data from two ActiveRecord databases.
# Works with ActiveRecord 3 and above.
#
# Written by Rémi Prévost
# Based on the work of Rama McIntosh & Mike Castleman
#
# This task is released under this BSD license:
#
# Copyright (c) 2008, Matson Systems, Inc.
# Copyright (c) 2010, Roasted Vermicelli LLC.
# Copyright (c) 2013, Mirego, Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Matson Systems, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.namespace :db do
namespace :db do
namespace :migrate do
task data: :environment do
puts '-------'
puts 'Migrating all data from an old database to a new one. All data in the new one will be deleted.'
puts "Old: #{ENV['OLD_DATABASE_URL']}"
puts "New: #{ENV['NEW_DATABASE_URL']}"
puts '-------'
# Use separate models to avoid using application-specific code
class OldModelClass < ActiveRecord::Base; end
class NewModelClass < ActiveRecord::Base; end
ActiveRecord::Base.establish_connection ENV['OLD_DATABASE_URL']
tables = ActiveRecord::Base.connection.tables - %w(schema_info schema_migrations)
tables.each do |table_name|
# Configure the old table
OldModelClass.table_name = table_name
OldModelClass.reset_column_information
# Configure the new table
NewModelClass.table_name = table_name
NewModelClass.establish_connection ENV['NEW_DATABASE_URL']
NewModelClass.reset_column_information
NewModelClass.record_timestamps = false
# Remove all data in the new table
NewModelClass.delete_all
print "Migrating #{table_name}..."
columns = nil
# Loop through all records (in batches of 1000) and insert them into the new table
OldModelClass.find_in_batches do |models|
values = models.inject([]) do |memo, model|
attrs = model.attributes
columns ||= attrs.keys.map { |value| NewModelClass.connection.quote_column_name(value) }.join(',')
memo << '(' + attrs.values.map { |value| NewModelClass.connection.quote(value) }.join(',') + ')'
end
# Insert those 1000 records
NewModelClass.transaction do
NewModelClass.connection.execute("INSERT INTO #{NewModelClass.connection.quote_table_name(table_name)} (#{columns}) VALUES #{values.join(',')}")
end
end
# Reset the sequence of a table's primary key to the maximum value
NewModelClass.connection.reset_pk_sequence!(table_name) if NewModelClass.connection.respond_to?(:reset_pk_sequence!)
puts " #{OldModelClass.count} records migrated."
end
puts 'Done!'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment