Skip to content

Commit

Permalink
Added create_db and destroy_db tasks in the Rakefile to make it easie…
Browse files Browse the repository at this point in the history
…r to load the dumped structure into the database #1587 [Sam Stephenson]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1701 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 5, 2005
1 parent 25c33a9 commit 91afb92
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 58 deletions.
2 changes: 2 additions & 0 deletions railties/CHANGELOG
@@ -1,5 +1,7 @@
*SVN* *SVN*


* Added create_db and destroy_db tasks in the Rakefile to make it easier to load the dumped structure into the database #1587 [Sam Stephenson]

* Added an EXPERIMENTAL gateway.cgi for getting high-speed performance through vanilla CGI using a long-running, DRb-backed server in the background (using script/listener and script/tracker) #1603 [Nicholas Seckar] * Added an EXPERIMENTAL gateway.cgi for getting high-speed performance through vanilla CGI using a long-running, DRb-backed server in the background (using script/listener and script/tracker) #1603 [Nicholas Seckar]


* Added migration generator: ./script/generate migration add_system_settings * Added migration generator: ./script/generate migration add_system_settings
Expand Down
136 changes: 78 additions & 58 deletions railties/fresh_rakefile
Expand Up @@ -30,8 +30,33 @@ def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
end.compact end.compact
end end


def with_database(environment = RAILS_ENV)
config = ActiveRecord::Base.configurations[environment]
case config['adapter']
when /mysql/
ActiveRecord::Base.establish_connection(config)
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
db = :mysql
when /postgresql/
ENV['PGHOST'] = config['host'].to_s if config['host']
ENV['PGPORT'] = config['port'].to_s if config['port']
ENV['PGPASSWORD'] = config['password'].to_s if config['password']
db = :postgresql
when /sqlite/
db = :sqlite
else
raise "Unknown database adapter '#{config['adapter']}'"
end
schema = "db/schema-#{db}.sql"
yield(config, schema)[db].call
end

task :test_environment do
ENV['RAILS_ENV'] = 'test'
end

desc 'Test recent changes.' desc 'Test recent changes.'
Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t| Rake::TestTask.new(:recent) do |t|
since = TEST_CHANGES_SINCE since = TEST_CHANGES_SINCE
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } + touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
recent_tests('app/models/*.rb', 'test/unit', since) + recent_tests('app/models/*.rb', 'test/unit', since) +
Expand All @@ -41,26 +66,26 @@ Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
t.verbose = true t.verbose = true
t.test_files = touched.uniq t.test_files = touched.uniq
end end
task :test_recent => [ :clone_structure_to_test ] task :test_recent => [:test_environment, :create_db]


desc "Run the unit tests in test/unit" desc "Run the unit tests in test/unit"
Rake::TestTask.new("test_units") { |t| Rake::TestTask.new(:test_units) { |t|
t.libs << "test" t.libs << "test"
t.pattern = 'test/unit/**/*_test.rb' t.pattern = 'test/unit/**/*_test.rb'
t.verbose = true t.verbose = true
} }
task :test_units => [ :clone_structure_to_test ] task :test_units => [:test_environment, :create_db]


desc "Run the functional tests in test/functional" desc "Run the functional tests in test/functional"
Rake::TestTask.new("test_functional") { |t| Rake::TestTask.new(:test_functional) { |t|
t.libs << "test" t.libs << "test"
t.pattern = 'test/functional/**/*_test.rb' t.pattern = 'test/functional/**/*_test.rb'
t.verbose = true t.verbose = true
} }
task :test_functional => [ :clone_structure_to_test ] task :test_functional => [:test_environment, :create_db]


desc "Generate documentation for the application" desc "Generate documentation for the application"
Rake::RDocTask.new("appdoc") { |rdoc| Rake::RDocTask.new(:appdoc) { |rdoc|
rdoc.rdoc_dir = 'doc/app' rdoc.rdoc_dir = 'doc/app'
rdoc.title = "Rails Application Documentation" rdoc.title = "Rails Application Documentation"
rdoc.options << '--line-numbers --inline-source' rdoc.options << '--line-numbers --inline-source'
Expand All @@ -69,7 +94,7 @@ Rake::RDocTask.new("appdoc") { |rdoc|
} }


desc "Generate documentation for the Rails framework" desc "Generate documentation for the Rails framework"
Rake::RDocTask.new("apidoc") { |rdoc| Rake::RDocTask.new(:apidoc) { |rdoc|
rdoc.rdoc_dir = 'doc/api' rdoc.rdoc_dir = 'doc/api'
rdoc.template = "#{ENV['template']}.rb" if ENV['template'] rdoc.template = "#{ENV['template']}.rb" if ENV['template']
rdoc.title = "Rails Framework Documentation" rdoc.title = "Rails Framework Documentation"
Expand Down Expand Up @@ -118,64 +143,59 @@ task :stats => [ :environment ] do
).to_s ).to_s
end end


desc "Recreate the test databases from the development structure" desc "Create the database for the current environment from the schema SQL"
task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do task :create_db => :destroy_db do
abcs = ActiveRecord::Base.configurations with_database do |config, schema|
case abcs["test"]["adapter"] {:mysql => lambda do
when "mysql" IO.read(schema).split("\n\n").each do |table|
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table|
ActiveRecord::Base.connection.execute(table) ActiveRecord::Base.connection.execute(table)
end end
when "postgresql" end,
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] :postgresql => lambda do
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] `createdb -T template0 -U "#{config['username']}" #{config['database']}`
`psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` `psql -U "#{config['username']}" -f #{schema} #{config['database']}`
when "sqlite", "sqlite3" end,
`#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
else :sqlite => lambda do
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'" `#{config['adapter']} #{config['dbfile']} < #{schema}`
end}
end end
end end


desc "Dump the database structure to a SQL file" desc "Destroy the database for the current environment"
task :db_structure_dump => :environment do task :destroy_db => :environment do
abcs = ActiveRecord::Base.configurations with_database do |config, schema|
case abcs[RAILS_ENV]["adapter"] {:mysql => lambda do
when "mysql" ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{config['database']}")
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) end,
File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
when "postgresql" :postgresql => lambda do
ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] `dropdb -U "#{config['username']}" #{config['database']}`
ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] end,
ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"]
`pg_dump -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{abcs[RAILS_ENV]["database"]}` :sqlite => lambda do
when "sqlite", "sqlite3" rm_f config['dbfile']
`#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql` end}
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
end end
end end


desc "Empty the test database" desc "Extract the current environment's database structure into schema SQL"
task :purge_test_database => :environment do task :extract_db_structure => :environment do
abcs = ActiveRecord::Base.configurations with_database do |config, schema|
case abcs["test"]["adapter"] {:mysql => lambda do
when "mysql" File.open(schema, 'w+') do |sql|
ActiveRecord::Base.establish_connection(:test) sql << ActiveRecord::Base.connection.structure_dump
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) end
when "postgresql" end,
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] :postgresql => lambda do
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] `pg_dump -U "#{config['username']}" -s -x -O -f #{schema} #{config['database']}`
`dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` end,
`createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
when "sqlite","sqlite3" :sqlite => lambda do
File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"]) `#{config['adapter']} #{config['dbfile']} .schema > #{schema}`
else end}
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
end end
end end


Expand Down

0 comments on commit 91afb92

Please sign in to comment.