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 194 default content addressable id for snapshots #3181

Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions lix/packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"sideEffects": false,
"dependencies": {
"dedent": "1.5.1",
"js-sha256": "^0.11.0",
"kysely": "^0.27.4",
"lodash-es": "^4.17.21",
"minimatch": "^10.0.1",
Expand Down
13 changes: 7 additions & 6 deletions lix/packages/sdk/src/change-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ test("should use queue and settled correctly", async () => {
if (textBefore === textAfter) {
return [];
}

return [
{
type: "text",
Expand Down Expand Up @@ -80,7 +81,7 @@ test("should use queue and settled correctly", async () => {
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll("change")
.select("snapshot.value")
.select("snapshot.content")
.execute();

expect(changes).toEqual([
Expand All @@ -93,7 +94,7 @@ test("should use queue and settled correctly", async () => {
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
content: {
text: "test",
},
},
Expand Down Expand Up @@ -146,7 +147,7 @@ test("should use queue and settled correctly", async () => {
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
.selectAll("change")
.select("snapshot.value")
.select("snapshot.content")
.execute();

expect(updatedChanges).toEqual([
Expand All @@ -159,7 +160,7 @@ test("should use queue and settled correctly", async () => {
type: "text",
file_id: "test",
plugin_key: "mock-plugin",
value: {
content: {
text: "test",
},
},
Expand All @@ -172,7 +173,7 @@ test("should use queue and settled correctly", async () => {
parent_id: updatedChanges[0]?.id,
plugin_key: "mock-plugin",
type: "text",
value: {
content: {
text: "test updated text",
},
},
Expand All @@ -185,7 +186,7 @@ test("should use queue and settled correctly", async () => {
entity_id: "test",
plugin_key: "mock-plugin",
type: "text",
value: {
content: {
text: "test updated text second update",
},
},
Expand Down
8 changes: 5 additions & 3 deletions lix/packages/sdk/src/database/applySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ export async function applySchema(args: { sqlite: SqliteDatabase }) {

CREATE INDEX IF NOT EXISTS idx_change_parent_id ON change (parent_id);

create TABLE IF NOT EXISTS snapshot (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
value TEXT
CREATE TABLE IF NOT EXISTS snapshot (
id TEXT GENERATED ALWAYS AS (sha256(content)) STORED UNIQUE,
content TEXT
) strict;

CREATE INDEX IF NOT EXISTS idx_content_hash ON snapshot (id);

CREATE TABLE IF NOT EXISTS conflict (
change_id TEXT NOT NULL,
conflicting_change_id TEXT NOT NULL,
Expand Down
18 changes: 18 additions & 0 deletions lix/packages/sdk/src/database/initDb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ test("change ids should default to uuid", async () => {
expect(validate(change.id)).toBe(true);
});

test("snapshot ids should default to sha256", async () => {
const sqlite = await createInMemoryDatabase({
readOnly: false,
});
const db = initDb({ sqlite });
const snapshot = await db
.insertInto("snapshot")
.values({
content: { a: "value from insert statement" },
})
.returningAll()
.executeTakeFirstOrThrow();

expect(snapshot.id).toBe(
"19ce22178013c4a047e8c90135ed57bfe4cc6451917dbb75f5b838922cf10b19",
);
});

// https://github.com/opral/lix-sdk/issues/71
test("files should be able to have metadata", async () => {
const sqlite = await createInMemoryDatabase({
Expand Down
10 changes: 10 additions & 0 deletions lix/packages/sdk/src/database/initDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v4 } from "uuid";
import { SerializeJsonPlugin } from "./serializeJsonPlugin.js";
import type { LixDatabaseSchema } from "./schema.js";
import { applySchema } from "./applySchema.js";
import { sha256 } from "js-sha256";

export function initDb(args: { sqlite: SqliteDatabase }) {
initDefaultValueFunctions({ sqlite: args.sqlite });
Expand All @@ -23,4 +24,13 @@ function initDefaultValueFunctions(args: { sqlite: SqliteDatabase }) {
arity: 0,
xFunc: () => v4(),
});

args.sqlite.createFunction({
name: "sha256",
arity: 1,
xFunc: (_ctx: number, value) => {
return value ? sha256(value as string) : "no-value";
},
deterministic: true,
});
}
10 changes: 5 additions & 5 deletions lix/packages/sdk/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ type SnapshotTable = {
* - 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>;
content?: Record<string, any>;
};

// TODO #185 rename value to snapshot_value
export type ChangeWithSnapshot = Change & { value: SnapshotTable["value"] };
export type NewChangeWithSnapshot = NewChange & {
value: SnapshotTable["value"];
// TODO #185 rename content to snapshot_content
export type ChangeWithSnapshot = Change & { content: SnapshotTable["content"] };
export type NewChangeWithSnapshot = Omit<NewChange, "snapshot_id"> & {
content: SnapshotTable["content"];
};

export type Conflict = Selectable<ConflictTable>;
Expand Down
16 changes: 9 additions & 7 deletions lix/packages/sdk/src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import type { LixDatabaseSchema, LixFile } from "./database/schema.js";
import type { DetectedChange, LixPlugin } from "./plugin.js";
import { minimatch } from "minimatch";
import { Kysely } from "kysely";
import { getLeafChange } from "./query-utilities/get-leaf-change.js";
import { isInSimulatedCurrentBranch } from "./query-utilities/is-in-simulated-branch.js";
import { getLeafChange } from "./query-utilities/get-leaf-change.js";

// start a new normalize path function that has the absolute minimum implementation.
function normalizePath(path: string) {
Expand Down Expand Up @@ -53,10 +53,8 @@ export async function handleFileInsert(args: {
const snapshot = await trx
.insertInto("snapshot")
.values({
// @ts-expect-error- database expects stringified json
value: detectedChange.snapshot
? JSON.stringify(detectedChange.snapshot)
: null,
// @ts-expect-error - database expects stringified json
content: JSON.stringify(detectedChange.snapshot),
})
.onConflict((oc) => oc.doNothing())
.returning("id")
Expand Down Expand Up @@ -145,11 +143,15 @@ export async function handleFileChange(args: {
.insertInto("snapshot")
.values({
// @ts-expect-error- database expects stringified json
value: detectedChange.snapshot
content: detectedChange.snapshot
? JSON.stringify(detectedChange.snapshot)
: null,
})
.onConflict((oc) => oc.doNothing())
.onConflict((oc) =>
oc.doUpdateSet((eb) => ({
content: eb.ref("excluded.content"),
})),
)
.returning("id")
.executeTakeFirstOrThrow();

Expand Down
Loading
Loading