Permalink
Browse files

The 'create_db' Rake task will now use the connection details in test…

…/database.yml instead of hardcoded connection details.
  • Loading branch information...
1 parent 98333cc commit 1d1957dbe624ac1add1cb3862e859af7ed4de12e @FooBarWidget FooBarWidget committed Aug 29, 2008
Showing with 33 additions and 17 deletions.
  1. +25 −14 Rakefile
  2. +7 −2 test/database.yml.example
  3. +1 −1 test/test_helper.rb
View
@@ -26,35 +26,46 @@ task :changelog do
`git log | grep -v git-svn-id > History.txt`
end
+def load_database_yml
+ filename = "test/database.yml"
+ if !File.exist?(filename)
+ STDERR.puts "\n*** ERROR ***:\n" <<
+ "You must have a 'test/database.yml' file in order to create the test database. " <<
+ "An example is provided in 'test/database.yml.example'.\n\n"
+ exit 1
+ end
+ YAML::load(ERB.new(IO.read(filename)).result)
+end
+
def setup_connection
require 'active_record'
- ENV['RAILS_ENV'] = 'test'
-
- ActiveRecord::Base.configurations = { 'test' => { :adapter => 'mysql', :host => 'localhost', :database => 'mysql' } }
- ActiveRecord::Base.establish_connection 'test'
+ ActiveRecord::Base.configurations = load_database_yml
+ ActiveRecord::Base.establish_connection('fiveruns_city_austin_test_master')
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ ActiveRecord::Base.logger.level = Logger::DEBUG
end
-def using_connection(&block)
+def using_connection(database_identifier, &block)
ActiveRecord::Base.connection.instance_eval(&block)
end
def setup(create = false)
setup_connection
-
- databases = %w( vr_austin_master vr_austin_slave vr_dallas_master vr_dallas_slave )
- databases.each do |db|
- using_connection do
+
+ ActiveRecord::Base.configurations.each_pair do |identifier, config|
+ using_connection(identifier) do
+ db_name = config['database']
if create
- execute "drop database if exists #{db}"
- execute "create database #{db}"
+ execute "drop database if exists #{db_name}"
+ execute "create database #{db_name}"
end
- execute "use #{db}"
+ execute "use #{db_name}"
execute "drop table if exists the_whole_burritos"
execute "drop table if exists enchiladas"
execute "create table enchiladas (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
- execute "insert into enchiladas (id, name) values (1, '#{db}')"
+ execute "insert into enchiladas (id, name) values (1, '#{db_name}')"
execute "create table the_whole_burritos (id integer not null auto_increment, name varchar(30) not null, primary key(id))"
- execute "insert into the_whole_burritos (id, name) values (1, '#{db}')"
+ execute "insert into the_whole_burritos (id, name) values (1, '#{db_name}')"
end
end
end
@@ -1,6 +1,11 @@
# The unit tests make use of the data populated in these databases.
-# The database identifiers (e.g. "fiveruns_city_austin_test_master") MUST NOT
-# be changed! Everything else may be changed.
+#
+# Notes:
+# - The database identifiers (e.g. "fiveruns_city_austin_test_master") MUST NOT
+# be changed! Everything else may be changed.
+# - The user defined for "fiveruns_city_austin_test_master" MUST have the
+# privilege to create and drop databases and tables.
+
fiveruns_city_austin_test_master:
adapter: mysql
View
@@ -22,7 +22,7 @@
def load_database_yml
filename = DATABASE_YML_PATH
- ActiveRecord::Base.configurations = YAML::load(ERB.new(IO.read(filename)).result)
+ YAML::load(ERB.new(IO.read(filename)).result)
end
if !File.exist?(DATABASE_YML_PATH)

0 comments on commit 1d1957d

Please sign in to comment.