lib/pg_search/tasks.rb in pg_search-0.3.2 vs lib/pg_search/tasks.rb in pg_search-0.3.3

- old
+ new

@@ -46,15 +46,23 @@ filename = "#{now.strftime('%Y%m%d%H%M%S')}_add_pg_search_dmetaphone_support_functions.rb" dmetaphone_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'dmetaphone.sql')).chomp uninstall_dmetaphone_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'uninstall_dmetaphone.sql')).chomp + unnest_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'unnest.sql')).chomp + uninstall_unnest_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'uninstall_unnest.sql')).chomp + File.open(Rails.root + 'db' + 'migrate' + filename, 'wb') do |migration_file| migration_file.puts <<-RUBY class AddPgSearchDmetaphoneSupportFunctions < ActiveRecord::Migration def self.up say_with_time("Adding support functions for pg_search :dmetaphone") do + if ActiveRecord::Base.connection.send(:postgresql_version) < 80400 + ActiveRecord::Base.connection.execute(<<-SQL) + #{unnest_sql} + SQL + end ActiveRecord::Base.connection.execute(<<-SQL) #{dmetaphone_sql} SQL end end @@ -62,9 +70,49 @@ def self.down say_with_time("Dropping support functions for pg_search :dmetaphone") do ActiveRecord::Base.connection.execute(<<-SQL) #{uninstall_dmetaphone_sql} SQL + if ActiveRecord::Base.connection.send(:postgresql_version) < 80400 + ActiveRecord::Base.connection.execute(<<-SQL) + #{uninstall_unnest_sql} + end + end + end + end +end + RUBY + end + end + + desc "Generate migration to add support functions for :associated_against in PostgreSQL 8.3 and earlier" + task :associated_against do + now = Time.now.utc + filename = "#{now.strftime('%Y%m%d%H%M%S')}_add_pg_search_associated_against_support_functions.rb" + + array_agg_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'array_agg.sql')).chomp + uninstall_array_agg_sql = File.read(File.join(File.dirname(__FILE__), '..', '..', 'sql', 'uninstall_array_agg.sql')).chomp + + File.open(Rails.root + 'db' + 'migrate' + filename, 'wb') do |migration_file| + migration_file.puts <<-RUBY +class AddPgSearchAssociatedAgainstSupportFunctions < ActiveRecord::Migration + def self.up + say_with_time("Adding support functions for pg_search :associated_against") do + if ActiveRecord::Base.connection.send(:postgresql_version) < 80400 + ActiveRecord::Base.connection.execute(<<-SQL) + #{array_agg_sql} + SQL + end + end + end + + def self.down + say_with_time("Dropping support functions for pg_search :associated_against") do + if ActiveRecord::Base.connection.send(:postgresql_version) < 80400 + ActiveRecord::Base.connection.execute(<<-SQL) + #{uninstall_array_agg_sql} + SQL + end end end end RUBY end