refactor: 🗃️ Change event log database for note purges

This commit is contained in:
Maxime Cannoodt 2022-08-08 23:39:37 +02:00
parent e7f17dbe4d
commit 293af7f34b
4 changed files with 38 additions and 16 deletions

View File

@ -0,0 +1,24 @@
/*
Warnings:
- You are about to drop the column `purge_count` on the `event` table. All the data in the column will be lost.
*/
-- RedefineTables
PRAGMA foreign_keys=OFF;
CREATE TABLE "new_event" (
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
"time" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"type" TEXT NOT NULL,
"success" BOOLEAN NOT NULL,
"size_bytes" INTEGER,
"note_id" TEXT,
"host" TEXT,
"error" TEXT,
"expire_window_days" INTEGER
);
INSERT INTO "new_event" ("error", "expire_window_days", "host", "id", "size_bytes", "success", "time", "type") SELECT "error", "expire_window_days", "host", "id", "size_bytes", "success", "time", "type" FROM "event";
DROP TABLE "event";
ALTER TABLE "new_event" RENAME TO "event";
PRAGMA foreign_key_check;
PRAGMA foreign_keys=ON;

View File

@ -24,7 +24,7 @@ model event {
type String type String
success Boolean success Boolean
size_bytes Int? size_bytes Int?
purge_count Int? note_id String?
host String? host String?
error String? error String?
expire_window_days Int? expire_window_days Int?

View File

@ -6,6 +6,7 @@ describe("Logging write events", () => {
it("Should write a write event to database", async () => { it("Should write a write event to database", async () => {
const testWriteEvent = { const testWriteEvent = {
host: "localhost", host: "localhost",
note_id: "test_id",
size_bytes: 100, size_bytes: 100,
success: true, success: true,
expire_window_days: 30, expire_window_days: 30,
@ -18,7 +19,7 @@ describe("Logging write events", () => {
// Is event in database? // Is event in database?
const results = await prisma.event.findMany({ const results = await prisma.event.findMany({
where: { type: EventType.WRITE }, where: { type: EventType.WRITE, note_id: testWriteEvent.note_id },
}); });
expect(results.length).toBe(1); expect(results.length).toBe(1);
@ -30,22 +31,19 @@ describe("Logging write events", () => {
it("Should log a read event to database", async () => { it("Should log a read event to database", async () => {
const testReadEvent = { const testReadEvent = {
host: "localhost", host: "localhost",
note_id: "test_id",
size_bytes: 100, size_bytes: 100,
success: true, success: true,
}; };
// Is event written successfully? // Is event written successfully?
const logged = await EventLogger.readEvent({ const logged = await EventLogger.readEvent(testReadEvent);
host: "localhost",
size_bytes: 100,
success: true,
});
expect(logged).not.toBeNull(); expect(logged).not.toBeNull();
expect(logged).toMatchObject(testReadEvent); expect(logged).toMatchObject(testReadEvent);
// Is event in database? // Is event in database?
const results = await prisma.event.findMany({ const results = await prisma.event.findMany({
where: { type: EventType.READ }, where: { type: EventType.READ, note_id: testReadEvent.note_id },
}); });
expect(results.length).toBe(1); expect(results.length).toBe(1);
@ -56,9 +54,9 @@ describe("Logging write events", () => {
it("Should log a purge event to database", async () => { it("Should log a purge event to database", async () => {
const testPurgeEvent = { const testPurgeEvent = {
success: true, note_id: "test_id",
purge_count: 1,
size_bytes: 100, size_bytes: 100,
success: true,
}; };
// Is event written successfully? // Is event written successfully?
@ -68,7 +66,7 @@ describe("Logging write events", () => {
// Is event in database? // Is event in database?
const results = await prisma.event.findMany({ const results = await prisma.event.findMany({
where: { type: EventType.PURGE }, where: { type: EventType.PURGE, note_id: "test_id" },
}); });
expect(results.length).toBe(1); expect(results.length).toBe(1);

View File

@ -14,20 +14,20 @@ interface Event {
interface ClientEvent extends Event { interface ClientEvent extends Event {
host: string; host: string;
size_bytes: number;
success: boolean; success: boolean;
note_id?: string;
size_bytes?: number;
} }
interface WriteEvent extends ClientEvent { interface WriteEvent extends ClientEvent {
expire_window_days: number; expire_window_days?: number;
} }
interface ReadEvent extends ClientEvent {} interface ReadEvent extends ClientEvent {}
interface PurgeEvent extends Event { interface PurgeEvent extends Event {
success: boolean; note_id: string;
purge_count: number; size_bytes?: number;
size_bytes: number;
} }
export default class EventLogger { export default class EventLogger {