Skip to content

Commit

Permalink
[7] Add rollback tests (#342)
Browse files Browse the repository at this point in the history
* Implement entity history tables for each entity

* Wip implement per table insert history item function with tests

* Add entity history schema creator

* Add eval function for inserting entity history

* Fix spelling of variable

* Move EntityHistory to its own file

* Add EntityHistoryRowAction enum

* Handle delete entities with enum flag

* Use new entity history tables for saving entity history

* Add serial to entity history

* Make InMemomoryStore static

* Move InMemomoryStore to static templates dir

* Add new rollback diffing queries

* Implement delete rolled back entity history

* Implement multichain rollback filters

* Implement unordered and ordered multichain versions of rollbacks

* Make tests compile

* Fix broken magic in memory table function

* Fix double quoted dynamic table name in query

* Add new test expectations

* Refactor simpler in memory table entity history state

* Fix diff and delete entity history functions

* Fix tests

* Fix inverted rollback check

* Implement entity history pruning

* Remove all code related to previous entity_history table

* Remove union schemas for float and int

* Fix tests

* Implement dynamic contract registry as entity

* Fix test to allow for duplicates in raw events table

* Add back condition for validating evm contract address

* Remove unused dynamic contract registry key

* Fix mocha fail binding

* Add depenency injection for testing history functions

* Add rollback diff tests

* Strip undefined fields from entity history delete parser

* Add tests for deleting rolled back history

* Fix prune entity history function add tests

* Remove commented code
  • Loading branch information
JonoPrest authored Nov 19, 2024
1 parent 46f07db commit ede2d57
Show file tree
Hide file tree
Showing 5 changed files with 444 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Assert = {
external doesNotThrow: (unit => 'a, ~error: 'error=?, ~message: string=?) => unit = "doesNotThrow"

@module("assert") external ok: (bool, ~message: string=?) => unit = "ok"
@module("assert") external fail: string => unit = "fail"
@module("assert") external fail: string => 'a = "fail"
}

/* Mocha bindings on `this` for `describe` and `it` functions */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ module EntityHistory = {
sql,
~isUnorderedMultichainMode,
~eventIdentifier: Types.eventIdentifier,
~allEntities=Entities.allEntities,
): promise<unit> => {
let {chainId, blockNumber, blockTimestamp} = eventIdentifier
let args: Args.t = isUnorderedMultichainMode
Expand All @@ -385,7 +386,7 @@ module EntityHistory = {
safeBlockTimestamp: blockTimestamp,
})

Entities.allEntities
allEntities
->Belt.Array.map(async entityMod => {
let module(Entity) = entityMod
try await deleteRolledBackEntityHistory(
Expand Down Expand Up @@ -455,11 +456,15 @@ module EntityHistory = {
}
}

let getFirstChangeEventPerChain = async (sql, args: Args.t) => {
let getFirstChangeEventPerChain = async (
sql,
args: Args.t,
~allEntities=Entities.allEntities,
) => {
let firstChangeEventPerChain = FirstChangeEventPerChain.make()

let _ =
await Entities.allEntities
await allEntities
->Belt.Array.map(async entityMod => {
let module(Entity) = entityMod
let res = try await getFirstChangeEntityHistoryPerChain(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ module.exports.pruneStaleEntityHistory = (
FROM
public.${sql(tableName)}
WHERE
serial >= (SELECT serial FROM first_change)
serial >= (SELECT first_change_serial FROM first_change)
ORDER BY
id,
serial ASC -- Select the row with the lowest serial per id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ let makeHistoryRowSchema: S.t<'entity> => S.t<historyRow<'entity>> = entitySchem
},
entityData: switch v["action"] {
| SET => v["entityData"]->(Utils.magic: Js.Dict.t<unknown> => 'entity)->Set
| DELETE => v["entityData"]->(Utils.magic: Js.Dict.t<unknown> => entityIdOnly)->Delete
| DELETE =>
let {id} = v["entityData"]->(Utils.magic: Js.Dict.t<unknown> => entityIdOnly)
Delete({id: id})
},
},
serializer: v => {
Expand Down
Loading

0 comments on commit ede2d57

Please sign in to comment.