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: generalized discussions #3192

Merged
merged 1 commit into from
Oct 27, 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
14 changes: 9 additions & 5 deletions packages/lix-sdk/src/database/applySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export async function applySchema(args: { sqlite: SqliteDatabase }) {
FOREIGN KEY(change_id) REFERENCES change(id)
) strict;

-- tags on change sets
CREATE TABLE IF NOT EXISTS change_set_tag (
change_set_id TEXT NOT NULL,
tag_id TEXT NOT NULL,
Expand All @@ -107,14 +106,19 @@ export async function applySchema(args: { sqlite: SqliteDatabase }) {
FOREIGN KEY(tag_id) REFERENCES tag(id)
) strict;

CREATE TABLE IF NOT EXISTS change_set_discussion (
change_set_id TEXT NOT NULL,
discussion_id TEXT NOT NULL,

UNIQUE(change_set_id, discussion_id),
FOREIGN KEY(change_set_id) REFERENCES change_set(id),
FOREIGN KEY(discussion_id) REFERENCES discussion(id)
) strict;

-- discussions

CREATE TABLE IF NOT EXISTS discussion (
id TEXT PRIMARY KEY DEFAULT (uuid_v4()),
change_set_id TEXT NOT NULL,

FOREIGN KEY(change_set_id) REFERENCES change_set(id)
id TEXT PRIMARY KEY DEFAULT (uuid_v4())
) strict;

CREATE TABLE IF NOT EXISTS comment (
Expand Down
19 changes: 9 additions & 10 deletions packages/lix-sdk/src/database/initDb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,21 +176,20 @@ test("creating multiple discussions for one change set should be possible", asyn

await db
.insertInto("discussion")
.values([{ id: "discussion-1" }, { id: "discussion-2" }])
.returningAll()
.execute();

await db
.insertInto("change_set_discussion")
.values([
{
id: "discussion-1",
change_set_id: changeSet.id,
},
{
id: "discussion-2",
change_set_id: changeSet.id,
},
{ change_set_id: changeSet.id, discussion_id: "discussion-1" },
{ change_set_id: changeSet.id, discussion_id: "discussion-2" },
])
.returningAll()
.execute();

const discussions = await db
.selectFrom("discussion")
.selectFrom("change_set_discussion")
.selectAll()
.where("change_set_id", "=", changeSet.id)
.execute();
Expand Down
10 changes: 9 additions & 1 deletion packages/lix-sdk/src/database/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type LixDatabaseSchema = {
change_set: ChangeSetTable;
change_set_item: ChangeSetItemTable;
change_set_tag: ChangeSetTagTable;
change_set_discussion: ChangeSetDiscussionTable;

// discussion
discussion: DiscussionTable;
Expand Down Expand Up @@ -145,7 +146,6 @@ export type NewDiscussion = Insertable<DiscussionTable>;
export type DiscussionUpdate = Updateable<DiscussionTable>;
type DiscussionTable = {
id: Generated<string>;
change_set_id: string;
};

export type Comment = Selectable<CommentTable>;
Expand All @@ -159,6 +159,14 @@ type CommentTable = {
body: string;
};

export type ChangeSetDiscussion = Selectable<ChangeSetDiscussionTable>;
export type NewChangeSetDiscussion = Insertable<ChangeSetDiscussionTable>;
export type ChangeSetDiscussionUpdate = Updateable<ChangeSetDiscussionTable>;
type ChangeSetDiscussionTable = {
change_set_id: string;
discussion_id: string;
};

// ----- tags -----

export type Tag = Selectable<TagTable>;
Expand Down
4 changes: 2 additions & 2 deletions packages/lix-sdk/src/discussion/create-discussion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test("should be able to start a discussion on changes", async () => {
});

const discussions = await lix.db
.selectFrom("discussion")
.selectFrom("change_set_discussion")
.selectAll()
.execute();

Expand All @@ -57,7 +57,7 @@ test("should be able to start a discussion on changes", async () => {
const comments = await lix.db
.selectFrom("comment")
.selectAll()
.where("discussion_id", "=", discussions[0]!.id)
.where("discussion_id", "=", discussions[0]!.discussion_id)
.execute();

expect(comments).toHaveLength(1);
Expand Down
10 changes: 8 additions & 2 deletions packages/lix-sdk/src/discussion/create-discussion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ export async function createDiscussion(args: {
return args.lix.db.transaction().execute(async (trx) => {
const discussion = await trx
.insertInto("discussion")
.defaultValues()
.returningAll()
.executeTakeFirstOrThrow();

await trx
.insertInto("change_set_discussion")
.values({
change_set_id: args.changeSet.id,
discussion_id: discussion.id,
})
.returningAll()
.executeTakeFirstOrThrow();
.execute();

await trx
.insertInto("comment")
Expand Down
Loading