Ruby Forum Ruby on Rails > From "schema.rb" file to MySQL

Posted by David B. (dendicus)
on 12.05.2008 00:11
Hello,

I have write a schema.rb database such as this:

ActiveRecord::Schema.define do
  create_table :arts do |t|
    t.string :name, :null => false, :limit => 45
  end
end

This file is saved into test/db/schema.rb, and test/config/database.yml
is correctly configured with "test_development" database created in a
MySQL server.

Could you help me to install the content of schema.rb file in MySQL and
in my Rails 2 application?

Thank you for your help.

Regards
Posted by James Byrne (byrnejb)
on 12.05.2008 00:25
David B. wrote:
> Hello,
> 
> I have write a schema.rb database such as this:
> 
> ActiveRecord::Schema.define do
>   create_table :arts do |t|
>     t.string :name, :null => false, :limit => 45
>   end
> end
> 
> This file is saved into test/db/schema.rb, and test/config/database.yml
> is correctly configured with "test_development" database created in a
> MySQL server.
> 

Well, this is a rather awkward way of doing things. Effectively, 
db/schema.rb is a composite migration file.  To load it one would 
typically move it to something called similar to 
db/migrate/001_initial_db_load.rb and run rake db:migrate.  When one 
runs the rake task, depending upon the settings in 
config/environment.rb, then db/schema.rb gets overwritten with the 
results of the applied migrations, making direct editing of this file 
rather pointless.

It would be far better for you to read up on migrations and to use 
individual migrations contained in db/migrate instead.
Posted by Rimantas Liubertas (Guest)
on 12.05.2008 00:55
(Received via mailing list)
<...>
> Well, this is a rather awkward way of doing things. Effectively,
> db/schema.rb is a composite migration file.  To load it one would
> typically move it to something called similar to
> db/migrate/001_initial_db_load.rb and run rake db:migrate.

To load schema.rb. you do not need to move or rename anything,
just use rake db:schema:load

Regards,
Rimantas
--
http://rimantas.com/
Posted by David B. (dendicus)
on 12.05.2008 01:21
Rimantas Liubertas wrote:
> <...>
>> Well, this is a rather awkward way of doing things. Effectively,
>> db/schema.rb is a composite migration file.  To load it one would
>> typically move it to something called similar to
>> db/migrate/001_initial_db_load.rb and run rake db:migrate.
> 
> To load schema.rb. you do not need to move or rename anything,
> just use rake db:schema:load
> 
> Regards,
> Rimantas
> --
> http://rimantas.com/

Wow! This command work perfectly, thanks a lot! :)
Posted by David B. (dendicus)
on 12.05.2008 15:26
By the way, I just have a last question: is there a command to 
automaticly create all the model for any tables of my database? I ask 
this because my database have 16 tables and I don't think using 
"script/generate model <a table>" 16 times is a good idea.
Posted by Greg Donald (destiney)
on 13.05.2008 06:30
(Received via mailing list)
On Mon, May 12, 2008 at 8:26 AM, David B.
<rails-mailing-list@andreas-s.net> wrote:
>
>  By the way, I just have a last question: is there a command to
>  automaticly create all the model for any tables of my database? I ask
>  this because my database have 16 tables and I don't think using
>  "script/generate model <a table>" 16 times is a good idea.

for x in `echo "show tables" | mysql my_db`; do script/generate model 
$x; done


--
Greg Donald
http://destiney.com/
Posted by Greg Donald (destiney)
on 13.05.2008 06:40
(Received via mailing list)
On Mon, May 12, 2008 at 11:29 PM, Greg Donald <gdonald@gmail.com> wrote:
>  for x in `echo "show tables" | mysql my_db`; do script/generate model $x; done

Oops, I forgot to remove the 's' from the table name, model names
usually being singular and all:

for x in `echo "show tables" | mysql my_db | sed 's/s$//'`; do
script/generate model $x; done


--
Greg Donald
http://destiney.com/
Posted by Kyle (Guest)
on 13.05.2008 18:01
(Received via mailing list)
That should work well as long as you don't have a singular/plural pair
that doesn't use "+s" for the plural (e.g., pony, ponies).  You could
probably route your command through pluralize, but in that case, it
would be easier just to type in the 16 commands.  :)

-Kyle
Posted by David B. (dendicus)
on 13.05.2008 18:32
Thanks :)