Forum: Ruby on Rails help me understand migrations movie

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.
61396c69040bbc56dbad1c5c71ac27d2?d=identicon&s=25 Rover Rhubarb (rhubarb)
on 2005-12-22 10:24
I'm somewhat of a newbie - played with recent versions of rails a few
mths ago, but have decided to start from scratch with 1.0.
I want to build my new db as much as possible with migrations, so I
watched DHH's migrations movie from rubyonrails.org.

A few things confuse me:

1. He generates the model ("post") before running rake db_schema_dump.
Is that necessary or just a preference?

2. He says that running db_schema_dump will create not only the schema
file, but the database. He's using sqllite, I'm using MySQL, and it
didnt work for me. That is, rake db_schema_dump issued an error
complaining that the database didnt exist.

I created the database in the MySQL console, tried again and it worked.
So question: Does db_schema_dump only create the db for sqllite, or did
DHH misspeak, or did I misunderstand?

3. DHH then goes into the autogenerated schema.rb and adds code to
create the tables. He notes the warning about not modifying the
autogenerated file, but says "we're going to run with scissors here".
I appreciate what he's doing here, but what concerns me is, is this the
way it's meant to be done? Or is it some shortcut for a quick video?

I'm not criticizing here, I'm just trying to start a project from
scratch and follow the simplest path.

Is creating the tables in the db console before runing db_schema_dump
the proper way?
Is db_schema_dump really only meant to be used do dump out the schema
later?
Should I be creating migrations files to start creating my tables, and
not be using db_schema_dump first?

Also another startup question: I'm starting my project, and I'll be
messing with the table definitions a bit. But I have no data yet and no
real need for migrations - this is all preliminary stuff - so should I
create migrations each time for each new column, etc, or "hard code" it
all in schema.rb, or do it all in the MySQL console.

thanks for any advice offered
3d333b0012928f3dd5a6861cb09ad683?d=identicon&s=25 Kris Leech (Guest)
on 2005-12-22 10:45
I would pretty much ignore schema.rb, and just generate new migration
files, then run "rake migrate" to update your database.

Schema.rb basically is all your migrations in one file, so good for when
you distribute/deploy an application.

Schema.rb gets created when you run db_schema_dump or gets created
automatically if you un-comment  "config.active_record.schema_format =
:ruby" in enviroment.rb.

As far as I am aware you have to create a database first then run rake
migrate. I guess you could have the database created automatically if
you add create_database to the first migration file, but I have not
tried it because some developers may have different database names in
database.yml depending on their local setup, so there is no need to
force a database name, only table/field names.

If your messing with tables to start with you could do it without
migrations using command line or GUI but when you are done do a
db_schema_dump, and copy the code in schema.rb to your first migration
file.

I hope that helps...

Kris.

rover rhubarb wrote:
>
> 1. He generates the model ("post") before running rake db_schema_dump.
> Is that necessary or just a preference?
>
> 2. He says that running db_schema_dump will create not only the schema
> file, but the database. He's using sqllite, I'm using MySQL, and it
> didnt work for me. That is, rake db_schema_dump issued an error
> complaining that the database didnt exist.
>
> I created the database in the MySQL console, tried again and it worked.
> So question: Does db_schema_dump only create the db for sqllite, or did
> DHH misspeak, or did I misunderstand?
>
> 3. DHH then goes into the autogenerated schema.rb and adds code to
> create the tables. He notes the warning about not modifying the
> autogenerated file, but says "we're going to run with scissors here".
> I appreciate what he's doing here, but what concerns me is, is this the
> way it's meant to be done? Or is it some shortcut for a quick video?
>
> I'm not criticizing here, I'm just trying to start a project from
> scratch and follow the simplest path.
>
> Is creating the tables in the db console before runing db_schema_dump
> the proper way?
> Is db_schema_dump really only meant to be used do dump out the schema
> later?
> Should I be creating migrations files to start creating my tables, and
> not be using db_schema_dump first?
>
> Also another startup question: I'm starting my project, and I'll be
> messing with the table definitions a bit. But I have no data yet and no
> real need for migrations - this is all preliminary stuff - so should I
> create migrations each time for each new column, etc, or "hard code" it
> all in schema.rb, or do it all in the MySQL console.
>
> thanks for any advice offered
B45eab4f18aa1bb2a44d6e657531a642?d=identicon&s=25 Alain Ravet (aravet)
on 2005-12-22 11:09
Rover,

If you haven't already, explore the 2 resources at:
   http://wiki.rubyonrails.com/rails/pages/ActiveReco...

Migrations are brilliant.
When creating a project from scratch, all you have to do on the DB
server is create an empty DB. Everything - ? - else is done though
migrations.


I must confess I also found the screencast a little confusing. I watched
it anyway, and learned a few things.

For example, uncomment this line in 'environment.rb'
  config.active_record.schema_format = :ruby

=> after each migration, 'schema.rb' will be updated and show you all
the structures you have created in one place. Invaluable.

There are still a few bugs though.

Alain
This topic is locked and can not be replied to.