Class: Sunspot::Installer::SchemaBuilder

Inherits:
Object
  • Object
show all
Includes:
TaskHelper
Defined in:
lib/sunspot/installer/schema_builder.rb

Overview

This class modifies an existing Solr schema.xml file to work with Sunspot. It makes the minimum necessary changes to the schema, adding fields and types only when they don’t already exist. It also comments all fields and types that Sunspot needs, whether or not they were preexisting, so that users can modify the resulting schema without unwittingly breaking it.

Constant Summary collapse

CONFIG_PATH =
File.join(
  File.dirname(__FILE__), '..', '..', '..', 'installer', 'config', 'schema.yml'
)

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TaskHelper

#add_element

Constructor Details

#initialize(schema_path, options = {}) ⇒ SchemaBuilder

Returns a new instance of SchemaBuilder.



30
31
32
33
34
35
# File 'lib/sunspot/installer/schema_builder.rb', line 30

def initialize(schema_path, options = {})
  @schema_path = schema_path
  @config = File.open(CONFIG_PATH) { |f| YAML.load(f) }
  @verbose = !!options[:verbose]
  @force = !!options[:force]
end

Class Method Details

.execute(schema_path, options = {}) ⇒ Object



23
24
25
# File 'lib/sunspot/installer/schema_builder.rb', line 23

def execute(schema_path, options = {})
  new(schema_path, options).execute
end

Instance Method Details

#executeObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sunspot/installer/schema_builder.rb', line 37

def execute
  if @force
    FileUtils.mkdir_p(File.dirname(@schema_path))
    source_path = File.join(File.dirname(__FILE__), '..', '..', '..', 'solr', 'solr', 'conf', 'schema.xml')
    FileUtils.cp(source_path, @schema_path)
    say("Copied default schema.xml to #{@schema_path}")
  else
    @document = File.open(@schema_path) do |f|
      Nokogiri::XML(
        f, nil, nil,
        Nokogiri::XML::ParseOptions::DEFAULT_XML |
        Nokogiri::XML::ParseOptions::NOBLANKS
      )
    end
    @root = @document.root
    @added_types = Set[]
    add_fixed_fields
    add_dynamic_fields
    set_misc_settings
    original_path = "#{@schema_path}.orig"
    FileUtils.cp(@schema_path, original_path)
    say("Saved backup of original to #{original_path}")
    File.open(@schema_path, 'w') do |file|
      @document.write_xml_to(file, :indent => 2)
    end
    say("Wrote schema to #{@schema_path}")
  end
end