Forum: Ruby on Rails Can I export db data to fixtures?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Arthur C. (Guest)
on 2008-12-02 04:14
Hi Guys,

Is there any way to export db data to fixtures files?

(like the reverse of  "rake db:fixtures:load")

Thanks,
Arthur
Brian H. (Guest)
on 2008-12-02 05:12
(Received via mailing list)
Yeah there are a few plugins to do that. There's one called
ar_fixtures that should work just fine.  I don't know if it's
supported any more but I know it still works fine.

On Mon, Dec 1, 2008 at 8:14 PM, Arthur C.
Arthur C. (Guest)
on 2008-12-02 09:00
Brian H. wrote:
> Yeah there are a few plugins to do that. There's one called
> ar_fixtures that should work just fine.  I don't know if it's
> supported any more but I know it still works fine.
>
> On Mon, Dec 1, 2008 at 8:14 PM, Arthur C.

Thanks Brian, it seems to be what I want. :)
Bobnation (Guest)
on 2008-12-02 16:19
(Received via mailing list)
I'm curious as to what you are using this for.

On Dec 2, 1:00 am, Arthur C. <removed_email_address@domain.invalid>
Arthur C. (Guest)
on 2008-12-03 11:08
Bobnation wrote:
> I'm curious as to what you are using this for.
>
> On Dec 2, 1:00�am, Arthur C. <removed_email_address@domain.invalid>

It is difficult to handle the data with relationships between different
tables. So, it is better to start the creation of fixtures from using
existing data.

Also, it is useful for us to generate demo site.
Arthur C. (Guest)
on 2008-12-03 11:10
I just modified the plugin (the ar_fixtures.rake file) to allow output
all tables:

<code>
# arthur: if "rake db:data:dump MODEL=ALL" then load the following
models
# put all your tables need to export here
all_modelsS = ['Album', 'Asset',...
              'UserContact', 'UserInfo', 'User']

def env_or_raise(var_name, human_name)
  if ENV[var_name].blank?
    raise "No #{var_name} value given. Set #{var_name}=#{human_name}"
  # arthur: allow the ALL input
  elsif var_name == 'MODEL' && ENV[var_name] == 'ALL'
    return all_models
  else
    return ENV[var_name]
  end
  #else
  #  return ENV[var_name]
  #end
end


# arthur: check if all model is needed
def all_models

  return all_modelsS if ENV['MODEL'] == 'ALL'
  input = env_or_raise('MODEL', 'ModelName')
  return [input]
end

def model_or_raise

  return env_or_raise('MODEL', 'ModelName')
end

def limit_or_nil_string
  ENV['LIMIT'].blank? ? 'nil' : ENV['LIMIT']
end

namespace :db do
  namespace :fixtures do
    desc "Dump data to the test/fixtures/ directory. Use MODEL=ModelName
and LIMIT (optional)"
    task :dump => :environment do
      # arthur, allow all models input
      all_models.each do | model|
        eval "#{model}.to_fixture(#{limit_or_nil_string})"
      end
      # eval "#{model_or_raise}.to_fixture(#{limit_or_nil_string})"
    end
  end

  namespace :data do
    desc "Dump data to the db/ directory. Use MODEL=ModelName and LIMIT
(optional)"
    task :dump => :environment do
      # arthur, allow all models input
      all_models.each do | model|
        eval "#{model}.dump_to_file(nil, #{limit_or_nil_string})"
        puts "#{model} has been dumped to the db folder."
      end
    end

    desc "Load data from the db/ directory. Use MODEL=ModelName"
    task :load => :environment do
      # arthur, allow all models input
       all_models.each do | model|
        eval "#{model}.load_from_file"
      end
      #eval "#{model_or_raise}.load_from_file"
    end
  end
end

</code>
This topic is locked and can not be replied to.