
from ggrc import db
from . import Indexer

class SqlIndexer(Indexer):
  def create_record(self, record, commit=True):
    for k,v in record.properties.items():
      db.session.add(self.record_type(
        key=record.key,
        type=record.type,
        context_id=record.context_id,
        tags=record.tags,
        property=k,
        content=v,
        ))
    if commit:
      db.session.commit()

  def update_record(self, record, commit=True):
    self.delete_record(record.key, commit=False)
    self.create_record(record, commit=commit)

  def delete_record(self, key, type, commit=True):
    db.session.query(self.record_type).filter(\
        self.record_type.key == key,
        self.record_type.type == type).delete()
    if commit:
      db.session.commit()

  def delete_all_records(self, commit=True):
    db.session.query(self.record_type).delete()
    if commit:
      db.session.commit()


from ggrc import db
from . import Indexer

class SqlIndexer(Indexer):
  def create_record(self, record, commit=True):
    for k,v in record.properties.items():
      db.session.add(self.record_type(
        key=record.key,
        type=record.type,
        context_id=record.context_id,
        tags=record.tags,
        property=k,
        content=v,
        ))
    if commit:
      db.session.commit()

  def update_record(self, record, commit=True):
    self.delete_record(record.key, record.type, commit=False)
    self.create_record(record, commit=commit)

  def delete_record(self, key, type, commit=True):
    db.session.query(self.record_type).filter(\
        self.record_type.key == key,
        self.record_type.type == type).delete()
    if commit:
      db.session.commit()

  def delete_all_records(self, commit=True):
    db.session.query(self.record_type).delete()
    if commit:
      db.session.commit()

Fix test broken due to delete_record change
Fix test broken due to delete_record change

  
  from ggrc import db
  from . import Indexer
  
  class SqlIndexer(Indexer):
    def create_record(self, record, commit=True):
      for k,v in record.properties.items():
        db.session.add(self.record_type(
          key=record.key,
          type=record.type,
          context_id=record.context_id,
          tags=record.tags,
          property=k,
          content=v,
          ))
      if commit:
        db.session.commit()
  
    def update_record(self, record, commit=True):
-     self.delete_record(record.key, commit=False)
+     self.delete_record(record.key, record.type, commit=False)
      self.create_record(record, commit=commit)
  
    def delete_record(self, key, type, commit=True):
      db.session.query(self.record_type).filter(\
          self.record_type.key == key,
          self.record_type.type == type).delete()
      if commit:
        db.session.commit()
  
    def delete_all_records(self, commit=True):
      db.session.query(self.record_type).delete()
      if commit:
        db.session.commit()
  
Fix test broken due to delete_record change
## Code Before:

from ggrc import db
from . import Indexer

class SqlIndexer(Indexer):
  def create_record(self, record, commit=True):
    for k,v in record.properties.items():
      db.session.add(self.record_type(
        key=record.key,
        type=record.type,
        context_id=record.context_id,
        tags=record.tags,
        property=k,
        content=v,
        ))
    if commit:
      db.session.commit()

  def update_record(self, record, commit=True):
    self.delete_record(record.key, commit=False)
    self.create_record(record, commit=commit)

  def delete_record(self, key, type, commit=True):
    db.session.query(self.record_type).filter(\
        self.record_type.key == key,
        self.record_type.type == type).delete()
    if commit:
      db.session.commit()

  def delete_all_records(self, commit=True):
    db.session.query(self.record_type).delete()
    if commit:
      db.session.commit()

## Instruction:
Fix test broken due to delete_record change
## Code After:

from ggrc import db
from . import Indexer

class SqlIndexer(Indexer):
  def create_record(self, record, commit=True):
    for k,v in record.properties.items():
      db.session.add(self.record_type(
        key=record.key,
        type=record.type,
        context_id=record.context_id,
        tags=record.tags,
        property=k,
        content=v,
        ))
    if commit:
      db.session.commit()

  def update_record(self, record, commit=True):
    self.delete_record(record.key, record.type, commit=False)
    self.create_record(record, commit=commit)

  def delete_record(self, key, type, commit=True):
    db.session.query(self.record_type).filter(\
        self.record_type.key == key,
        self.record_type.type == type).delete()
    if commit:
      db.session.commit()

  def delete_all_records(self, commit=True):
    db.session.query(self.record_type).delete()
    if commit:
      db.session.commit()
