refactor(database): ♻️ Move Note <-> Media relations to a many-to-many model instead of one-to-many

This commit is contained in:
Jesse Wierzbinski 2025-01-23 20:36:09 +01:00
parent 9c30dacda7
commit 3216fc339a
No known key found for this signature in database
7 changed files with 2428 additions and 31 deletions

View file

@ -0,0 +1,26 @@
CREATE TABLE "MediasToNote" (
"mediaId" uuid NOT NULL,
"noteId" uuid NOT NULL
);
--> statement-breakpoint
ALTER TABLE "Medias" DROP CONSTRAINT "Medias_noteId_Notes_id_fk";
--> statement-breakpoint
ALTER TABLE "Medias" ADD COLUMN "content" jsonb NOT NULL;--> statement-breakpoint
ALTER TABLE "Medias" ADD COLUMN "original_content" jsonb;--> statement-breakpoint
ALTER TABLE "Medias" ADD COLUMN "thumbnail" jsonb;--> statement-breakpoint
ALTER TABLE "MediasToNote" ADD CONSTRAINT "MediasToNote_mediaId_Medias_id_fk" FOREIGN KEY ("mediaId") REFERENCES "public"."Medias"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
ALTER TABLE "MediasToNote" ADD CONSTRAINT "MediasToNote_noteId_Notes_id_fk" FOREIGN KEY ("noteId") REFERENCES "public"."Notes"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
CREATE INDEX "MediasToNote_mediaId_index" ON "MediasToNote" USING btree ("mediaId");--> statement-breakpoint
CREATE INDEX "MediasToNote_noteId_index" ON "MediasToNote" USING btree ("noteId");--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "url";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "remote_url";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "thumbnail_url";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "mime_type";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "description";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "sha256";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "fps";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "duration";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "width";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "height";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "size";--> statement-breakpoint
ALTER TABLE "Medias" DROP COLUMN "noteId";

File diff suppressed because it is too large Load diff

View file

@ -295,6 +295,13 @@
"when": 1737644734501,
"tag": "0041_bright_doctor_spectrum",
"breakpoints": true
},
{
"idx": 42,
"version": "7",
"when": 1737660317024,
"tag": "0042_swift_the_phantom",
"breakpoints": true
}
]
}

View file

@ -309,10 +309,6 @@ export const Medias = pgTable("Medias", {
originalContent: jsonb("original_content").$type<ContentFormat>(),
thumbnail: jsonb("thumbnail").$type<ContentFormat>(),
blurhash: text("blurhash"),
noteId: uuid("noteId").references(() => Notes.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
});
export const Notifications = pgTable("Notifications", {
@ -791,10 +787,38 @@ export const UserToPinnedNotes = pgTable(
],
);
export const AttachmentsRelations = relations(Medias, ({ one }) => ({
notes: one(Notes, {
fields: [Medias.noteId],
export const MediasRelations = relations(Medias, ({ many }) => ({
notes: many(Notes),
}));
export const MediasToNotes = pgTable(
"MediasToNote",
{
mediaId: uuid("mediaId")
.notNull()
.references(() => Medias.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
noteId: uuid("noteId")
.notNull()
.references(() => Notes.id, {
onDelete: "cascade",
onUpdate: "cascade",
}),
},
(table) => [index().on(table.mediaId), index().on(table.noteId)],
);
export const MediasToNotesRelations = relations(MediasToNotes, ({ one }) => ({
media: one(Medias, {
fields: [MediasToNotes.mediaId],
references: [Medias.id],
}),
note: one(Notes, {
fields: [MediasToNotes.noteId],
references: [Notes.id],
relationName: "AttachmentToNote",
}),
}));
@ -886,7 +910,9 @@ export const NotesRelations = relations(Notes, ({ many, one }) => ({
references: [Users.id],
relationName: "NoteToAuthor",
}),
attachments: many(Medias),
attachments: many(MediasToNotes, {
relationName: "AttachmentToNote",
}),
mentions: many(NoteToMentions),
reblog: one(Notes, {
fields: [Notes.reblogId],