Topic: Does rake db:schema:dump export data ?

Hello!

I want to export the database structure into one file, so when i should perform a setup.. i run it without going through all migrations i have. I found rake db:schema:dump, It worked fine, but i have some initialization records that i need to be inserted and the previous task exports only empty tables.

My questions is: Since Rails Migrations are able to insert data into the database, how to generate a database dump with all data i have?

Re: Does rake db:schema:dump export data ?

There's no pre-defined rake task that will export the data as well as the schema. After all, how would it know which records are "initialization records" that you need, and which are records that were added after the fact? Migrations are the way to go. If your concern is just to have it in one file, you can copy all the migrations into a single ActiveRecord::Migration class (and therefore a single file) and delete all the current migrations. However, I don't see any advantage to that, except that you are un-cluttering the migrations folder. There will be no performance benefits.

Re: Does rake db:schema:dump export data ?

You're right, Rails should not know which of data I consider as initialization data! Thgat's why i asked for a task that exports the structure + all data:

mrorm wrote:

..with all data i have?

Re: Does rake db:schema:dump export data ?

After returning back to the Rails Recipes book, i found that the only way is to use the proposed little of code to export live data to fixtures (they have written this to avoid manually write fixtures for testing) to create YAML files. Then, to return them back i can use: rake db:fixtures:load after a dumping the schema into the database (rake db:schema:load).

Re: Does rake db:schema:dump export data ?

You could use mysqldump if you're using mysql.

Another alternative is to create your own custom rake task that will load the data.

# lib/tasks/populate.task
namespace :db do
  desc "populate the database with my data"
  task :populate => :environment do
    Item.create!(:name => 'foo')
    #...
  end
end

Then just run rake db:populate.

Railscasts - Free Ruby on Rails Screencasts

Re: Does rake db:schema:dump export data ?

In this case, I'm adding data "manually", but my objective is to load auto-generated data..

Re: Does rake db:schema:dump export data ?

How is it being auto-generated? If it's a ruby script you could put the same ruby in your rake task and have that do the auto-generation. Or maybe I'm missing something...

Railscasts - Free Ruby on Rails Screencasts

Re: Does rake db:schema:dump export data ?

I was talking about the script in the Rails Recipes book. Sure, i could mix it with the customized task that you suggested. And it will be more more elegant!

Thanks to all of you!

Last edited by mrorm (2007-06-24 12:58:42)