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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
In a rails app you can supply a parameter to the establish_connection call that specifies a section in the database.yaml file. Rick -- Posted via http://www.ruby-forum.com/.
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 On 11/16/05, Rick Williams <willirl-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > In a rails app you can supply a parameter to the establish_connection > call that specifies a section in the database.yaml file. > > Rick > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 -- Posted via http://www.ruby-forum.com/.
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 _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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/Migration.html HTH, Tim Peter Michaux wrote:> 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 > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 On 11/17/05, Tim <Tim.Ferrell-gUTHP3170uhWk0Htik3J/w@public.gmane.org> wrote:> > 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/Migration.html > > HTH, > Tim > > Peter Michaux wrote: > > 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 > > ------------------------------------------------------------------------ > > > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails