Database independent representation of schema

Guys,

Been away from Rails for awhile but am finally diving back in.

I know there had been some discussion of (and I have been logging for)
adding definition of a database schema in a file outside of the
database,
a la Hibernate.

In other words, in Hibernate you define your object relationships in
external xml files and you can then use the packaged tools (hbm2ddl) to
generate DDL for the target database.

Why is this nice? First, you go about designing your application truly
thinking about the objects, and not the tables, from the get go (to a
certain extent). But secondly, and most important, I don’t have to
rewrite
the DDL each time I switch from say, Hsqldb (for demos), to PostgreSQL
(production).

Has there been any movement on things in this regard in Rails?

Thanks for the info.

John

Hi,

You may want to have a look at migrations that allow you to define it
in a database agnostic way for postgres and mysql.

http://wiki.rubyonrails.com/rails/pages/UnderstandingMigrations
http://api.rubyonrails.com/classes/ActiveRecord/Migration.html

If you need foreign key enforcement then you will need to look at
“Foreign Key Schema Dumper Support” on
http://wiki.rubyonrails.com/rails/pages/Plugins


HTHs,

Peter D.

Peter D. said:

You may want to have a look at migrations that allow you to define it
in a database agnostic way for postgres and mysql.

Thanks Peter. This is definitely a step in the right direction. It seems
somewhat green in terms of features (not yet supporting NOT NULL, etc),
but definitely workable. Too bad it doesn’t support sqlite…we use it
for
demo for our rails app while the production version runs on PostgreSQL.

Thanks for the pointer!

John

I don’t know much about migrations yet but the following two points made
me
wonder.

On 12/4/05, John W. wrote:

It seems somewhat green in terms of features (not yet supporting NOT
NULL, etc)

what about “:null => false”?

Too bad it doesn’t support sqlite

From http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations

“Migrations are database agnostic. What this means is that you can write
your description of the database in ruby -once- and have it easily
translate
to MySQL, PostgreSQL or SQLite out of the box.”

-Peter

Currently I use raw SQL for DDL because I can also populate/seed my
database
in the same file. It is nice because my INSERT statements can be written
directly below my CREATE TABLE statements.

If I switch to using schema.rb to define my database what is the best
way to
seed it?. I’d like to type one command to both define and seed the
database.

Thanks,
Peter

On 12/3/05, John W. [email protected] wrote:

generate DDL for the target database.

Why is this nice? First, you go about designing your application truly
thinking about the objects, and not the tables, from the get go (to a
certain extent). But secondly, and most important, I don’t have to rewrite
the DDL each time I switch from say, Hsqldb (for demos), to PostgreSQL
(production).

Has there been any movement on things in this regard in Rails?

You’re looking for migrations and schemas. You still define your
relationships and all that in the code. Schemas and migrations use
the database agnostic methods of the Connection Adapter for the DDL
queries. It makes it a snap to develop and test in different
databases, support multiple ones, and make iterative changes.

Kevin wrote a great article on Migrations themselves. The actual
methods are the same for schemas. Just type rake db_schema_dump to
see the autogenerated schema for your current database.

http://glu.ttono.us/articles/2005/10/27/the-joy-of-migrations


rick
http://techno-weenie.net

Hi,

On 12/5/05, Peter M. [email protected] wrote:

Currently I use raw SQL for DDL because I can also populate/seed my database
in the same file. It is nice because my INSERT statements can be written
directly below my CREATE TABLE statements.

If I switch to using schema.rb to define my database what is the best way
to seed it?. I’d like to type one command to both define and seed the
database.

You can populate your database using the model object directly. ie

MyFoo.create( :field1 => ‘blah’, :field2 => 2 )

or

foo = MyFoo.new
foo.field1 = ‘blah’
foo.field2 = 2
foo.save


Cheers,

Peter D.

On Dec 4, 2005, at 10:35 AM, Peter M. wrote:

Currently I use raw SQL for DDL because I can also populate/seed my
database in the same file. It is nice because my INSERT statements
can be written directly below my CREATE TABLE statements.

If I switch to using schema.rb to define my database what is the
best way to seed it?. I’d like to type one command to both define
and seed the database.

See the second example in the [ActiveRecord::Migration documentation]
1.

–Steve