Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1] Entity History Rework #310

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

[1] Entity History Rework #310

wants to merge 3 commits into from

Conversation

JonoPrest
Copy link
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
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
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
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
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

Copy link
Member

@DZakh DZakh left a comment

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
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
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
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
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
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
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
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