I needed to dump my database to fixtures for testing purposes. None of the commonly referenced scripts could do this while preserving UTF8 characters.
I modified this script to use the Ya2YAML utf8-compliant gem. You will need to install Ya2YAML:
sudo gem install ya2yaml
And save this script into lib/tasks, with the name extract_fixtures.rake:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
desc 'Create YAML test fixtures from data in an existing database. Defaults to development database. Set RAILS_ENV to override.' require 'Ya2YAML' task :extract_fixtures => :environment do sql = "SELECT * FROM %s" skip_tables = ["schema_info"] ActiveRecord::Base.establish_connection (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name| i = "000" File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file| data = ActiveRecord::Base.connection.select_all(sql % table_name) file.write data.inject({}) { |hash, record| hash["#{table_name}_#{i.succ!}"] = record hash }.ya2yaml end end end |
You can then run it:
rake extract_fixtures
.. and behold as your fixtures directory is filled with proper unicode YAML files. It will overwrite anything else there, so be careful.
December 14th, 2008 at 7:15 am
Great script, thanks!
January 19th, 2009 at 1:24 am
[...] to YAML problem Here’s a patch for "rake extract_fixtures" that also uses ya2yaml: https://fukamachi.org/wp/2007/05/18/r…eserving-utf8/ Saving YAML as UTF-8 should really be default behavior. UnicodeDammit! — Posted via [...]
February 6th, 2009 at 9:12 am
Sweet, just used this to create my test fixtures.
Thanks,
Steven
November 17th, 2009 at 7:06 am
Thanks the script! It run without hassles.