Skip to content

Commit

Permalink
Update sql_versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
leodube-aot committed Feb 10, 2025
1 parent 9a607f7 commit 9265ca7
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 5 deletions.
5 changes: 5 additions & 0 deletions legal-api/src/legal_api/models/office.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class Office(db.Model, Versioned): # pylint: disable=too-few-public-methods
# relationships
business_id = db.Column('business_id', db.Integer, db.ForeignKey('businesses.id'), index=True)

@classmethod
def get_all_by_business_id(cls, business_id: int):
"""Get all offices for a business."""
return cls.query.filter_by(business_id=business_id).all()


class OfficeType(db.Model): # pylint: disable=too-few-public-methods
"""Define the Office Types available for Legal Entities."""
Expand Down
2 changes: 1 addition & 1 deletion legal-api/src/legal_api/models/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ def find_by_type(cls, business_id: int, resolution_type: str):
return resolutions

@classmethod
def get_resolution_by_business_id(cls, business_id: int):
def get_all_by_business_id(cls, business_id: int):
"""Get all resolutions for a business."""
return cls.query.filter_by(business_id=business_id).all()
5 changes: 5 additions & 0 deletions legal-api/src/legal_api/models/share_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ def find_by_share_class_id(cls, share_class_id: int) -> ShareClass:
share_class = cls.query.filter_by(id=share_class_id).one_or_none()
return share_class

@classmethod
def get_all_by_business_id(cls, business_id: int):
"""Get all share classes for a business."""
return cls.query.filter_by(business_id=business_id).all()


@event.listens_for(ShareClass, 'before_insert')
@event.listens_for(ShareClass, 'before_update')
Expand Down
11 changes: 8 additions & 3 deletions legal-api/src/legal_api/reports/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
CorpType,
Document,
Filing,
Office,
OfficeType,
PartyRole,
Resolution,
ShareClass
)
from legal_api.models.business import ASSOCIATION_TYPE_DESC
from legal_api.reports.registrar_meta import RegistrarInfo
Expand Down Expand Up @@ -836,8 +839,8 @@ def _set_from_primary_or_holding_business_data(self, filing): # pylint: disable
self._filing.transaction_id,
primary_or_holding_business.id)
else:
officelist = primary_or_holding_business.offices.all()
for i in officelist:
offices = Office.get_all_by_business_id(primary_or_holding_business.id)
for i in offices.all():
if i.office_type in [OfficeType.REGISTERED, OfficeType.RECORDS]:
offices[i.office_type] = {}
for address in i.addresses:
Expand All @@ -851,6 +854,7 @@ def _set_from_primary_or_holding_business_data(self, filing): # pylint: disable
self._filing.transaction_id,
primary_or_holding_business.id)
else:
share_classes = ShareClass.get_all_by_business_id(primary_or_holding_business.id)
for share_class in primary_or_holding_business.share_classes.all():
share_classes.append(share_class.json)
filing['shareClasses'] = share_classes
Expand All @@ -862,7 +866,8 @@ def _set_from_primary_or_holding_business_data(self, filing): # pylint: disable
self._filing.transaction_id,
primary_or_holding_business.id)
else:
for resolution in primary_or_holding_business.resolutions.all():
resolutions = Resolution.get_all_by_business_id(primary_or_holding_business.id)
for resolution in resolutions.all():
resolutions.append({'date': resolution.resolution_date.strftime(OUTPUT_DATE_FORMAT)})
filing['resolutions'] = resolutions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ def _set_shares(primary_or_holding_business, amalgamation_filing, transaction_id
share_classes = VersionedBusinessDetailsService.get_share_class_revision(transaction_id,
primary_or_holding_business.id)
amalgamation_filing['shareStructure'] = {'shareClasses': share_classes}
resolutions = Resolution.get_resolution_by_business_id(primary_or_holding_business.id)
resolutions = Resolution.get_all_by_business_id(primary_or_holding_business.id)
business_dates = [item.resolution_date.isoformat() for item in resolutions]
if business_dates:
amalgamation_filing['shareStructure']['resolutionDates'] = business_dates
Expand Down
10 changes: 10 additions & 0 deletions python/common/sql-versioning/sql_versioning/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,22 @@ def _after_configured(cls):
for pending_cls in cls._pending_version_classes:
version_cls = pending_cls._version_cls
mapper = inspect(pending_cls)

# Now add columns from the original table
for c in mapper.columns:
# Make sure table's column name and class's property name can be different
property_name = mapper.get_property_by_column(c).key
if not hasattr(version_cls, property_name):
setattr(version_cls, property_name, Column(c.name, c.type))

# Add relationships from the original table. Currently only works for one-to-one relationships
# TODO: get "many-to-..." relationships working
for r in mapper.relationships:
property_name = r.key
property_value = getattr(pending_cls, property_name)
if not hasattr(version_cls, property_name) and property_value:
setattr(version_cls, property_name, property_value)

delattr(cls, '_pending_version_classes')


Expand Down
25 changes: 25 additions & 0 deletions python/common/sql-versioning/tests/test_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class User(Base, Versioned):
name = Column(String)

address = orm.relationship('Address', backref='user', uselist=False)
locations = orm.relationship('Location', backref='user')

class Address(Base, Versioned):
__tablename__ = 'addresses'
Expand All @@ -46,6 +47,14 @@ class Address(Base, Versioned):

user_id = Column(Integer, ForeignKey('users.id'))

class Location(Base, Versioned):
__tablename__ = 'locations'

id = Column(Integer, primary_key=True)
name = Column(String)

user_id = Column(Integer, ForeignKey('users.id'))

orm.configure_mappers()


Expand Down Expand Up @@ -134,6 +143,22 @@ def test_versioning_insert(db, session):
assert result_versioned_address.operation_type == 0
assert result_versioned_address.end_transaction_id is None

def test_versioning_relationships(db, session):
user = User(name='user')
address = Address(name='address')
locations = [Location(name='location1'), Location(name='location2')]
user.address = address
user.locations = locations
session.add(user)
session.commit()

user_version = version_class(User)
result_revision = session.query(user_version)\
.filter(user_version.name=='user')\
.one_or_none()
assert result_revision.address == address
# assert result_revision.locations == locations


def test_versioning_delete(db, session):
"""Test deletion."""
Expand Down

0 comments on commit 9265ca7

Please sign in to comment.