patchlevel / event-sourcing

An event sourcing library, complete with all the essential features, powered by the reliable Doctrine ecosystem and focused on developer experience.
https://event-sourcing.patchlevel.io
MIT License
115 stars 4 forks source link

Find upgrade path for new headers in messages #553

Open DavidBadura opened 1 month ago

DavidBadura commented 1 month ago

It is currently not possible to upgrade from version 1 to version 2 without hacking into the DB yourself. An upgrade path must be found here.

DavidBadura commented 1 month ago

POC:

<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\Message\Serializer;

final class LegacyHeadersSerializer implements HeadersSerializer
{
    public function __construct(
        private readonly HeadersSerializer $parent,
    ) {
    }

    /**
     * @param list<object> $headers
     *
     * @return array<string, array<string, mixed>>
     */
    public function serialize(array $headers): array
    {
        return $this->parent->serialize($headers);
    }

    /**
     * @param array<string, mixed> $serializedHeaders
     *
     * @return list<object>
     */
    public function deserialize(array $serializedHeaders): array
    {
        $legacyHeaders = [];

        if (array_key_exists('application-id', $serializedHeaders)) {
            $legacyHeaders[] = new ApplicationHeader($serializedHeaders['application-id']);
            unset($serializedHeaders['application-id']);
        }

        $headers = $this->parent->deserialize($serializedHeaders);

        return array_merge($legacyHeaders, $headers);
    }
}