# Challenge Scratch My notes for the technical challenge. ## Original request from Justin Link to the gist: https://gist.github.com/bodymindarts/9919abc26a1dbbcd71d426030000df62 > # Write a trigger function that 'rolls up' the customer events in PG > > We would like to ask you to work on a POC for a solution to the 'event sourced entities' issue I explained in the interview. > > The approach we would like you to explore is to write a postgres trigger function that creates / updates a rollup of the entity events and gets committed atomically. > > In this task we will focus only on the 'user' entities. > > Anytime an event gets added to `core_user_events` table the PG function should atomically write to the rollup table with the data representing the current snapshot of a user. > > Please commit a working example to a fork of our lana repository and send us a link to it when you are ready. > > In a follow up interview we will dive into the approach you took and what thoughts you have around topics such as maintainability and extendability. > > The following are instructions on how to get the system into a usefull state for experimenting: > > ## Dependencies > > Nix: > - Recommended install method using https://github.com/DeterminateSystems/nix-installer > ``` > curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install > ``` > > docker > > ## Dev shell > > Clone the repo: `git clone git@github.com:GaloyMoney/lana-bank.git && cd lana-bank` > > Use `nix develop` to enter the dev shell or `direnv allow` if you have direnv installed - this will install all the dev dependencies you should need. > > > ## Run tests > > start dependencies: > ``` > make reset-deps run-server > ``` > > Compilation can take a while but eventually you should see the following logs: > ``` > Starting customer server on port 5254 > Starting admin server on port 5253 > ``` > > Run the 'superuser' end to end tests in another shell: > > ``` > bats -t bats/superuser.bats > ``` > > This will seed the database with some user events. > You can see the result via: > > ``` > $ psql postgres://user:password@localhost:5433/pg -c 'select count(*) from core_user_events;' > count > ------- > 6 > (1 row) > ``` > > ## Solution > > Create a new migration file via: > ``` > cd lana/app > cargo sqlx migrate add > ``` > > you can add / remove the migration via > ``` > cd lana/app > cargo sqlx migrate run > cargo sqlx migrate revert ``` > > rerun `make reset-deps run-server` to wipe the database state. > > > We are looking forward to your ideas - happy coding! ## My notes ### Understanding the request * We need to modify the project so that we materialize the state of users on each user event. * This needs to be done with a postgres trigger. * Our changes in Postgres must be delivered with a new migration. * The whole new state of the repo must be in a fork in Github which I'll send over. Interesting bits: * How to ensure atomicity of rollup state (no way for an event to be added without the ) * How to possible create the rollup in a lana instance that is already rolling and has events? (I consider this to be out of scope in terms of implementing, but I should have an opinion) * How to do something that's not hardcoded to the user events, but rather is extensible to other events? Potentially leverage some schema definitions to dynamically generate Postgres triggers? Maybe we need some artifact where we declare which events must be rolled up and add some config needed to do so? I'll probably begin with a naive, hardcoded approach to users and then reflect on how that can be extended to other aggregates. * I'll try to understand the testing of the project to see where can I add tests to ensure that my rollup works fine. First bits I'll explore before moving on: * Getting the environment working. * Run the tests. * Get more familiar with the user events and how schemas are defined and used in the repo. * Get more familiar with how the tests are written. I'll clear those first and then take it from there.