Forum: Ruby on Rails portable db table definitions?

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.
petermichaux (Guest)
on 2005-11-16 07:34
(Received via mailing list)
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
Rick W. (Guest)
on 2005-11-16 19:50
In a rails app you can supply a parameter to the establish_connection
call that specifies a section in the database.yaml file.

Rick
petermichaux (Guest)
on 2005-11-16 20:24
(Received via mailing list)
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
Rick (Guest)
on 2005-11-17 02:26
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
petermichaux (Guest)
on 2005-11-17 03:15
(Received via mailing list)
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
Tim.Ferrell (Guest)
on 2005-11-17 16:53
(Received via mailing list)
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 http://api.rubyonrails.com/classes/ActiveRecord/Mi...

HTH,
Tim
petermichaux (Guest)
on 2005-11-17 18:36
(Received via mailing list)
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
This topic is locked and can not be replied to.