Skip to content

Commit

Permalink
refactor: remove author for data structure stability milestone
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelstroschein committed Oct 17, 2024
1 parent 4e1f66c commit 73e3ac5
Show file tree
Hide file tree
Showing 10 changed files with 2 additions and 51 deletions.
11 changes: 1 addition & 10 deletions lix/packages/sdk/src/change-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ test("should use queue and settled correctly", async () => {
expect(changes).toEqual([
{
id: changes[0]?.id,
author: null,
created_at: changes[0]?.created_at,
snapshot_id: changes[0]?.snapshot_id,
parent_id: null,
Expand Down Expand Up @@ -152,7 +151,6 @@ test("should use queue and settled correctly", async () => {

expect(updatedChanges).toEqual([
{
author: null,
id: updatedChanges[0]?.id,
created_at: updatedChanges[0]?.created_at,
snapshot_id: updatedChanges[0]?.snapshot_id,
Expand All @@ -166,7 +164,6 @@ test("should use queue and settled correctly", async () => {
},
},
{
author: null,
entity_id: "test",
created_at: updatedChanges[1]?.created_at,
snapshot_id: updatedChanges[1]?.snapshot_id,
Expand All @@ -180,7 +177,6 @@ test("should use queue and settled correctly", async () => {
},
},
{
author: null,
created_at: updatedChanges[2]?.created_at,
snapshot_id: updatedChanges[2]?.snapshot_id,
file_id: "test",
Expand All @@ -196,7 +192,7 @@ test("should use queue and settled correctly", async () => {
]);
});

test("changes should contain the author", async () => {
test.todo("changes should contain the author", async () => {
const mockPlugin: LixPlugin = {
key: "mock-plugin",
glob: "*",
Expand All @@ -222,8 +218,6 @@ test("changes should contain the author", async () => {
providePlugins: [mockPlugin],
});

await lix.currentAuthor.set("some-id");

// testing an insert

await lix.db
Expand Down Expand Up @@ -257,9 +251,6 @@ test("changes should contain the author", async () => {

expect(changes2[1]?.author).toBe("some-id");

// testing setting the author
await lix.currentAuthor.set("some-other-id");

await lix.db
.updateTable("file")
.set({
Expand Down
2 changes: 0 additions & 2 deletions lix/packages/sdk/src/database/applySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export async function applySchema(args: { sqlite: SqliteDatabase }) {
CREATE TABLE IF NOT EXISTS change (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
author TEXT,
parent_id TEXT,
entity_id TEXT NOT NULL,
type TEXT NOT NULL,
Expand Down Expand Up @@ -95,7 +94,6 @@ export async function applySchema(args: { sqlite: SqliteDatabase }) {
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
parent_id TEXT,
discussion_id TEXT NULL,
author_id TEXT NOT NULL,
created_at TEXT DEFAULT CURRENT_TIMESTAMP NOT NULL,
body TEXT NOT NULL,
Expand Down
2 changes: 0 additions & 2 deletions lix/packages/sdk/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export type NewChange = Insertable<ChangeTable>;
type ChangeTable = {
id: Generated<string>;
parent_id: Generated<string> | null;
author?: string;
/**
* The entity the change refers to.
*/
Expand Down Expand Up @@ -135,7 +134,6 @@ type CommentTable = {
id: Generated<string>;
parent_id?: string;
discussion_id: string;
author_id: string;
created_at: Generated<string>;
body: string;
};
2 changes: 0 additions & 2 deletions lix/packages/sdk/src/discussion/add-comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { Comment, LixDatabaseSchema } from "../database/schema.js";

export async function addComment(args: {
db: Kysely<LixDatabaseSchema>;
currentAuthor: string;
parentCommentId: string;
body: string;
}): Promise<{ id: Comment["id"] }> {
Expand All @@ -20,7 +19,6 @@ export async function addComment(args: {
.values({
parent_id: args.parentCommentId,
discussion_id,
author_id: args.currentAuthor,
body: args.body,
})
.returning("id")
Expand Down
2 changes: 0 additions & 2 deletions lix/packages/sdk/src/discussion/create-discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { LixDatabaseSchema } from "../database/schema.js";

export async function createDiscussion(args: {
db: Kysely<LixDatabaseSchema>;
currentAuthor: string;
changeIds?: string[];
body: string;
}) {
Expand Down Expand Up @@ -35,7 +34,6 @@ export async function createDiscussion(args: {
.values({
parent_id: undefined,
discussion_id: discussion.id,
author_id: args.currentAuthor,
body: args.body,
})
.execute();
Expand Down
4 changes: 0 additions & 4 deletions lix/packages/sdk/src/discussion/discussion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ test("should be able to start a discussion on changes", async () => {
providePlugins: [mockPlugin],
});

lix.currentAuthor.set("Test User");

const enc = new TextEncoder();

await lix.db
Expand Down Expand Up @@ -86,8 +84,6 @@ test("should fail to create a disussion on non existing changes", async () => {
providePlugins: [mockPlugin],
});

lix.currentAuthor.set("Test User");

const enc = new TextEncoder();

await lix.db
Expand Down
4 changes: 0 additions & 4 deletions lix/packages/sdk/src/file-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export async function handleFileInsert(args: {
after: LixFile;
plugins: LixPlugin[];
db: Kysely<LixDatabaseSchema>;
currentAuthor?: string;
queueEntry: any;
}) {
const detectedChanges: Array<DetectedChange & { pluginKey: string }> = [];
Expand Down Expand Up @@ -68,7 +67,6 @@ export async function handleFileInsert(args: {
.values({
type: detectedChange.type,
file_id: args.after.id,
author: args.currentAuthor,
entity_id: detectedChange.entity_id,
plugin_key: detectedChange.pluginKey,
snapshot_id: snapshot.id,
Expand All @@ -89,7 +87,6 @@ export async function handleFileChange(args: {
before: LixFile;
after: LixFile;
plugins: LixPlugin[];
currentAuthor?: string;
db: Kysely<LixDatabaseSchema>;
}) {
const fileId = args.after?.id ?? args.before?.id;
Expand Down Expand Up @@ -163,7 +160,6 @@ export async function handleFileChange(args: {
file_id: fileId,
plugin_key: detectedChange.pluginKey,
entity_id: detectedChange.entity_id,
author: args.currentAuthor,
parent_id: leafChange?.id,
snapshot_id: snapshot.id,
})
Expand Down
5 changes: 0 additions & 5 deletions lix/packages/sdk/src/merge/merge.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,6 @@ test("it should copy discussion and related comments and mappings", async () =>
providePlugins: [mockPlugin],
});

lix1.currentAuthor.set("Test User");

const enc = new TextEncoder();

await lix1.db
Expand All @@ -637,8 +635,6 @@ test("it should copy discussion and related comments and mappings", async () =>
providePlugins: [mockPlugin],
});

lix2.currentAuthor.set("Test User 2");

const changes = await lix1.db
.selectFrom("change")
.innerJoin("snapshot", "snapshot.id", "change.snapshot_id")
Expand All @@ -649,7 +645,6 @@ test("it should copy discussion and related comments and mappings", async () =>
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,
Expand Down
20 changes: 1 addition & 19 deletions lix/packages/sdk/src/open/openLix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export async function openLix(args: {
},
});

let currentAuthor: string | undefined;

let pending: Promise<void> | undefined;
let resolve: () => void;
// run number counts the worker runs in a current batch and is used to prevent race conditions where a trigger is missed because a previous run is just about to reset the hasMoreEntriesSince flag
Expand Down Expand Up @@ -87,7 +85,6 @@ export async function openLix(args: {

if (existingFile?.data) {
await handleFileChange({
currentAuthor,
queueEntry: entry,
before: {
...existingFile,
Expand All @@ -102,7 +99,6 @@ export async function openLix(args: {
});
} else {
await handleFileInsert({
currentAuthor,
queueEntry: entry,
after: {
...entry,
Expand Down Expand Up @@ -155,13 +151,6 @@ export async function openLix(args: {
return {
db,
settled,
currentAuthor: {
get: () => currentAuthor,
// async setter for future proofing
set: async (author: string) => {
currentAuthor = author;
},
},
toBlob: async () => {
await settled();
return new Blob([contentFromDatabase(args.database)]);
Expand All @@ -174,20 +163,13 @@ export async function openLix(args: {
await db.destroy();
},
createDiscussion: (args: { changeIds?: string[]; body: string }) => {
if (currentAuthor === undefined) {
throw new Error("current author not set");
}
return createDiscussion({
...args,
db,
currentAuthor,
});
},
addComment: (args: { parentCommentId: string; body: string }) => {
if (!currentAuthor) {
throw new Error("current author not set");
}
return addComment({ ...args, db, currentAuthor });
return addComment({ ...args, db });
},
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ test("resolving a conflict should throw if the to be resolved with change is not
lix: lix,
conflict: conflict,
newChange: {
author: undefined,
file_id: "mock",
parent_id: null,
plugin_key: "plugin1",
Expand Down

0 comments on commit 73e3ac5

Please sign in to comment.