Module: ActiveRecord::ConnectionAdapters::SchemaStatements

Defined in:
lib/pg-xml/activerecord.rb

Instance Method Summary collapse

Instance Method Details

#add_xpath_index(table_name, column_name, path, index_name = nil) ⇒ Object

enable adding an xpath index



48
49
50
51
52
53
54
55
# File 'lib/pg-xml/activerecord.rb', line 48

def add_xpath_index table_name, column_name, path, index_name=nil
  # generate an index name
  index_name ||= "index_#{table_name}_on_#{column_name}__#{path}"
  index_name = index_name.downcase.gsub(/[^a-z0-9]/, '_')

  # create the index
  execute "CREATE INDEX #{index_name} ON #{table_name}(xpath('#{path}',#{column_name}))"
end

#invert_add_xpath_index(args) ⇒ Object

TODO this doesn’t get called – why? adding the xpath index should be reversible



76
77
78
79
80
# File 'lib/pg-xml/activerecord.rb', line 76

def invert_add_xpath_index(args)
  # table_name, column_name, path, index_name = *args
  # [:remove_index, [table_name, column_name, path, index_name]]
  [:remove_xpath_index, args]
end

#remove_xpath_index(index_name_or_table_name, column_name = nil, path = nil, index_name = nil) ⇒ Object

can call in any of the following ways remove_xpath_index :users, :properties, ‘/xml/name’ remove_xpath_index :index_users_on_properties__xml_name remove_xpath_index :users, :properties, ‘/xml/name’, :index_users_on_properties__xml_name



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/pg-xml/activerecord.rb', line 62

def remove_xpath_index index_name_or_table_name, column_name=nil, path=nil, index_name=nil
  # figure out which version was called
  index_name = index_name_or_table_name if !column_name

  # transform index name
  index_name ||= "index_#{table_name}_on_#{column_name}__#{path}"
  index_name = index_name.downcase.gsub(/[^a-z0-9]/, '_')

  # drop the index
  execute "DROP INDEX #{index_name}"
end