refactor(database): ♻️ Cleanup Notes database declaration

This commit is contained in:
Jesse Wierzbinski 2024-04-26 11:19:01 -10:00
parent a45a4b3652
commit db35ba6f93
No known key found for this signature in database
4 changed files with 2207 additions and 208 deletions

View file

@ -0,0 +1,10 @@
ALTER TABLE "Notes" DROP CONSTRAINT "Notes_replyId_Notes_id_fk";
--> statement-breakpoint
DROP INDEX IF EXISTS "Notes_uri_index";--> statement-breakpoint
DO $$ BEGIN
ALTER TABLE "Notes" ADD CONSTRAINT "Notes_replyId_Notes_id_fk" FOREIGN KEY ("replyId") REFERENCES "Notes"("id") ON DELETE cascade ON UPDATE cascade;
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
ALTER TABLE "Notes" ADD CONSTRAINT "Notes_uri_unique" UNIQUE("uri");

File diff suppressed because it is too large Load diff

View file

@ -148,6 +148,13 @@
"when": 1714017186457, "when": 1714017186457,
"tag": "0020_giant_the_stranger", "tag": "0020_giant_the_stranger",
"breakpoints": true "breakpoints": true
},
{
"idx": 21,
"version": "5",
"when": 1714165180389,
"tag": "0021_wise_stephen_strange",
"breakpoints": true
} }
] ]
} }

View file

@ -10,6 +10,7 @@ import {
timestamp, timestamp,
uniqueIndex, uniqueIndex,
uuid, uuid,
type AnyPgColumn,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import type * as Lysand from "lysand-types"; import type * as Lysand from "lysand-types";
import type { Source as APISource } from "~types/mastodon/source"; import type { Source as APISource } from "~types/mastodon/source";
@ -272,11 +273,9 @@ export const Notifications = pgTable("Notifications", {
dismissed: boolean("dismissed").default(false).notNull(), dismissed: boolean("dismissed").default(false).notNull(),
}); });
export const Notes = pgTable( export const Notes = pgTable("Notes", {
"Notes",
{
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(), id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
uri: text("uri"), uri: text("uri").unique(),
authorId: uuid("authorId") authorId: uuid("authorId")
.notNull() .notNull()
.references(() => Users.id, { .references(() => Users.id, {
@ -292,12 +291,21 @@ export const Notes = pgTable(
}) })
.defaultNow() .defaultNow()
.notNull(), .notNull(),
reblogId: uuid("reblogId"), reblogId: uuid("reblogId").references((): AnyPgColumn => Notes.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
content: text("content").default("").notNull(), content: text("content").default("").notNull(),
contentType: text("content_type").default("text/plain").notNull(), contentType: text("content_type").default("text/plain").notNull(),
visibility: text("visibility").notNull(), visibility: text("visibility").notNull(),
replyId: uuid("replyId"), replyId: uuid("replyId").references((): AnyPgColumn => Notes.id, {
quotingId: uuid("quoteId"), onDelete: "cascade",
onUpdate: "cascade",
}),
quotingId: uuid("quoteId").references((): AnyPgColumn => Notes.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
sensitive: boolean("sensitive").notNull(), sensitive: boolean("sensitive").notNull(),
spoilerText: text("spoiler_text").default("").notNull(), spoilerText: text("spoiler_text").default("").notNull(),
applicationId: uuid("applicationId").references(() => Applications.id, { applicationId: uuid("applicationId").references(() => Applications.id, {
@ -305,31 +313,7 @@ export const Notes = pgTable(
onUpdate: "cascade", onUpdate: "cascade",
}), }),
contentSource: text("content_source").default("").notNull(), contentSource: text("content_source").default("").notNull(),
}, });
(table) => {
return {
uriKey: uniqueIndex().on(table.uri),
noteReblogIdFkey: foreignKey({
columns: [table.reblogId],
foreignColumns: [table.id],
})
.onUpdate("cascade")
.onDelete("cascade"),
noteReplyIdFkey: foreignKey({
columns: [table.replyId],
foreignColumns: [table.id],
})
.onUpdate("cascade")
.onDelete("set null"),
noteQuotingIdFkey: foreignKey({
columns: [table.quotingId],
foreignColumns: [table.id],
})
.onUpdate("cascade")
.onDelete("set null"),
};
},
);
export const Instances = pgTable("Instances", { export const Instances = pgTable("Instances", {
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(), id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),