Hi Sirs,
Many tutorials over the web shows how to load data from fixtures. But, if you want to migrate your mysql data to a postgresql database?
We can export our data to fixtures, and after that we can load the fixtures to every database management system we wish.
Let's go to the rake task:
desc "export the database models to YML fixtures" task(:models_to_fixtures => :environment) do ActiveRecord::Base.establish_connection( :adapter => 'postgresql', # mysql, sqlite3 :encoding => 'utf8', :database => 'cnxs_development', :username => 'username', :password => 'secret' ) ActiveRecord::Base.connection if ENV['MODELS'].nil? || ENV['MODELS'].blank? raise "Please enter valid models names separated by coma. Ex: MODELS=User,Account" end models = ENV['MODELS'].split(',').collect { |arg| arg.camelize.constantize } models.each do |model| output = {} collection = model.find(:all) collection.each do |object| output.store(object.to_param, object.attributes) end file_path = "#{RAILS_ROOT}/tmp/#{model.table_name}.yml" # /tmp/ File.open(file_path, "w+") { |file| file.write(output.to_yaml) } end end
Here you need to fill your database attributes like you do in the /config/database.yml.
You will call the command line:
$ rake models_to_fixtures MODELS=<your model names separated by coma> For example:
$ rake models_to_fixtures MODELS=User,PostAll your model data will be stored on /tmp/ directory.
Download this rake task here.
See you.