lib/neo4j/active_node/labels.rb in neo4j-5.0.15 vs lib/neo4j/active_node/labels.rb in neo4j-5.1.0.rc.1

- old
+ new

@@ -101,11 +101,11 @@ end # Finds the first record matching the specified conditions. There is no implied ordering so if order matters, you should specify it yourself. # @param Hash args of arguments to find def find_by(values) - all.query_as(:n).where(n: values).limit(1).pluck(:n).first + all.where(values).limit(1).query_as(:n).pluck(:n).first end # Like find_by, except that if no record is found, raises a RecordNotFound error. def find_by!(values) find_by(values) || fail(RecordNotFound, "#{self.query_as(:n).where(n: values).limit(1).to_cypher} returned no results") @@ -145,10 +145,11 @@ # # below is same as: index :name, index: :exact, constraint: {type: :unique} # index :name, constraint: {type: :unique} # end def index(property, conf = {}) Neo4j::Session.on_session_available do |_| + drop_constraint(property, type: :unique) if Neo4j::Label.constraint?(mapped_label_name, property) _index(property, conf) end indexed_properties.push property unless indexed_properties.include? property end @@ -159,15 +160,25 @@ # def constraint(property, constraints) Neo4j::Session.on_session_available do |session| unless Neo4j::Label.constraint?(mapped_label_name, property) label = Neo4j::Label.create(mapped_label_name) + drop_index(property, label) if index?(property) label.create_constraint(property, constraints, session) end end end - def drop_constraint(property, constraint) + # @param [Symbol] property The name of the property index to be dropped + # @param [Neo4j::Label] label An instance of label from Neo4j::Core + def drop_index(property, label = nil) + label_obj = label || Neo4j::Label.create(mapped_label_name) + label_obj.drop_index(property) + end + + # @param [Symbol] property The name of the property constraint to be dropped + # @param [Hash] constraint The constraint type to be dropped. + def drop_constraint(property, constraint = {type: :unique}) Neo4j::Session.on_session_available do |session| label = Neo4j::Label.create(mapped_label_name) label.drop_constraint(property, constraint, session) end end