Created
August 19, 2011 19:18
-
-
Save mattscilipoti/1157737 to your computer and use it in GitHub Desktop.
rake files for postgres support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# This patch makes structure dump a first class citizen | |
# | |
puts "WARN: We are patching rake tasks to support postgres. You have upgraded Rails and may not need this patch any more (lib/tasks/postgres.rake)." if Rails.version > "3.0.9" | |
Rake.application.remove_task('db:schema:dump') | |
Rake.application.remove_task('db:schema:load') | |
namespace :db do | |
namespace :schema do | |
desc "Create a db/schema.rb file that can be portably used against any DB supported by AR" | |
task :dump => :environment do | |
case ActiveRecord::Base.schema_format | |
when :ruby | |
require 'active_record/schema_dumper' | |
File.open(ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb", "w") do |file| | |
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file) | |
end | |
Rake::Task["db:schema:dump"].reenable | |
when :sql | |
Rake::Task["db:structure:dump"].invoke | |
else | |
raise "That schema_format (#{ActiveRecord::Base.schema_format}) is not currently supported." | |
end | |
end | |
desc "Load a schema.rb file into the database" | |
task :load => :environment do | |
case ActiveRecord::Base.schema_format | |
when :ruby | |
file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb" | |
if File.exists?(file) | |
load(file) | |
else | |
abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded} | |
end | |
when :sql | |
Rake::Task["db:structure:load"].invoke | |
else | |
raise "That schema_format (#{ActiveRecord::Base.schema_format}) is not currently supported." | |
end | |
end | |
end | |
namespace :structure do | |
desc "Load the database structure from an SQL file" | |
task :load => :environment do | |
file = File.join(Rails.root, 'db', 'development_structure.sql') | |
if File.exists?(file) | |
IO.readlines(file).join.split("\n\n").each do |sql_statement| | |
ActiveRecord::Base.connection.execute(sql_statement) | |
end | |
else | |
abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/boot.rb to limit the frameworks that will be loaded} | |
end | |
end | |
end | |
# | |
# This patch ensures db:drop can work by killing the postgres connections | |
# see: http://stackoverflow.com/questions/2369744/rails-postgres-drop-error-database-is-being-accessed-by-other-users | |
# | |
desc 'kills connections to postgres db' | |
task :kill_postgres_connections => :environment do | |
db_name = "#{File.basename(Rails.root)}_#{Rails.env}" | |
puts "WARN: killing connections to #{db_name}." | |
sh = <<EOF | |
ps xa \ | |
| grep postgres: \ | |
| grep #{db_name} \ | |
| grep -v grep \ | |
| awk '{print $1}' \ | |
| xargs kill | |
EOF | |
puts `#{sh}` | |
end | |
task "db:drop" => :kill_postgres_connections | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment