2025-08-02 20:13:29 +02:00
|
|
|
import {pgTable, text, boolean, timestamp, uuid, integer, pgEnum, uniqueIndex} from "drizzle-orm/pg-core";
|
|
|
|
|
import {Agent, agent} from "./07_agent";
|
|
|
|
|
import {Project, project} from "./05_project";
|
|
|
|
|
import {relations} from "drizzle-orm";
|
|
|
|
|
import {dbmsEnum, statusEnum} from "./types";
|
|
|
|
|
import {createSelectSchema} from "drizzle-zod";
|
|
|
|
|
import {z} from "zod";
|
2025-05-16 15:54:17 +02:00
|
|
|
|
|
|
|
|
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()
|
2025-08-02 20:13:29 +02:00
|
|
|
.references(() => agent.id, {onDelete: "cascade"}),
|
2025-05-16 15:54:17 +02:00
|
|
|
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()
|
2025-08-02 20:13:29 +02:00
|
|
|
.references(() => database.id, {onDelete: "cascade"}),
|
2025-05-16 15:54:17 +02:00
|
|
|
},
|
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()
|
2025-08-02 20:13:29 +02:00
|
|
|
.references(() => backup.id, {onDelete: "cascade"}),
|
|
|
|
|
databaseId: uuid("database_id").references(() => database.id, {onDelete: "cascade"}),
|
2025-05-16 15:54:17 +02:00
|
|
|
});
|
|
|
|
|
|
2025-08-02 20:13:29 +02:00
|
|
|
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]}),
|
2025-05-16 15:54:17 +02:00
|
|
|
backups: many(backup),
|
|
|
|
|
restorations: many(restoration),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-08-02 20:13:29 +02:00
|
|
|
export const backupRelations = relations(backup, ({one, many}) => ({
|
|
|
|
|
database: one(database, {fields: [backup.databaseId], references: [database.id]}),
|
2025-05-16 15:54:17 +02:00
|
|
|
restorations: many(restoration),
|
|
|
|
|
}));
|
|
|
|
|
|
2025-08-02 20:13:29 +02:00
|
|
|
export const restorationRelations = relations(restoration, ({one}) => ({
|
|
|
|
|
backup: one(backup, {fields: [restoration.backupId], references: [backup.id]}),
|
|
|
|
|
database: one(database, {fields: [restoration.databaseId], references: [database.id]}),
|
2025-05-16 15:54:17 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
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 & {
|
2025-08-02 20:13:29 +02:00
|
|
|
agent?: Agent | null;
|
|
|
|
|
project?: Project | null;
|
|
|
|
|
backups?: Backup[] | null;
|
|
|
|
|
restorations?: Restoration[] | null;
|
2025-05-16 15:54:17 +02:00
|
|
|
};
|
2025-08-02 20:13:29 +02:00
|
|
|
|