mirror of
https://github.com/versia-pub/server.git
synced 2026-01-26 12:16:01 +01:00
refactor(database): ♻️ Clean up database schema
This commit is contained in:
parent
c334cd9cc8
commit
6f97f9f8f1
11
drizzle/migrations/0039_special_serpent_society.sql
Normal file
11
drizzle/migrations/0039_special_serpent_society.sql
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
ALTER TABLE "VersiaObject" DISABLE ROW LEVEL SECURITY;--> statement-breakpoint
|
||||
DROP TABLE "VersiaObject" CASCADE;--> statement-breakpoint
|
||||
ALTER TABLE "Likes" RENAME COLUMN "createdAt" TO "created_at";--> statement-breakpoint
|
||||
ALTER TABLE "Notes" RENAME COLUMN "createdAt" TO "created_at";--> statement-breakpoint
|
||||
ALTER TABLE "Notes" RENAME COLUMN "updatedAt" TO "updated_at";--> statement-breakpoint
|
||||
ALTER TABLE "Notifications" RENAME COLUMN "createdAt" TO "created_at";--> statement-breakpoint
|
||||
ALTER TABLE "Reaction" RENAME COLUMN "update_at" TO "updated_at";--> statement-breakpoint
|
||||
ALTER TABLE "OpenIdAccounts" DROP CONSTRAINT "OpenIdAccounts_userId_Users_id_fk";
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "OpenIdAccounts" ADD CONSTRAINT "OpenIdAccounts_userId_Users_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."Users"("id") ON DELETE cascade ON UPDATE cascade;--> statement-breakpoint
|
||||
ALTER TABLE "Users" ADD CONSTRAINT "Users_uri_unique" UNIQUE("uri");
|
||||
2237
drizzle/migrations/meta/0039_snapshot.json
Normal file
2237
drizzle/migrations/meta/0039_snapshot.json
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -274,6 +274,13 @@
|
|||
"when": 1734548407684,
|
||||
"tag": "0038_friendly_supernaut",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 39,
|
||||
"version": "7",
|
||||
"when": 1734555117380,
|
||||
"tag": "0039_special_serpent_society",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import { relations, sql } from "drizzle-orm";
|
|||
import {
|
||||
type AnyPgColumn,
|
||||
boolean,
|
||||
foreignKey,
|
||||
index,
|
||||
integer,
|
||||
jsonb,
|
||||
|
|
@ -16,8 +15,27 @@ import {
|
|||
uuid,
|
||||
} from "drizzle-orm/pg-core";
|
||||
|
||||
// biome-ignore lint/nursery/useExplicitType: Type is too complex
|
||||
const createdAt = () =>
|
||||
timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull();
|
||||
|
||||
// biome-ignore lint/nursery/useExplicitType: Type is too complex
|
||||
const updatedAt = () =>
|
||||
timestamp("updated_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull();
|
||||
|
||||
// biome-ignore lint/nursery/useExplicitType: Type is too complex
|
||||
const uri = () => text("uri").unique();
|
||||
|
||||
// biome-ignore lint/nursery/useExplicitType: Type is too complex
|
||||
const id = () =>
|
||||
uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull();
|
||||
|
||||
export const Challenges = pgTable("Challenges", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
challenge: jsonb("challenge").notNull().$type<Challenge>(),
|
||||
expiresAt: timestamp("expires_at", {
|
||||
precision: 3,
|
||||
|
|
@ -28,13 +46,11 @@ export const Challenges = pgTable("Challenges", {
|
|||
sql`NOW() + INTERVAL '5 minutes'`,
|
||||
)
|
||||
.notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
export const Emojis = pgTable("Emojis", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
shortcode: text("shortcode").notNull(),
|
||||
url: text("url").notNull(),
|
||||
visibleInPicker: boolean("visible_in_picker").notNull(),
|
||||
|
|
@ -52,8 +68,8 @@ export const Emojis = pgTable("Emojis", {
|
|||
});
|
||||
|
||||
export const Reactions = pgTable("Reaction", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
uri: text("uri").unique(),
|
||||
id: id(),
|
||||
uri: uri(),
|
||||
// Emoji ID is nullable, in which case it is a text emoji, and the emojiText field is used
|
||||
emojiId: uuid("emojiId").references(() => Emojis.id, {
|
||||
onDelete: "cascade",
|
||||
|
|
@ -72,15 +88,8 @@ export const Reactions = pgTable("Reaction", {
|
|||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
}),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
updatedAt: timestamp("update_at", {
|
||||
precision: 3,
|
||||
mode: "string",
|
||||
})
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
updatedAt: updatedAt(),
|
||||
});
|
||||
|
||||
export const ReactionRelations = relations(Reactions, ({ one }) => ({
|
||||
|
|
@ -99,7 +108,7 @@ export const ReactionRelations = relations(Reactions, ({ one }) => ({
|
|||
}));
|
||||
|
||||
export const Filters = pgTable("Filters", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
userId: uuid("userId")
|
||||
.notNull()
|
||||
.references(() => Users.id, {
|
||||
|
|
@ -115,13 +124,11 @@ export const Filters = pgTable("Filters", {
|
|||
title: text("title").notNull(),
|
||||
filterAction: text("filter_action").notNull().$type<"warn" | "hide">(),
|
||||
expireAt: timestamp("expires_at", { precision: 3, mode: "string" }),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
export const FilterKeywords = pgTable("FilterKeywords", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
filterId: uuid("filterId")
|
||||
.notNull()
|
||||
.references(() => Filters.id, {
|
||||
|
|
@ -144,7 +151,7 @@ export const FilterKeywordsRelations = relations(FilterKeywords, ({ one }) => ({
|
|||
}));
|
||||
|
||||
export const Markers = pgTable("Markers", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
noteId: uuid("noteId").references(() => Notes.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
|
|
@ -160,14 +167,12 @@ export const Markers = pgTable("Markers", {
|
|||
})
|
||||
.notNull(),
|
||||
timeline: text("timeline").notNull().$type<"home" | "notifications">(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
export const Likes = pgTable("Likes", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
uri: text("uri").unique(),
|
||||
id: id(),
|
||||
uri: uri(),
|
||||
likerId: uuid("likerId")
|
||||
.notNull()
|
||||
.references(() => Users.id, {
|
||||
|
|
@ -180,41 +185,11 @@ export const Likes = pgTable("Likes", {
|
|||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
}),
|
||||
createdAt: timestamp("createdAt", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
export const VersiaObjects = pgTable(
|
||||
"VersiaObject",
|
||||
{
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
remoteId: text("remote_id").notNull(),
|
||||
type: text("type").notNull(),
|
||||
uri: text("uri").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
authorId: uuid("authorId"),
|
||||
extraData: jsonb("extra_data").notNull(),
|
||||
extensions: jsonb("extensions").notNull(),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
remoteIdKey: uniqueIndex().on(table.remoteId),
|
||||
uriKey: uniqueIndex().on(table.uri),
|
||||
versiaObjectAuthorIdFkey: foreignKey({
|
||||
columns: [table.authorId],
|
||||
foreignColumns: [table.id],
|
||||
})
|
||||
.onUpdate("cascade")
|
||||
.onDelete("cascade"),
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
export const Relationships = pgTable("Relationships", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
ownerId: uuid("ownerId")
|
||||
.notNull()
|
||||
.references(() => Users.id, {
|
||||
|
|
@ -238,21 +213,14 @@ export const Relationships = pgTable("Relationships", {
|
|||
endorsed: boolean("endorsed").notNull(),
|
||||
languages: text("languages").array(),
|
||||
note: text("note").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
updatedAt: timestamp("updated_at", {
|
||||
precision: 3,
|
||||
mode: "string",
|
||||
})
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
updatedAt: updatedAt(),
|
||||
});
|
||||
|
||||
export const Applications = pgTable(
|
||||
"Applications",
|
||||
{
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
name: text("name").notNull(),
|
||||
website: text("website"),
|
||||
vapidKey: text("vapid_key"),
|
||||
|
|
@ -261,11 +229,7 @@ export const Applications = pgTable(
|
|||
scopes: text("scopes").notNull(),
|
||||
redirectUri: text("redirect_uri").notNull(),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
clientIdKey: uniqueIndex().on(table.clientId),
|
||||
};
|
||||
},
|
||||
(table) => [uniqueIndex().on(table.clientId)],
|
||||
);
|
||||
|
||||
export const ApplicationsRelations = relations(Applications, ({ many }) => ({
|
||||
|
|
@ -274,15 +238,13 @@ export const ApplicationsRelations = relations(Applications, ({ many }) => ({
|
|||
}));
|
||||
|
||||
export const Tokens = pgTable("Tokens", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
tokenType: text("token_type").notNull(),
|
||||
scope: text("scope").notNull(),
|
||||
accessToken: text("access_token").notNull(),
|
||||
code: text("code"),
|
||||
expiresAt: timestamp("expires_at", { precision: 3, mode: "string" }),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
clientId: text("client_id").notNull().default(""),
|
||||
redirectUri: text("redirect_uri").notNull().default(""),
|
||||
idToken: text("id_token"),
|
||||
|
|
@ -299,7 +261,7 @@ export const Tokens = pgTable("Tokens", {
|
|||
});
|
||||
|
||||
export const Attachments = pgTable("Attachments", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
url: text("url").notNull(),
|
||||
remoteUrl: text("remote_url"),
|
||||
thumbnailUrl: text("thumbnail_url"),
|
||||
|
|
@ -319,11 +281,9 @@ export const Attachments = pgTable("Attachments", {
|
|||
});
|
||||
|
||||
export const Notifications = pgTable("Notifications", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
type: text("type").notNull(),
|
||||
createdAt: timestamp("createdAt", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
notifiedId: uuid("notifiedId")
|
||||
.notNull()
|
||||
.references(() => Users.id, {
|
||||
|
|
@ -344,23 +304,16 @@ export const Notifications = pgTable("Notifications", {
|
|||
});
|
||||
|
||||
export const Notes = pgTable("Notes", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
uri: text("uri").unique(),
|
||||
id: id(),
|
||||
uri: uri(),
|
||||
authorId: uuid("authorId")
|
||||
.notNull()
|
||||
.references(() => Users.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
}),
|
||||
createdAt: timestamp("createdAt", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
updatedAt: timestamp("updatedAt", {
|
||||
precision: 3,
|
||||
mode: "string",
|
||||
})
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
updatedAt: updatedAt(),
|
||||
reblogId: uuid("reblogId").references((): AnyPgColumn => Notes.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
|
|
@ -386,7 +339,7 @@ export const Notes = pgTable("Notes", {
|
|||
});
|
||||
|
||||
export const Instances = pgTable("Instances", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
baseUrl: text("base_url").notNull(),
|
||||
name: text("name").notNull(),
|
||||
version: text("version").notNull(),
|
||||
|
|
@ -404,9 +357,9 @@ export const Instances = pgTable("Instances", {
|
|||
});
|
||||
|
||||
export const OpenIdAccounts = pgTable("OpenIdAccounts", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
userId: uuid("userId").references(() => Users.id, {
|
||||
onDelete: "set null",
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
}),
|
||||
serverId: text("server_id").notNull(),
|
||||
|
|
@ -416,8 +369,8 @@ export const OpenIdAccounts = pgTable("OpenIdAccounts", {
|
|||
export const Users = pgTable(
|
||||
"Users",
|
||||
{
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
uri: text("uri"),
|
||||
id: id(),
|
||||
uri: uri(),
|
||||
username: text("username").notNull(),
|
||||
displayName: text("display_name").notNull(),
|
||||
password: text("password"),
|
||||
|
|
@ -453,15 +406,8 @@ export const Users = pgTable(
|
|||
>(),
|
||||
avatar: text("avatar").notNull(),
|
||||
header: text("header").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
updatedAt: timestamp("updated_at", {
|
||||
precision: 3,
|
||||
mode: "string",
|
||||
})
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
updatedAt: updatedAt(),
|
||||
isBot: boolean("is_bot").default(false).notNull(),
|
||||
isLocked: boolean("is_locked").default(false).notNull(),
|
||||
isDiscoverable: boolean("is_discoverable").default(false).notNull(),
|
||||
|
|
@ -476,17 +422,15 @@ export const Users = pgTable(
|
|||
.default(false)
|
||||
.notNull(),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
uriKey: uniqueIndex().on(table.uri),
|
||||
usernameKey: index().on(table.username),
|
||||
emailKey: uniqueIndex().on(table.email),
|
||||
};
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex().on(table.uri),
|
||||
index().on(table.username),
|
||||
uniqueIndex().on(table.email),
|
||||
],
|
||||
);
|
||||
|
||||
export const OpenIdLoginFlows = pgTable("OpenIdLoginFlows", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
codeVerifier: text("code_verifier").notNull(),
|
||||
applicationId: uuid("applicationId").references(() => Applications.id, {
|
||||
onDelete: "cascade",
|
||||
|
|
@ -506,11 +450,9 @@ export const OpenIdLoginFlowsRelations = relations(
|
|||
);
|
||||
|
||||
export const Flags = pgTable("Flags", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
flagType: text("flag_type").default("other").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
noteId: uuid("noteId").references(() => Notes.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
|
|
@ -522,7 +464,7 @@ export const Flags = pgTable("Flags", {
|
|||
});
|
||||
|
||||
export const ModNotes = pgTable("ModNotes", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
nodeId: uuid("noteId").references(() => Notes.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
|
|
@ -538,13 +480,11 @@ export const ModNotes = pgTable("ModNotes", {
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
note: text("note").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
export const ModTags = pgTable("ModTags", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
noteId: uuid("noteId").references(() => Notes.id, {
|
||||
onDelete: "cascade",
|
||||
onUpdate: "cascade",
|
||||
|
|
@ -560,9 +500,7 @@ export const ModTags = pgTable("ModTags", {
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
tag: text("tag").notNull(),
|
||||
createdAt: timestamp("created_at", { precision: 3, mode: "string" })
|
||||
.defaultNow()
|
||||
.notNull(),
|
||||
createdAt: createdAt(),
|
||||
});
|
||||
|
||||
/**
|
||||
|
|
@ -669,7 +607,7 @@ export const ADMIN_ROLES = [
|
|||
];
|
||||
|
||||
export const Roles = pgTable("Roles", {
|
||||
id: uuid("id").default(sql`uuid_generate_v7()`).primaryKey().notNull(),
|
||||
id: id(),
|
||||
name: text("name").notNull(),
|
||||
permissions: text("permissions")
|
||||
.array()
|
||||
|
|
@ -727,12 +665,10 @@ export const EmojiToUser = pgTable(
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
abUnique: uniqueIndex().on(table.emojiId, table.userId),
|
||||
bIdx: index().on(table.userId),
|
||||
};
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex().on(table.emojiId, table.userId),
|
||||
index().on(table.userId),
|
||||
],
|
||||
);
|
||||
|
||||
export const EmojiToUserRelations = relations(EmojiToUser, ({ one }) => ({
|
||||
|
|
@ -762,12 +698,10 @@ export const EmojiToNote = pgTable(
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
abUnique: uniqueIndex().on(table.emojiId, table.noteId),
|
||||
bIdx: index().on(table.noteId),
|
||||
};
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex().on(table.emojiId, table.noteId),
|
||||
index().on(table.noteId),
|
||||
],
|
||||
);
|
||||
|
||||
export const NoteToMentions = pgTable(
|
||||
|
|
@ -786,12 +720,10 @@ export const NoteToMentions = pgTable(
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
abUnique: uniqueIndex().on(table.noteId, table.userId),
|
||||
bIdx: index().on(table.userId),
|
||||
};
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex().on(table.noteId, table.userId),
|
||||
index().on(table.userId),
|
||||
],
|
||||
);
|
||||
|
||||
export const UserToPinnedNotes = pgTable(
|
||||
|
|
@ -810,12 +742,10 @@ export const UserToPinnedNotes = pgTable(
|
|||
onUpdate: "cascade",
|
||||
}),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
abUnique: uniqueIndex().on(table.userId, table.noteId),
|
||||
bIdx: index().on(table.noteId),
|
||||
};
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex().on(table.userId, table.noteId),
|
||||
index().on(table.noteId),
|
||||
],
|
||||
);
|
||||
|
||||
export const AttachmentsRelations = relations(Attachments, ({ one }) => ({
|
||||
|
|
|
|||
Loading…
Reference in a new issue