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

refactor: add entity id #3170

Merged
merged 2 commits into from
Oct 16, 2024

Conversation

samuelstroschein
Copy link
Member

Closes opral/lix-sdk#94

Changes

  • adds entity_id to a change
  • removes the & { id: string} requirement from snapshots
  • cleans tests

Copy link

changeset-bot bot commented Oct 15, 2024

⚠️ No Changeset found

Latest commit: 754eff8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Comment on lines 85 to 98
.select([
"change.id",
"change.author",
"change.created_at",
"change.snapshot_id",
"change.parent_id",
"change.entity_id",
"change.type",
"change.file_id",
"change.plugin_key",
"snapshot.value as value",
"change.meta",
"change.commit_id",
])
Copy link
Member Author

Choose a reason for hiding this comment

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

This is required because an .innerJoin("snapshot", "snapshot.id", "change.snapshot_id") select all overshadows the change.id with the snapshot.id

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh god what a beast - this combined with flaky run envs in test (importing from dist...) Sounds like fun.

Copy link
Contributor

Choose a reason for hiding this comment

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

I looked into the code - seems there are a couple more places where this is still a problem

Copy link
Contributor

@martin-lysk martin-lysk Oct 16, 2024

Choose a reason for hiding this comment

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

I wonder if we can use some bits in the uuid v7 to encode the entity type (like id of a change vs id of a snapshot) - In a former project we used some of the machine id bits in snowflake to signal the type of the id. This allowed us to runtime assert correct entities early based on passed id's - saved us hours, if not days of debugging and post mortems.

Not the solution - and I need into uuid v7 but to outline what I have in mind:

If every id of a change must end with a C
40e97286-9b78-4a35-8fb1-0036bdfbbf7C

Every id of a snapshot ends with an S
40e97286-9b78-4a35-8fb1-0036bdfbbf7S

We could assert in a function like getLeafChange that the id is an expected one:

getLeafChange(change) {
  if (!change.id.endsWith('C')) {
    throw new WrongObjectTypePassedError("The Id passed is not the id of a change")
  } 
 ....
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I double checked kysely we can make use selectAll with 'change' as parameter and and an explicit select

Suggested change
.select([
"change.id",
"change.author",
"change.created_at",
"change.snapshot_id",
"change.parent_id",
"change.entity_id",
"change.type",
"change.file_id",
"change.plugin_key",
"snapshot.value as value",
"change.meta",
"change.commit_id",
])
+ .selectAll('change')
+ .select('snapshot.value')

which is translated to:

SELECT
  "change".*,
  "snapshot"."value"

I will commit on this pr and fix the other places where we make use of innerjoin as well

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh wow I was looking for select('change.*'). I didn't think about using selectAll('change'). Nice.

Comment on lines -139 to -165
const changes = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.execute();

// console.log(await lix.db.selectFrom("queue").selectAll().execute());

expect(changes).toEqual([
{
id: changes[0]?.id,
author: "Test User",
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
id: "test",
text: "inserted text",
},
meta: null,
commit_id: null,
},
]);

Copy link
Member Author

Choose a reason for hiding this comment

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

The discussion test tested the change queue :D redundant

});

if (previousCommittedDiff?.length === 0) {
const previousSnapshot = await trx
Copy link
Member Author

Choose a reason for hiding this comment

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

Very nice: The snapshot can now be queried on demand.

Copy link
Contributor

Choose a reason for hiding this comment

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

Shall we remove the lix.commit

Copy link
Member Author

Choose a reason for hiding this comment

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

I propose to remove it in another PR. One step at a time

Copy link
Contributor

Choose a reason for hiding this comment

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

was just about that one line in the test (as you where cleaning up the discussion tests as well - but not important)

// console.log({ args });
for (const plugin of args.plugins) {
// glob expressions are expressed relative without leading / but path has leading /
if (!minimatch(normalizePath(args.after.path), "/" + plugin.glob)) {
break;
}

const diffs = await plugin.diff!.file!({
const diffs = await plugin.diff?.file?.({
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the diff function optional for plugins? I would prefer to fail here - while we should check for undefined with a nice error message instead of the ! I would rather fail early instead of returning - no change silently

Copy link
Member Author

Choose a reason for hiding this comment

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


for (const plugin of args.plugins) {
// glob expressions are expressed relative without leading / but path has leading /
if (!minimatch(normalizePath(args.after.path), "/" + plugin.glob)) {
break;
}

const diffs = await plugin.diff!.file!({
const diffs = await plugin.diff?.file?.({
Copy link
Contributor

Choose a reason for hiding this comment

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

see previous comment about failling instead of optional chaining

Copy link
Member Author

Choose a reason for hiding this comment

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

author: args.currentAuthor,
parent_id: previousChange?.id,
snapshot_id: snapshotId,
snapshot_id: snapshot.id,
Copy link
Contributor

Choose a reason for hiding this comment

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

way better/cleaner than the (await ...).id

@samuelstroschein samuelstroschein merged commit e16b755 into lix-integration Oct 16, 2024
2 checks passed
@samuelstroschein samuelstroschein deleted the lixdk-187-add-changeentity_id branch October 16, 2024 09:04
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants