Skip to content

Instantly share code, notes, and snippets.

@serg-kovalev
Forked from babatakao/extract_fixtures.rake
Last active August 29, 2015 14: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 serg-kovalev/84a897f1337c21f5b58a to your computer and use it in GitHub Desktop.
Save serg-kovalev/84a897f1337c21f5b58a to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
# DBからデータを取り出してYAMLにする。生成したYAMLはtmp/fixturesに保存される
namespace :db do
namespace :fixtures do
desc "Extract database data to tmp/fixtures directory."
task :extract => :environment do
fixtures_dir = "#{Rails.root}/tmp/fixtures/"
skip_tables = ["schema_info", "schema_migrations", "sessions"]
ActiveRecord::Base.establish_connection
FileUtils.mkdir_p(fixtures_dir)
if ENV['FIXTURES']
table_names = ENV['FIXTURES'].split(/,/)
else
table_names = (ActiveRecord::Base.connection.tables - skip_tables)
end
table_names.each do |table_name|
begin
model = table_name.classify.constantize # check class existing
rescue NameError
Object.const_set table_name.classify, Class.new(ApplicationModel)
retry
end
if model.columns.any?{|c| c.name == 'id'}
sql = "SELECT * FROM #{table_name} ORDER BY id ASC"
else
sql = "SELECT * FROM #{table_name}"
end
File.open("#{fixtures_dir}#{table_name}.yml", "w") do |file|
objects = ActiveRecord::Base.connection.select_all(sql)
objects.each_with_index do |obj, i|
# not nullのカラムがnullになっていることがあるので、その場合は空文字列を入れておく
model.columns.each do |col|
if !col.null && obj[col.name].nil?
obj[col.name] = ''
end
end
file.write({"#{table_name}#{i}" => obj}.to_yaml.sub('---', ''))
file.write "\n"
end
end
puts "extracted #{table_name}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment