Portable db table definitions?

Hi,

I’ve been playing with ActiveRecords create_table. I created a single
file
that I call from the command line using

ruby -rubygems db/tables.rb

I’ve attached the file below. I was just wondering if I am on the right
track with how this was intended to be used or if there is a better way.
I
can’t find any docs. I don’t feel very DRY since I am restating my
connection parameters in this file.

Thanks!
Peter

require ‘active_record’

ActiveRecord::Base.establish_connection({
:adapter => ‘mysql’,
:host => ‘localhost’,
:database => ‘tshirtshop_development’,
:username => ‘dev’
})

ActiveRecord::Schema.define do

drop_table :authors

create_table :authors do |t|
t.column :name, :string, :null => false
end

add_index :authors, :name, :unique

drop_table :posts

create_table :posts do |t|
t.column :author_id, :integer, :null => false
t.column :subject, :string
t.column :body, :text
t.column :private, :boolean, :default => false
end

add_index :posts, :author_id

end

In a rails app you can supply a parameter to the establish_connection
call that specifies a section in the database.yaml file.

Rick

Since you are posting in the rails forum I figured you were dealing with
a rails app. I’m kind of new at this so I’m just trying to kind of
point you in the right direction.

Rails uses a app/config/database.yml file to define the database
connection and I was suggesting you might want to use it also.

Rick

I’m not using a Rails app to define my tables. That ruby script I posted
is
stand alone. Maybe I am going at this all wrong.
Do people not use the Rails methods to describe their databases to
ensure
portability? I thought that we would be avoiding the SQL dialect
problems by
using ActiveRecord to define the tables and then YML file to populate
them.
Thanks,
Peter

Hi Rick,

Sorry, I wasn’t clear. I am using a Rails application. But the scipt I
posted is a free standing script that I run form the command line.

I suppose I want to do exactly what you are suggesting: use
database.yml.
But I don’t know how to call that. My table definition script is
currently
in the rails app db directory. How can I call the database.yml file from
my
script?

Thanks,
Peter

If this is for a rails app, why not use migrations? No need, then, to
duplicate the connection info as it gets pulled from database.yml and
you can gracefully handle schema changes over time…

See Peak Obsession

HTH,
Tim

Hi Tim,

Thanks for the info and the link. It looks like Migration should be what
I
use. For the create table method if I use the options hash for something
like “DEFAULT CHARSET=UTF-8” will that be portable between MySQL and
postgresql and future databases supported by migrations?

Thanks again,
Peter