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.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

pevl-0.0.2-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pevl-0.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.0

File hashes

Hashes for pevl-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d09c55c55e41fb81768225ad6f5c0935262095004346e289e92b31b497f9c510
MD5 265248f6eb39604cc425d2da71be7e59
BLAKE2b-256 aff56333c890ed2e33287efcdb7c94a917f002c0fedd85ee6486465b05a1652b

See more details on using hashes here.

Provenance

File details

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

File metadata

  • Download URL: pevl-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.8.0

File hashes

Hashes for pevl-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 981c5a7d703d1eae6d9b13dc77b5a5088639df7c58946b0becf63a8ded5b1ccb
MD5 4edd72e63f29dc75587d0e088c73a004
BLAKE2b-256 9d80ee348ed60a4bd35e690a2351741d4821c06b16e6e73740aebd2cff6f6719

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