Files
portabase/src/db/schema/06_database.ts
T

87 lines
3.4 KiB
TypeScript
Raw Normal View History

2025-05-16 15:54:17 +02:00
import { pgTable, text, boolean, timestamp, uuid, integer, pgEnum, uniqueIndex } from "drizzle-orm/pg-core";
2025-07-16 12:36:11 +02:00
import { Agent, agent } from "./07_agent";
import { Project, project } from "./05_project";
2025-05-16 15:54:17 +02:00
import { relations } from "drizzle-orm";
import { dbmsEnum, statusEnum } from "./types";
import { createSelectSchema } from "drizzle-zod";
import { z } from "zod";
export const database = pgTable("databases", {
id: uuid("id").primaryKey().defaultRandom(),
agentDatabaseId: uuid("agent_database_id").notNull().defaultRandom(),
name: text("name").notNull(),
dbms: dbmsEnum("dbms").notNull(),
2025-07-16 18:15:28 +02:00
description: text("description"),
2025-05-16 15:54:17 +02:00
backupPolicy: text("backup_policy"),
isWaitingForBackup: boolean("is_waiting_for_backup").default(false).notNull(),
backupToRestore: text("backup_to_restore"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at"),
agentId: uuid("agent_id")
.notNull()
.references(() => agent.id, { onDelete: "cascade" }),
lastContact: timestamp("last_contact"),
projectId: uuid("project_id")
2025-07-16 18:15:28 +02:00
.references(() => project.id),
2025-05-16 15:54:17 +02:00
});
export const backup = pgTable(
"backups",
{
id: uuid("id").primaryKey().defaultRandom(),
status: statusEnum("status").default("waiting").notNull(),
file: text("file"),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at"),
databaseId: uuid("database_id")
.notNull()
.references(() => database.id, { onDelete: "cascade" }),
},
2025-07-16 20:16:18 +02:00
// (table) => [uniqueIndex("database_id_status_unique").on(table.databaseId, table.status)]
2025-05-16 15:54:17 +02:00
);
export const restoration = pgTable("restorations", {
id: uuid("id").primaryKey().defaultRandom(),
status: statusEnum("status").default("waiting").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at"),
backupId: uuid("backup_id")
.notNull()
.references(() => backup.id, { onDelete: "cascade" }),
databaseId: uuid("database_id").references(() => database.id, { onDelete: "cascade" }),
});
export const databaseRelations = relations(database, ({ one, many }) => ({
agent: one(agent, { fields: [database.agentId], references: [agent.id] }),
project: one(project, { fields: [database.projectId], references: [project.id] }),
backups: many(backup),
restorations: many(restoration),
}));
export const backupRelations = relations(backup, ({ one, many }) => ({
database: one(database, { fields: [backup.databaseId], references: [database.id] }),
restorations: many(restoration),
}));
export const restorationRelations = relations(restoration, ({ one }) => ({
backup: one(backup, { fields: [restoration.backupId], references: [backup.id] }),
database: one(database, { fields: [restoration.databaseId], references: [database.id] }),
}));
export const databaseSchema = createSelectSchema(database);
export type Database = z.infer<typeof databaseSchema>;
export const backupSchema = createSelectSchema(backup);
export type Backup = z.infer<typeof backupSchema>;
export const restorationSchema = createSelectSchema(restoration);
export type Restoration = z.infer<typeof restorationSchema>;
export type DatabaseWith = Database & {
agent: Agent;
project: Project;
backups: Backup[];
restorations: Restoration[];
};