Giter Club home page Giter Club logo

Comments (2)

AidasK avatar AidasK commented on June 20, 2024 1

Probably we don't even need all of this logic anymore as we could just update one record each time. We could not do this in earlier versions of laravel as upsert method was introduced only recently. Though for this to work we would also need to add unique index on snapshots.aggregate_uuid and that would be a breaking change. Entire application would need to stop to clear duplicate records and to add a new index, it does not sound easy.

$this->snapshotModel()->upsert([
    'aggregate_uuid' => $snapshot->aggregateUuid, 
    'aggregate_version' => $snapshot->aggregateVersion,
    'state' => $snapshot->state,
], ['aggregate_uuid'], ['aggregate_version', 'state', 'updated_at']);

I would be willing to create a pull requests with all of these strategies and tests and make it configurable via config.

from laravel-event-sourcing.

AidasK avatar AidasK commented on June 20, 2024

Would you accept this pull request? It ensures that all old snapshots would get deleted after a new one was created.
EloquentSnapshotRepository.php:

    public function persist(Snapshot $snapshot): Snapshot
    {
        /** @var \Spatie\EventSourcing\Snapshots\EloquentSnapshot $eloquentSnapshot */
        $eloquentSnapshot = new $this->snapshotModel();

        $eloquentSnapshot->aggregate_uuid = $snapshot->aggregateUuid;
        $eloquentSnapshot->aggregate_version = $snapshot->aggregateVersion;
        $eloquentSnapshot->state = $snapshot->state;

        $eloquentSnapshot->save();

        $snapshts = $eloquentSnapshot::query()
            ->where('aggregate_uuid', $snapshot->aggregateUuid)
            ->where('aggregate_version', '<', $snapshot->aggregateVersion)
            ->oldest()
            ->limit(5)
            ->pluck('id');
        $eloquentSnapshot::query()->whereIn('id', $ids)->delete();

        return $eloquentSnapshot->toSnapshot();
    }

Notice that I have also added a limit to this query. Probably at this point some aggregate roots might have more than 1000 snapshots and we don't want to timeout these requests.

I am not doing a simple query like below, since mysql performs best, then delete is performed on unique index. I want to avoid gap locking at any cost. Also aggregate_version column has no index and that might create a lock on entire table.

        $this->snapshotModel()
             ->query()
             ->where('aggregate_uuid', $snapshot->aggregateUuid)
             ->where('aggregate_version', '<', $snapshot->aggregateVersion)
             ->delete();

Drawbacks? Migration period might create some temporary latencies while each aggregate root is performing snapshot cleanup.

from laravel-event-sourcing.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.