Skip to content

Commit

Permalink
Backed out of create/destroy_db -- didnt yet work right in mysql and …
Browse files Browse the repository at this point in the history
…breaks AWD

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1702 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
dhh committed Jul 5, 2005
1 parent 91afb92 commit 6eee49a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 80 deletions.
2 changes: 0 additions & 2 deletions railties/CHANGELOG
@@ -1,7 +1,5 @@
*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 migration generator: ./script/generate migration add_system_settings
Expand Down
136 changes: 58 additions & 78 deletions railties/fresh_rakefile
Expand Up @@ -30,33 +30,8 @@ def recent_tests(source_pattern, test_path, touched_since = 10.minutes.ago)
end.compact
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.'
Rake::TestTask.new(:recent) do |t|
Rake::TestTask.new(:recent => [ :clone_structure_to_test ]) do |t|
since = TEST_CHANGES_SINCE
touched = FileList['test/**/*_test.rb'].select { |path| File.mtime(path) > since } +
recent_tests('app/models/*.rb', 'test/unit', since) +
Expand All @@ -66,26 +41,26 @@ Rake::TestTask.new(:recent) do |t|
t.verbose = true
t.test_files = touched.uniq
end
task :test_recent => [:test_environment, :create_db]
task :test_recent => [ :clone_structure_to_test ]

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

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

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

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

desc "Create the database for the current environment from the schema SQL"
task :create_db => :destroy_db do
with_database do |config, schema|
{:mysql => lambda do
IO.read(schema).split("\n\n").each do |table|
desc "Recreate the test databases from the development structure"
task :clone_structure_to_test => [ :db_structure_dump, :purge_test_database ] do
abcs = ActiveRecord::Base.configurations
case abcs["test"]["adapter"]
when "mysql"
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)
end
end,

:postgresql => lambda do
`createdb -T template0 -U "#{config['username']}" #{config['database']}`
`psql -U "#{config['username']}" -f #{schema} #{config['database']}`
end,

:sqlite => lambda do
`#{config['adapter']} #{config['dbfile']} < #{schema}`
end}
when "postgresql"
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
`psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}`
when "sqlite", "sqlite3"
`#{abcs[RAILS_ENV]["adapter"]} #{abcs["test"]["dbfile"]} < db/#{RAILS_ENV}_structure.sql`
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
end
end

desc "Destroy the database for the current environment"
task :destroy_db => :environment do
with_database do |config, schema|
{:mysql => lambda do
ActiveRecord::Base.connection.execute("DROP DATABASE IF EXISTS #{config['database']}")
end,

:postgresql => lambda do
`dropdb -U "#{config['username']}" #{config['database']}`
end,

:sqlite => lambda do
rm_f config['dbfile']
end}
desc "Dump the database structure to a SQL file"
task :db_structure_dump => :environment do
abcs = ActiveRecord::Base.configurations
case abcs[RAILS_ENV]["adapter"]
when "mysql"
ActiveRecord::Base.establish_connection(abcs[RAILS_ENV])
File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump }
when "postgresql"
ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"]
ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"]
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"]}`
when "sqlite", "sqlite3"
`#{abcs[RAILS_ENV]["adapter"]} #{abcs[RAILS_ENV]["dbfile"]} .schema > db/#{RAILS_ENV}_structure.sql`
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
end
end

desc "Extract the current environment's database structure into schema SQL"
task :extract_db_structure => :environment do
with_database do |config, schema|
{:mysql => lambda do
File.open(schema, 'w+') do |sql|
sql << ActiveRecord::Base.connection.structure_dump
end
end,

:postgresql => lambda do
`pg_dump -U "#{config['username']}" -s -x -O -f #{schema} #{config['database']}`
end,

:sqlite => lambda do
`#{config['adapter']} #{config['dbfile']} .schema > #{schema}`
end}
desc "Empty the test database"
task :purge_test_database => :environment do
abcs = ActiveRecord::Base.configurations
case abcs["test"]["adapter"]
when "mysql"
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"])
when "postgresql"
ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"]
ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"]
ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"]
`dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
`createdb -T template0 -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}`
when "sqlite","sqlite3"
File.delete(abcs["test"]["dbfile"]) if File.exist?(abcs["test"]["dbfile"])
else
raise "Unknown database adapter '#{abcs["test"]["adapter"]}'"
end
end

Expand Down

0 comments on commit 6eee49a

Please sign in to comment.