Skip to content

Commit

Permalink
mysql drop_db rake task no longer prompts for confirmation
Browse files Browse the repository at this point in the history
fixed niggly bug in rake specs: something is wrong with the loading of rails tasks, see comments
  • Loading branch information
bradrobertson committed Dec 6, 2011
1 parent 82525ef commit 831393f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
15 changes: 10 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Bundler::GemHelper.install_tasks
require "rspec"
require "rspec/core/rake_task"

RSpec::Core::RakeTask.new(:spec => %w{postgres:drop_db postgres:build_db}) do |spec|
RSpec::Core::RakeTask.new(:spec => "db:test:prepare") do |spec|
spec.pattern = "spec/**/*_spec.rb"
end

Expand All @@ -21,6 +21,12 @@ end

task :default => :spec

namespace :db do
namespace :test do
task :prepare => %w{postgres:drop_db postgres:build_db mysql:drop_db mysql:build_db}
end
end

namespace :postgres do
require 'active_record'
require "#{File.join(File.dirname(__FILE__), 'spec', 'support', 'config')}"
Expand All @@ -29,7 +35,7 @@ namespace :postgres do
task :build_db do
%x{ createdb -E UTF8 #{pg_config['database']} } rescue "test db already exists"
ActiveRecord::Base.establish_connection pg_config
load 'spec/dummy/db/schema.rb'
ActiveRecord::Migrator.migrate('spec/dummy/db/migrate')
end

desc "drop the PostgreSQL test database"
Expand All @@ -48,14 +54,13 @@ namespace :mysql do
task :build_db do
%x{ mysqladmin -u root create #{my_config['database']} } rescue "test db already exists"
ActiveRecord::Base.establish_connection my_config
load 'spec/dummy/db/schema.rb'
ActiveRecord::Migrator.migrate('spec/dummy/db/migrate')
end

desc "drop the MySQL test database"
task :drop_db do
puts "dropping database #{my_config['database']}"
# TODO how can I run this and force answering y to the "are you sure?"
%x{ mysqladmin -u root drop #{my_config['database']} }
%x{ mysqladmin -u root drop #{my_config['database']} --force}
end

end
Expand Down
8 changes: 7 additions & 1 deletion spec/dummy/db/migrate/20110613152810_create_dummy_models.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CreateDummyModels < ActiveRecord::Migration
def change
def self.up
create_table :companies do |t|
t.boolean :dummy
t.string :database
Expand Down Expand Up @@ -28,4 +28,10 @@ def change

end

def self.down
drop_table :companies
drop_table :users
drop_table :delayed_jobs
end

end
8 changes: 7 additions & 1 deletion spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110613152810) do
ActiveRecord::Schema.define(:version => 20111202022214) do

create_table "books", :force => true do |t|
t.string "name"
t.integer "pages"
t.datetime "published"
end

create_table "companies", :force => true do |t|
t.boolean "dummy"
Expand Down
9 changes: 6 additions & 3 deletions spec/integration/apartment_rake_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
Rake.application = @rake
Dummy::Application.load_tasks

# stub out rails tasks (that modify the schema.rb)
Rake::Task.define_task('db:migrate')
Rake::Task.define_task('db:rollback')
# somehow this misc.rake file gets lost in the shuffle
# it defines a `rails_env` task that our db:migrate depends on
# No idea why, but during the tests, we somehow lose this tasks, so we get an error when testing migrations
# This is STUPID!
load "rails/tasks/misc.rake"
end

after do
Expand Down Expand Up @@ -56,6 +58,7 @@
end

@rake['apartment:rollback'].invoke
@rake['apartment:migrate'].invoke # migrate again so that our next test 'seed' can run (requires migrations to be complete)
end
end

Expand Down
4 changes: 0 additions & 4 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@

config.include RSpec::Integration::CapybaraSessions, :type => :request

config.before(:suite) do
load Rails.root.join('db','schema.rb') # Ensure we have some of our test tables loaded
end

config.before(:all) do
# Ensure that each test starts with a clean connection
# Necessary as some tests will leak things like current_schema into the next test
Expand Down

0 comments on commit 831393f

Please sign in to comment.