From 4a409bb80ab5827eb3ab59a6775890654648578c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Mon, 16 Mar 2020 16:55:48 -0500 Subject: [PATCH] Add new `CorePerson` data model, for use with customer sync --- .../c61f78243ff3_add_person_extension.py | 51 +++++++++++++++++++ rattail_corepos/db/model.py | 37 +++++++++++++- 2 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 rattail_corepos/db/alembic/versions/c61f78243ff3_add_person_extension.py diff --git a/rattail_corepos/db/alembic/versions/c61f78243ff3_add_person_extension.py b/rattail_corepos/db/alembic/versions/c61f78243ff3_add_person_extension.py new file mode 100644 index 0000000..6c428d9 --- /dev/null +++ b/rattail_corepos/db/alembic/versions/c61f78243ff3_add_person_extension.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +"""add Person extension + +Revision ID: c61f78243ff3 +Revises: b43e93d32275 +Create Date: 2020-03-16 16:50:55.266325 + +""" + +# revision identifiers, used by Alembic. +revision = 'c61f78243ff3' +down_revision = 'b43e93d32275' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa +import rattail.db.types + + + +def upgrade(): + + # corepos_person + op.create_table('corepos_person', + sa.Column('uuid', sa.String(length=32), nullable=False), + sa.Column('corepos_customer_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['uuid'], ['person.uuid'], name='corepos_person_fk_person'), + sa.PrimaryKeyConstraint('uuid') + ) + op.create_table('corepos_person_version', + sa.Column('uuid', sa.String(length=32), autoincrement=False, nullable=False), + sa.Column('corepos_customer_id', sa.Integer(), autoincrement=False, nullable=True), + sa.Column('transaction_id', sa.BigInteger(), autoincrement=False, nullable=False), + sa.Column('end_transaction_id', sa.BigInteger(), nullable=True), + sa.Column('operation_type', sa.SmallInteger(), nullable=False), + sa.PrimaryKeyConstraint('uuid', 'transaction_id') + ) + op.create_index(op.f('ix_corepos_person_version_end_transaction_id'), 'corepos_person_version', ['end_transaction_id'], unique=False) + op.create_index(op.f('ix_corepos_person_version_operation_type'), 'corepos_person_version', ['operation_type'], unique=False) + op.create_index(op.f('ix_corepos_person_version_transaction_id'), 'corepos_person_version', ['transaction_id'], unique=False) + + +def downgrade(): + + # corepos_person + op.drop_index(op.f('ix_corepos_person_version_transaction_id'), table_name='corepos_person_version') + op.drop_index(op.f('ix_corepos_person_version_operation_type'), table_name='corepos_person_version') + op.drop_index(op.f('ix_corepos_person_version_end_transaction_id'), table_name='corepos_person_version') + op.drop_table('corepos_person_version') + op.drop_table('corepos_person') diff --git a/rattail_corepos/db/model.py b/rattail_corepos/db/model.py index 6f4f87e..9bf3dbf 100644 --- a/rattail_corepos/db/model.py +++ b/rattail_corepos/db/model.py @@ -30,7 +30,42 @@ from sqlalchemy import orm from rattail.db import model -__all__ = ['CoreVendor'] +__all__ = ['CorePerson', 'CoreVendor'] + + +class CorePerson(model.Base): + """ + CORE-specific extensions to :class:`rattail:rattail.db.model.Person`. + """ + __tablename__ = 'corepos_person' + __table_args__ = ( + sa.ForeignKeyConstraint(['uuid'], ['person.uuid'], + name='corepos_person_fk_person'), + ) + __versioned__ = {} + + uuid = model.uuid_column(default=None) + person = orm.relationship( + model.Person, + doc=""" + Reference to the actual person record, which this one extends. + """, + backref=orm.backref( + '_corepos', + uselist=False, + cascade='all, delete-orphan', + doc=""" + Reference to the CORE-POS extension record for this person. + """)) + + corepos_customer_id = sa.Column(sa.Integer(), nullable=False, doc=""" + ``Customers.customerID`` value for this person, within CORE-POS. + """) + + def __str__(self): + return str(self.person) + +CorePerson.make_proxy(model.Person, '_corepos', 'corepos_customer_id') class CoreVendor(model.Base):