Skip to main content

Library to handle versions for events

Project description

pevl

Python Event Versioning Library

This library provides code to upgrade events. Think of it as an event equivalent to flyway, where you can define the mappings between events and have all incoming events upgraded to a version your code can deal with, on the fly.

The target environment is either an event sourcing system, or microservices with an event bus - anywhere you're consuming events and doing something with them.

Pre-requisites

All events must be versioned. That is, there must be a version number in the event structure somewhere.

There must be a way to map from a given event version to version+1 for every version number. This is typically done by defining an upgrade method.

The library provides a class, Upgrader, which will take events in, extract the version number, run all appropriate upgrade methods until the version is current, then call an event factory with the upgraded data and return the appropriate object.

Design decisions (and whys)

  • Factory is only called with the latest data, not for each version - this is to save from having to maintain factories for every version.

  • Supporting Python 3.8 and above - because I want static types.

  • All events are dictionaries - for simplicity. Events are deepcopy'ed as part of the implementation.

  • Upgrade methods may mutate events in place - each event is provided as a deepcopy (so don't upgrade things that don't deepcopy). If you return, that will be used - if not, the assumed mutated event will be used.

  • Upgrade methods will get dictionaries and return dictionaries, and wrapping will be invisible - because I want this to be as simple to use as possible.

  • All versions should be strings, not numbers or other oddball things - for simplicity.

  • No version will be represented with str(None) (ie. 'None')

How to Use

Create a module that holds a bunch of version upgrade methods.

Each method should use the @pevl.event.upgrade decorator, and in the decorator you specify a version number to apply to and a new-version number to set (otherwise you must set the new version number in the upgrade script itself).

Each method should apply an appropriate upgrade from the version number given to the next version number.

Each method should be named descriptively.

Each method takes an event :dict, and returns an event: dict

Version numbers do not need to sort properly - but each version needs to upgrade to another version that is then handled (or not, if final version)

Example:

from pevl.event import upgrade

@upgrade('v0.1', 'v0.2')
def split_first_last_name(event):
    event['first'], event['last'] = event['name'].split()

The code to upgrade the events:

from pevl.event import Upgrader

upgrader = event.Upgrader(upgrades=[split_first_name, ])
upgraded = upgrader.ingest(event, target_version='v0.2')

For an example of the code that does the upgrades, see sample_sns.py

Likely Failure Modes

Make sure your versions match what comes out of the event itself. str/int mismatches may be silently ignored.

Make sure you have a version. Again, failed version matches may be ignored.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pevl-0.0.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pevl-0.0.3-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pevl-0.0.3.tar.gz.

File metadata

  • Download URL: pevl-0.0.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for pevl-0.0.3.tar.gz
Algorithm Hash digest
SHA256 0009915e94ae7b8865054744e48320d29dd68482e311e9e4b159fbec11175126
MD5 917be762a188cfe527c30097c29edca5
BLAKE2b-256 8bd33ea2b8f41412f6e3948fa88d50da30656b4f9a31e3631fb9d99994916a10

See more details on using hashes here.

Provenance

File details

Details for the file pevl-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: pevl-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.12

File hashes

Hashes for pevl-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3ff1f6761b564ec8d8dbea50c46ea6ff7b30150113f85a411ef92f22a707429e
MD5 6113cdc9dedab5b4fc5dfc3e45409b7c
BLAKE2b-256 bcf86c7a61995e5ca72ace3b6c9d24bf6db52f29ca545b71a6aa97d9513214c2

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page