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:
parent
6ce4b59509
commit
a32753a04a
|
@ -24,6 +24,8 @@
|
||||||
DataSync for CORE POS
|
DataSync for CORE POS
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
from corepos.db.office_op import Session as CoreSession, model as corepos
|
from corepos.db.office_op import Session as CoreSession, model as corepos
|
||||||
|
|
||||||
from rattail.db import model
|
from rattail.db import model
|
||||||
|
@ -36,10 +38,24 @@ class CoreOfficeOpWatcher(DataSyncWatcher):
|
||||||
"""
|
"""
|
||||||
prunes_changes = True
|
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):
|
def get_changes(self, lastrun):
|
||||||
session = CoreSession()
|
session = CoreSession()
|
||||||
changes = session.query(corepos.Change).all()
|
result = session.execute(sa.sql.select([self.corepos_changes]))
|
||||||
session.expunge_all()
|
changes = result.fetchall()
|
||||||
session.close()
|
session.close()
|
||||||
if changes:
|
if changes:
|
||||||
return [
|
return [
|
||||||
|
@ -54,10 +70,11 @@ class CoreOfficeOpWatcher(DataSyncWatcher):
|
||||||
deleted = 0
|
deleted = 0
|
||||||
session = CoreSession()
|
session = CoreSession()
|
||||||
for key in keys:
|
for key in keys:
|
||||||
change = session.query(corepos.Change).get(key)
|
result = session.execute(self.corepos_changes.select()\
|
||||||
if change:
|
.where(self.corepos_changes.c.id == key))
|
||||||
session.delete(change)
|
if result.fetchall():
|
||||||
session.flush()
|
session.execute(self.corepos_changes.delete()\
|
||||||
|
.where(self.corepos_changes.c.id == key))
|
||||||
deleted += 1
|
deleted += 1
|
||||||
session.commit()
|
session.commit()
|
||||||
session.close()
|
session.close()
|
||||||
|
|
Loading…
Reference in a new issue