Skip to content

Instantly share code, notes, and snippets.

@stestaub
Created July 31, 2013 01:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stestaub/6118568 to your computer and use it in GitHub Desktop.
Save stestaub/6118568 to your computer and use it in GitHub Desktop.
Add this to lib/tasks to all rake tasks working again with schema_format :sql and postgis database adapter
#some hacks to get postgis adapter to work with schema_type sql
require 'rgeo/active_record/task_hacker'
::RGeo::ActiveRecord::TaskHacker.modify('db:structure:dump', nil, 'postgis') do |config_|
set_psql_env(config_)
filename_ = ::File.join(::Rails.root, "db/#{::Rails.env}_structure.sql")
search_path_ = config_["schema_search_path"].to_s.strip
search_path_ = search_path_.split(",").map{ |sp_| sp_.strip }
search_path_.delete('postgis')
search_path_ = ['public'] if search_path_.length == 0
search_path_ = search_path_.map{ |sp_| "--schema=#{sp_}" }.join(" ")
`pg_dump -i -U "#{config_["username"]}" -s -x -O -f #{filename_} #{search_path_} #{config_["database"]}`
raise "Error dumping database" if $?.exitstatus == 1
# This was missing
if ActiveRecord::Base.connection.supports_migrations?
File.open(filename_, "a") { |f| f << ActiveRecord::Base.connection.dump_schema_information }
end
end
# the AR rake task load_structure does call db:stucture:load with config set to test. The problem
# Is that this is overwritten by rgeo and it does not take the environment variable in AR.config into account
# So the load_structure is overwritten to directly load the structure without calling the db:structure:load
::RGeo::ActiveRecord::TaskHacker.modify('db:test:load_structure', 'test', 'postgis') do |config_|
set_psql_env(config_)
filename_ = ::File.join(::Rails.root, "db/#{::Rails.env}_structure.sql")
`psql -f #{filename_} #{config_["database"]}`
end
# db:clone_structure was overwritten and loaded the schema again. This overrides it again with the standard behaviour
Rake::Task['db:test:clone_structure'].clear.enhance(["db:structure:dump", "db:test:load_structure"]) {}
# The db:setup task tries to execute schema:load instead of structure:load so we override it
Rake::Task['db:setup'].clear.enhance() do # see the .clear method invoked?
Rake::Task['db:create'].invoke
# This is not working, it thinks it has schema ruby
#Rake::Task['db:schema:load'].invoke if ActiveRecord::Base.schema_format == :ruby
#Rake::Task['db:structure:load'].invoke if ActiveRecord::Base.schema_format == :sql
Rake::Task['db:structure:load'].invoke
Rake::Task['db:seed'].invoke
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment