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

Lixdk 167 #3168

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lix/packages/sdk/src/change-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,14 @@ test("should use queue and settled correctly", async () => {
},
]);

const changes = await lix.db.selectFrom("change").selectAll().execute();
const changes = await lix.db.selectFrom("change").innerJoin("snapshot", "snapshot.id", "change.snapshot_id").selectAll().execute();

expect(changes).toEqual([
{
id: changes[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
Expand Down Expand Up @@ -187,6 +188,7 @@ test("should use queue and settled correctly", async () => {

const updatedChanges = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.execute();

Expand All @@ -195,6 +197,7 @@ test("should use queue and settled correctly", async () => {
author: null,
id: updatedChanges[0]?.id,
created_at: updatedChanges[0]?.created_at,
snapshot_id: updatedChanges[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
Expand All @@ -211,6 +214,7 @@ test("should use queue and settled correctly", async () => {
author: null,
commit_id: null,
created_at: updatedChanges[1]?.created_at,
snapshot_id: updatedChanges[1]?.snapshot_id,
file_id: "test",
id: updatedChanges[1]?.id,
meta: null,
Expand All @@ -227,6 +231,7 @@ test("should use queue and settled correctly", async () => {
author: null,
commit_id: null,
created_at: updatedChanges[2]?.created_at,
snapshot_id: updatedChanges[2]?.snapshot_id,
file_id: "test",
id: updatedChanges[2]?.id,
meta: null,
Expand Down
14 changes: 12 additions & 2 deletions lix/packages/sdk/src/commit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ test("should be able to add and commit changes", async () => {
},
},
};

const lix = await openLixInMemory({
blob: await newLixFile(),
providePlugins: [mockPlugin],
Expand All @@ -59,7 +60,10 @@ test("should be able to add and commit changes", async () => {

await lix.settled();

const changes = await lix.db.selectFrom("change").selectAll().execute();
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());

Expand All @@ -68,6 +72,7 @@ test("should be able to add and commit changes", async () => {
id: changes[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
Expand Down Expand Up @@ -95,6 +100,7 @@ test("should be able to add and commit changes", async () => {
const commits = await lix.db.selectFrom("commit").selectAll().execute();
const commitedChanges = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.execute();

Expand All @@ -103,6 +109,7 @@ test("should be able to add and commit changes", async () => {
id: commitedChanges[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
Expand Down Expand Up @@ -138,6 +145,7 @@ test("should be able to add and commit changes", async () => {

const updatedChanges = await lix.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.execute();

Expand All @@ -146,6 +154,7 @@ test("should be able to add and commit changes", async () => {
id: updatedChanges[0]?.id!,
author: null,
created_at: updatedChanges[0]?.created_at,
snapshot_id: updatedChanges[0]?.snapshot_id,
parent_id: null,
type: "text",
file_id: "test",
Expand All @@ -162,7 +171,8 @@ test("should be able to add and commit changes", async () => {
id: updatedChanges[1]?.id!,
author: null,
parent_id: updatedChanges[0]?.id!,
created_at: updatedChanges[0]?.created_at,
created_at: updatedChanges[1]?.created_at,
snapshot_id: updatedChanges[1]?.snapshot_id,
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
Expand Down
11 changes: 8 additions & 3 deletions lix/packages/sdk/src/database/createSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export async function createSchema(args: { db: Kysely<any> }) {
file_id TEXT NOT NULL,
plugin_key TEXT NOT NULL,
operation TEXT NOT NULL,
value TEXT,
meta TEXT,
snapshot_id TEXT NOT NULL,
commit_id TEXT,
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
meta TEXT
) strict;

create TABLE snapshot (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
value TEXT
) strict;

CREATE TABLE conflict (
Expand Down
1 change: 1 addition & 0 deletions lix/packages/sdk/src/database/initDb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ test("change ids should default to uuid", async () => {
file_id: "mock",
plugin_key: "mock-plugin",
operation: "create",
snapshot_id: 'sn1'
})
.returningAll()
.executeTakeFirstOrThrow();
Expand Down
35 changes: 23 additions & 12 deletions lix/packages/sdk/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type LixDatabaseSchema = {
file_internal: LixFileTable;
change_queue: ChangeQueueTable;
conflict: ConflictTable;
snapshot: SnapshotTable;

// discussion
discussion: DiscussionTable;
Expand Down Expand Up @@ -67,10 +68,9 @@ type CommitTable = {

export type Change = Selectable<ChangeTable>;
export type NewChange = Insertable<ChangeTable>;
export type ChangeUpdate = Updateable<ChangeTable>;
type ChangeTable = {
id: Generated<string>;
parent_id?: ChangeTable["id"];
parent_id: Generated<string> | null;
author?: string;
file_id: string;
/**
Expand Down Expand Up @@ -101,16 +101,7 @@ type ChangeTable = {
* - "user" for a user change
*/
type: string;
/**
* The value of the change.
*
* The value is `undefined` for a delete operation.
*
* @example
* - For a csv cell change, the value would be the new cell value.
* - For an inlang message change, the value would be the new message.
*/
value?: Record<string, any> & { id: string };
snapshot_id: string;
/**
* Additional metadata for the change used by the plugin
* to process changes.
Expand All @@ -122,6 +113,26 @@ type ChangeTable = {
created_at: Generated<string>;
};

export type Snapshot = Selectable<SnapshotTable>;
export type NewSnapshot = Insertable<SnapshotTable>;
type SnapshotTable = {
id: Generated<string>
/**
* The value of the change.
*
* The value is `undefined` for a delete operation.
*
* @example
* - For a csv cell change, the value would be the new cell value.
* - For an inlang message change, the value would be the new message.
*/
value?: Record<string, any> & { id: string };
}

// TODO #185 rename value to snapshot_value
export type ChangeWithSnapshot = Change & { value: SnapshotTable['value'] };


export type Conflict = Selectable<ConflictTable>;
export type NewConflict = Insertable<ConflictTable>;
export type ConflictUpdate = Updateable<ConflictTable>;
Expand Down
12 changes: 10 additions & 2 deletions lix/packages/sdk/src/discussion/discussion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ test("should be able to start a discussion on changes", async () => {

await lix.settled();

const changes = await lix.db.selectFrom("change").selectAll().execute();
const changes = await lix.db.selectFrom("change")
.selectAll()
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.execute();

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

Expand All @@ -65,6 +68,7 @@ test("should be able to start a discussion on changes", async () => {
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",
Expand Down Expand Up @@ -136,7 +140,10 @@ test("should fail to create a disussion on non existing changes", async () => {

await lix.settled();

const changes = await lix.db.selectFrom("change").selectAll().execute();
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());

Expand All @@ -145,6 +152,7 @@ test("should fail to create a disussion on non existing changes", async () => {
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",
Expand Down
27 changes: 23 additions & 4 deletions lix/packages/sdk/src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ export async function handleFileInsert(args: {
for (const diff of diffs ?? []) {
const value = diff.before ?? diff.after;

const snapshotId = (await trx
.insertInto("snapshot")
.values({
id: v4(),
// @ts-expect-error - database expects stringified json
value: JSON.stringify(value)
})
.returning("id")
.executeTakeFirstOrThrow()).id

await trx
.insertInto("change")
.values({
Expand All @@ -53,8 +63,7 @@ export async function handleFileInsert(args: {
author: args.currentAuthor,
plugin_key: pluginKey,
operation: diff.operation,
// @ts-expect-error - database expects stringified json
value: JSON.stringify(value),
snapshot_id: snapshotId,
// @ts-expect-error - database expects stringified json
meta: JSON.stringify(diff.meta),
// add queueId interesting for debugging or knowning what changes were generated in same worker run
Expand Down Expand Up @@ -112,6 +121,7 @@ export async function handleFileChange(args: {

const previousChanges = await trx
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll()
.where("file_id", "=", fileId)
.where("plugin_key", "=", pluginKey)
Expand Down Expand Up @@ -151,6 +161,16 @@ export async function handleFileChange(args: {
}
}

const snapshotId = (await trx
.insertInto("snapshot")
.values({
id: v4(),
// @ts-expect-error - database expects stringified json
value: JSON.stringify(value),
})
.returning('id')
.executeTakeFirstOrThrow()).id;

await trx
.insertInto("change")
.values({
Expand All @@ -160,8 +180,7 @@ export async function handleFileChange(args: {
plugin_key: pluginKey,
author: args.currentAuthor,
parent_id: previousChange?.id,
// @ts-expect-error - database expects stringified json
value: JSON.stringify(value),
snapshot_id: snapshotId,
// @ts-expect-error - database expects stringified json
meta: JSON.stringify(diff.meta),
operation: diff.operation,
Expand Down
Loading
Loading