Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@awesome
Created August 31, 2016 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awesome/658c06db72a6b09e9b686e09613d1dec to your computer and use it in GitHub Desktop.
Save awesome/658c06db72a6b09e9b686e09613d1dec to your computer and use it in GitHub Desktop.
OG: Redo your migrations in Rails and keep your data over 8 years ago Quite often early in a project I find myself editing migration scripts, and wanting to keep my development data. https://samsaffron.com/archive/2008/02/02/Redo+your+migrations+in+Rails+and+keep+your+data
def interesting_tables
rval = ActiveRecord::Base.connection.tables.sort
rval.reject! do |tbl|
['schema_migrations','schema_info', 'sessions', 'public_exceptions'].include?(tbl)
end
rval
end
namespace :db do
namespace :backup do
desc "Reload the database and rerun migrations"
task :redo do
Rake::Task['db:backup:write'].invoke
Rake::Task['db:drop'].invoke
Rake::Task['db:create'].invoke
Rake::Task['db:migrate'].invoke
Rake::Task['db:backup:read'].invoke
end
desc "Dump entire db."
task :write => :environment do
dir = RAILS_ROOT + '/db/backup'
FileUtils.mkdir_p(dir)
FileUtils.chdir(dir)
interesting_tables.each do |tbl|
klass = tbl.classify.constantize
puts "Writing #{tbl}..."
File.open("#{tbl}.yml", 'w+') { |f| YAML.dump klass.find(:all).collect(&:attributes), f }
end
FileUtils.chdir(RAILS_ROOT)
end
desc "Loads the entire db."
task :read => [:environment, 'db:schema:load'] do
dir = RAILS_ROOT + '/db/backup'
FileUtils.mkdir_p(dir)
FileUtils.chdir(dir)
interesting_tables.each do |tbl|
ActiveRecord::Base.transaction do
begin
klass = tbl.classify.constantize
klass.destroy_all
klass.reset_column_information
puts "Loading #{tbl}..."
YAML.load_file("#{tbl}.yml").each do |fixture|
data = {}
klass.columns.each do |c|
# filter out missing columns
data[c.name] = fixture[c.name] if fixture[c.name]
end
ActiveRecord::Base.connection.execute "INSERT INTO #{tbl} (#{data.keys.map{|kk| "#{tbl}.#{kk}"}.join(",")}) VALUES (#{data.values.collect { |value| ActiveRecord::Base.connection.quote(value) }.join(",")})", 'Fixture Insert'
end
rescue
puts "failed to load table #{tbl}"
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment