Let CORE-POS datasync watcher use alternate "changes" table name

i needed this for sake of online demo, so that 2 separate rattail apps could
both watch the same CORE DB simultaneously
This commit is contained in:
Lance Edgar 2020-07-09 17:01:23 -05:00
parent 6ce4b59509
commit a32753a04a

View file

@ -24,6 +24,8 @@
DataSync for CORE POS
"""
import sqlalchemy as sa
from corepos.db.office_op import Session as CoreSession, model as corepos
from rattail.db import model
@ -36,10 +38,24 @@ class CoreOfficeOpWatcher(DataSyncWatcher):
"""
prunes_changes = True
def __init__(self, *args, **kwargs):
super(CoreOfficeOpWatcher, self).__init__(*args, **kwargs)
self.changes_table_name = kwargs.get('changes_table_name',
'datasync_changes')
self.corepos_metadata = sa.MetaData()
self.corepos_changes = sa.Table(
self.changes_table_name, self.corepos_metadata,
sa.Column('id', sa.Integer(), nullable=False, primary_key=True),
sa.Column('object_type', sa.String(length=255), nullable=False),
sa.Column('object_key', sa.String(length=255), nullable=False),
sa.Column('deleted', sa.Boolean(), nullable=False, default=False))
def get_changes(self, lastrun):
session = CoreSession()
changes = session.query(corepos.Change).all()
session.expunge_all()
result = session.execute(sa.sql.select([self.corepos_changes]))
changes = result.fetchall()
session.close()
if changes:
return [
@ -54,10 +70,11 @@ class CoreOfficeOpWatcher(DataSyncWatcher):
deleted = 0
session = CoreSession()
for key in keys:
change = session.query(corepos.Change).get(key)
if change:
session.delete(change)
session.flush()
result = session.execute(self.corepos_changes.select()\
.where(self.corepos_changes.c.id == key))
if result.fetchall():
session.execute(self.corepos_changes.delete()\
.where(self.corepos_changes.c.id == key))
deleted += 1
session.commit()
session.close()