Hi all, I''m too lazy to run the table creation scripts, so I have created a unit test that doesn''t do any actual testing, it just creates the tables for the other tests. The filename is "aaa_create_tables_test.rb", so that when rake runs the tests in alphabetical order, it will run this test first (I don''t know if rake guarantees that the tests will run in alphabetical order, but it seems to work, at least on Windows). For my own purposes, I have hard coded the test to use MySql: class CreateTablesTest < Test::Unit::TestCase def setup # This method is required by rake. end def run_sql_file(connection, path) sql_file = SqlFile.new(path) sql_file.each_statement { |statement| begin ActiveRecord::Base.connection.execute(statement) # Skip errors. If there is a problem creating the tables then it will show up in other tests. rescue ActiveRecord::StatementInvalid end } end def test_table_creation run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/mysql.drop.sql" run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/mysql.sql" assert_equal 1,1 end end I am thinking that it would be useful to modify the connection adapters so that they can return a string to identify themselves, so that the correct sql script can be run for any adapters. Is there already an easy way to do this that does not require modifying the adapters? Adelle.
Adelle Hartley wrote:> I''m too lazy to run the table creation scripts, so I have created a unit > test that doesn''t do any actual testing, it just creates the tables for the > other tests. > > The filename is "aaa_create_tables_test.rb", so that when rake runs the > tests in alphabetical order, it will run this test first (I don''t know if > rake guarantees that the tests will run in alphabetical order, but it seems > to work, at least on Windows).Quite the virtue! You may also use a rake task to reload the tables, then make the test tasks depend on it.> I am thinking that it would be useful to modify the connection adapters so > that they can return a string to identify themselves, so that the correct > sql script can be run for any adapters. > > Is there already an easy way to do this that does not require modifying the > adapters?ActiveRecord::Base.configurations is the Hash loaded from config/database.yml. You may establish connections by name: ActiveRecord::Base.establish_connection :test jeremy
Hi Jeremy,> > Is there already an easy way to do this that does not require > > modifying the adapters? > > ActiveRecord::Base.configurations is the Hash loaded from > config/database.yml. You may establish connections by name: > ActiveRecord::Base.establish_connection :testI don''t think I have conveyed my problem correctly. At the moment, I am only running the tests on MySql. There is a part of my test that does this: run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/mysql.sql" I want to run the tests on multiple adapters, so I want something that goes something like this: run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + ActiveRecord::Base.connection.type + ".sql" But I Ruby does not seem to like it if I treat the return value of "type" as a string. What am I missing? Adelle.
Hi Adelle, Try looking at: ActiveRecord::Base.connection.class.to_s it will return something like: ActiveRecord::ConnectionAdapters::MysqlAdapter I guess you can do some string-modification to find out which DB adapter it is ;) Regards, Flurin Egger Adelle Hartley wrote:>Hi Jeremy, > > > >>>Is there already an easy way to do this that does not require >>>modifying the adapters? >>> >>> >>ActiveRecord::Base.configurations is the Hash loaded from >>config/database.yml. You may establish connections by name: >> ActiveRecord::Base.establish_connection :test >> >> > >I don''t think I have conveyed my problem correctly. > >At the moment, I am only running the tests on MySql. > >There is a part of my test that does this: > > run_sql_file ActiveRecord::Base.connection, >"test/fixtures/db_definitions/mysql.sql" > >I want to run the tests on multiple adapters, so I want something that goes >something like this: > > run_sql_file ActiveRecord::Base.connection, >"test/fixtures/db_definitions/" + ActiveRecord::Base.connection.type + >".sql" > >But I Ruby does not seem to like it if I treat the return value of "type" as >a string. > >What am I missing? > >Adelle. > > > > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails > >
Adelle Hartley wrote:> Hi Jeremy, >>ActiveRecord::Base.configurations is the Hash loaded from >>config/database.yml. You may establish connections by name: >> ActiveRecord::Base.establish_connection :test > > I don''t think I have conveyed my problem correctly. > > I want to run the tests on multiple adapters, so I want something that goes > something like this: > > run_sql_file ActiveRecord::Base.connection, > "test/fixtures/db_definitions/" + ActiveRecord::Base.connection.type + > ".sql"I see. Apologies for the quick reply. Use RAILS_ENV to look up database config. ActiveRecord::Base.configurations[RAILS_ENV][:adapter] This is not foolproof since user may switch connections on the fly. The connection adapters deserve an adapter_name class method. You can also parse ActiveRecord::Base.connection.class.name.demodulize.sub(/Adapter$/, '''').downcase, but this fails for SQLite since versions 2 and 3 share an adapter and old the old Ruby bindings trigger use of a separate, deprecated adapter. jeremy
Jeremy Kemper wrote:> This is not foolproof since user may switch connections on > the fly. The connection adapters deserve an adapter_name > class method.Would it be more Ruby-ish to call the method "name" or "adapter_name" ? Adelle.
Adelle Hartley wrote:> Jeremy Kemper wrote: >>This is not foolproof since user may switch connections on >>the fly. The connection adapters deserve an adapter_name >>class method. > > Would it be more Ruby-ish to call the method "name" or "adapter_name" ?name is taken by Class. Perhaps simpler to make configuration options a class attribute so you may do connection.configuration[:adapter] jeremy
Jeremy Kemper wrote:> Adelle Hartley wrote: > > Jeremy Kemper wrote: > >>This is not foolproof since user may switch connections on > the fly. > >>The connection adapters deserve an adapter_name class method. > > > > Would it be more Ruby-ish to call the method "name" or > "adapter_name" ? > > name is taken by Class. Perhaps simpler to make > configuration options a class attribute so you may do > connection.configuration[:adapter]How I see it: connection.configuration[:adapter] would give you the name of the adapter that was requested. connection.adapter_name would give you the name of the adapter that Active Record gave you. In my patch, I have implemented the latter but not the former. http://dev.rubyonrails.com/ticket/587 Adelle.