diff options
Diffstat (limited to 'vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb')
| -rw-r--r-- | vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb | 114 |
1 files changed, 0 insertions, 114 deletions
diff --git a/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb b/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb deleted file mode 100644 index 4e67523..0000000 --- a/vendor/plugins/thinking-sphinx/spec/sphinx_helper.rb +++ /dev/null | |||
| @@ -1,114 +0,0 @@ | |||
| 1 | require 'active_record' | ||
| 2 | prefix = defined?(JRUBY_VERSION) ? "jdbc" : "" | ||
| 3 | require "active_record/connection_adapters/#{prefix}mysql_adapter" | ||
| 4 | begin | ||
| 5 | require "active_record/connection_adapters/#{prefix}postgresql_adapter" | ||
| 6 | rescue LoadError | ||
| 7 | # No postgres? no prob... | ||
| 8 | end | ||
| 9 | require 'yaml' | ||
| 10 | |||
| 11 | class SphinxHelper | ||
| 12 | attr_accessor :host, :username, :password | ||
| 13 | attr_reader :path | ||
| 14 | |||
| 15 | def initialize | ||
| 16 | @host = "localhost" | ||
| 17 | @username = "thinking_sphinx" | ||
| 18 | @password = "" | ||
| 19 | |||
| 20 | if File.exist?("spec/fixtures/database.yml") | ||
| 21 | config = YAML.load(File.open("spec/fixtures/database.yml")) | ||
| 22 | @host = config["host"] | ||
| 23 | @username = config["username"] | ||
| 24 | @password = config["password"] | ||
| 25 | end | ||
| 26 | |||
| 27 | @path = File.expand_path(File.dirname(__FILE__)) | ||
| 28 | end | ||
| 29 | |||
| 30 | def mysql_adapter | ||
| 31 | defined?(JRUBY_VERSION) ? 'jdbcmysql' : 'mysql' | ||
| 32 | end | ||
| 33 | |||
| 34 | def setup_mysql | ||
| 35 | ActiveRecord::Base.establish_connection( | ||
| 36 | :adapter => mysql_adapter, | ||
| 37 | :database => 'thinking_sphinx', | ||
| 38 | :username => @username, | ||
| 39 | :password => @password, | ||
| 40 | :host => @host | ||
| 41 | ) | ||
| 42 | ActiveRecord::Base.logger = Logger.new(File.open("tmp/activerecord.log", "a")) | ||
| 43 | |||
| 44 | structure = File.open("spec/fixtures/structure.sql") { |f| f.read.chomp } | ||
| 45 | structure.split(';').each { |table| | ||
| 46 | ActiveRecord::Base.connection.execute table | ||
| 47 | } | ||
| 48 | |||
| 49 | File.open("spec/fixtures/data.sql") { |f| | ||
| 50 | while line = f.gets | ||
| 51 | ActiveRecord::Base.connection.execute line unless line.blank? | ||
| 52 | end | ||
| 53 | } | ||
| 54 | end | ||
| 55 | |||
| 56 | def setup_sphinx | ||
| 57 | @configuration = ThinkingSphinx::Configuration.instance.reset | ||
| 58 | File.open("spec/fixtures/sphinx/database.yml", "w") do |file| | ||
| 59 | YAML.dump({@configuration.environment => { | ||
| 60 | :adapter => mysql_adapter, | ||
| 61 | :host => @host, | ||
| 62 | :database => "thinking_sphinx", | ||
| 63 | :username => @username, | ||
| 64 | :password => @password | ||
| 65 | }}, file) | ||
| 66 | end | ||
| 67 | FileUtils.mkdir_p(@configuration.searchd_file_path) | ||
| 68 | |||
| 69 | @configuration.database_yml_file = "spec/fixtures/sphinx/database.yml" | ||
| 70 | @configuration.build | ||
| 71 | |||
| 72 | index | ||
| 73 | end | ||
| 74 | |||
| 75 | def reset | ||
| 76 | setup_mysql | ||
| 77 | end | ||
| 78 | |||
| 79 | def index | ||
| 80 | cmd = "indexer --config #{@configuration.config_file} --all" | ||
| 81 | cmd << " --rotate" if running? | ||
| 82 | `#{cmd}` | ||
| 83 | end | ||
| 84 | |||
| 85 | def start | ||
| 86 | return if running? | ||
| 87 | |||
| 88 | cmd = "searchd --config #{@configuration.config_file}" | ||
| 89 | `#{cmd}` | ||
| 90 | |||
| 91 | sleep(1) | ||
| 92 | |||
| 93 | unless running? | ||
| 94 | puts "Failed to start searchd daemon. Check #{@configuration.searchd_log_file}." | ||
| 95 | end | ||
| 96 | end | ||
| 97 | |||
| 98 | def stop | ||
| 99 | return unless running? | ||
| 100 | `kill #{pid}` | ||
| 101 | end | ||
| 102 | |||
| 103 | def pid | ||
| 104 | if File.exists?("#{@configuration.pid_file}") | ||
| 105 | `cat #{@configuration.pid_file}`[/\d+/] | ||
| 106 | else | ||
| 107 | nil | ||
| 108 | end | ||
| 109 | end | ||
| 110 | |||
| 111 | def running? | ||
| 112 | pid && `ps #{pid} | wc -l`.to_i > 1 | ||
| 113 | end | ||
| 114 | end | ||
