Skip to content

[1] Entity History Rework#310

Merged
JonoPrest merged 4 commits into
mainfrom
jp/entity_history
Nov 18, 2024
Merged

[1] Entity History Rework#310
JonoPrest merged 4 commits into
mainfrom
jp/entity_history

Conversation

@JonoPrest

Copy link
Copy Markdown
Collaborator
  1. Creates entity history table per entity
  2. Creates an insert function for these tables
  3. Creates a schema for the table
  4. Adds tests

Next PR will apply the insert function on writes.

@JonoPrest JonoPrest requested a review from DZakh October 31, 2024 11:40
Comment on lines +60 to +64
{
"current": s.flatten(currentHistoryFieldsSchema),
"previous": s.flatten(previousHistoryFieldsSchema),
"entityData": s.flatten(entitySchema),
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using flatten here with a transform below to the actual record was the best way I could think of creating this schema.

sql->Postgres.unsafe(`SELECT * FROM "public"."${mockEntityHistory.table.tableName}"`)

describe("Entity history serde", () => {
it("serializes and deserializes correctly", () => {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test explains the desired effect/approach of using the schema for an entity

})

describe("Entity History Codegen", () => {
it("Creates an insert function", () => {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one just illustrates the insert function that gets generated

Assert.equal(expected, mockEntityHistory.createInsertFnQuery)
})

Async.it("Creating tables and functions works", async () => {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one demonstrates the function and how it will be called. Although each entity will have an actual function it can call in the next PR

@DZakh DZakh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really like the tests 👍

{
"current": s.flatten(currentHistoryFieldsSchema),
"previous": s.flatten(previousHistoryFieldsSchema),
"entityData": s.flatten(entitySchema),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It probably won't happen, but it might break if the entity has fields entity_history_log_index etc


let createInsertFnQuery = {
let insertFnName = `"insert_${table.tableName}"`
let historRowArg = "history_row"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let historRowArg = "history_row"
let historyRowArg = "history_row"

Comment on lines +213 to +215
ORDER BY ${currentChangeFieldNames
->Belt.Array.map(fieldName => fieldName ++ " DESC")
->Js.Array2.joinWith(", ")}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should take the previous one even without the ORDER BY

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postgres doesn't guarentee ordering say by insertion. So what we should probably do is have a serial field that increments and we can order by that instead. What do you think?

Reckon we will need that for the suggested unordered multichain implementation as well.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Postgres doesn't guarentee ordering say by insertion.

Didn't know that.

So what we should probably do is have a serial field that increments and we can order by that instead. What do you think?

This sounds good, but the current approach also works

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works for ordered multichain 😬

@JonoPrest JonoPrest force-pushed the jp/entity_history branch 2 times, most recently from f500b14 to 3ae5772 Compare November 6, 2024 09:42
@JonoPrest JonoPrest changed the title Entity History Rework 1 [1] Entity History Rework Nov 7, 2024
@JonoPrest JonoPrest merged commit 881a193 into main Nov 18, 2024
@JonoPrest JonoPrest deleted the jp/entity_history branch November 18, 2024 12:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants