Rails: dump database to fixtures preserving UTF8

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.

4 Responses to “Rails: dump database to fixtures preserving UTF8”

  1. Anders Says:

    Great script, thanks!

  2. Saving UTF-8 characters to YAML problem | keyongtech Says:

    [...] 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 [...]

  3. Steven Hansen Says:

    Sweet, just used this to create my test fixtures.

    Thanks,
    Steven

  4. Chris Strobel Says:

    Thanks the script! It run without hassles.

Leave a Reply