From 5eee7c9ac473da313d10a153a66adabbf5ba831d Mon Sep 17 00:00:00 2001 From: Andreas Knuth Date: Fri, 7 Feb 2025 17:23:26 -0600 Subject: [PATCH] Umstellung auf Date --- api/src/app/drizzle.service.ts | 27 +++++++-------- api/src/db/schema.ts | 7 ++-- ...rs.sql => 0000_known_stepford_cuckoos.sql} | 16 +++++---- drizzle/meta/0000_snapshot.json | 34 +++++++++++++------ drizzle/meta/_journal.json | 4 +-- 5 files changed, 51 insertions(+), 37 deletions(-) rename drizzle/{0000_dapper_the_watchers.sql => 0000_known_stepford_cuckoos.sql} (64%) diff --git a/api/src/app/drizzle.service.ts b/api/src/app/drizzle.service.ts index e091e87..a9c0978 100644 --- a/api/src/app/drizzle.service.ts +++ b/api/src/app/drizzle.service.ts @@ -18,7 +18,9 @@ export class DrizzleService { // }, // }, // }); - this.db = drizzle('postgresql://haiky:xieng7Seih@localhost:15432/haiky', { + + this.db = drizzle(process.env['DATABASE_URL']!, { + //this.db = drizzle('postgresql://haiky:xieng7Seih@localhost:15432/haiky', { logger: { logQuery: (query: string, params: any[]) => { this.sqlLogger.logQuery(query, params); @@ -91,7 +93,7 @@ export class DrizzleService { .update(deck) .set({ deckname: newDeckname, - updated: sql`CURRENT_TIMESTAMP`, // Setze 'updated' auf CURRENT_TIMESTAMP + updated: new Date(), // Setze 'updated' auf CURRENT_TIMESTAMP }) .where(and(eq(deck.deckname, oldDeckname), eq(deck.user, user.email))); return { status: 'success', message: 'Deck renamed successfully' }; @@ -152,7 +154,7 @@ export class DrizzleService { x2: box.x2, y1: box.y1, y2: box.y2, - updated: sql`CURRENT_TIMESTAMP`, // Setze 'updated' auf CURRENT_TIMESTAMP + updated: new Date(), // Setze 'updated' auf CURRENT_TIMESTAMP }) .where(and(eq(deck.user, user.email), eq(deck.bildid, data.bildid), eq(deck.deckname, data.deckname), eq(deck.id, box.id!!))); if (result.rowsAffected === 0) { @@ -185,8 +187,7 @@ export class DrizzleService { const affectedDecks = await this.db .select({ deckname: deck.deckname }) .from(deck) - .where(and(eq(deck.bildid, bildid), eq(deck.user, user.email))) - .all(); + .where(and(eq(deck.bildid, bildid), eq(deck.user, user.email))); if (affectedDecks.length === 0) { throw new HttpException('No entries found for the given image ID', HttpStatus.NOT_FOUND); @@ -207,8 +208,7 @@ export class DrizzleService { const existingImages = await this.db .select() .from(deck) - .where(and(eq(deck.bildid, bildid), eq(deck.user, user.email))) - .all(); + .where(and(eq(deck.bildid, bildid), eq(deck.user, user.email))); if (existingImages.length === 0) { throw new HttpException('No entries found for the given image ID', HttpStatus.NOT_FOUND); @@ -218,7 +218,7 @@ export class DrizzleService { .update(deck) .set({ deckname: targetDeckId, - updated: sql`CURRENT_TIMESTAMP`, // Setze 'updated' auf CURRENT_TIMESTAMP + updated: new Date(), // Setze 'updated' auf CURRENT_TIMESTAMP }) .where(and(eq(deck.bildid, bildid), eq(deck.user, user.email))); @@ -236,16 +236,11 @@ export class DrizzleService { throw new HttpException('Deck not found', HttpStatus.NOT_FOUND); } - // const existingNewDeck = await this.getDeckByName(newDeckname, user); - // if (existingNewDeck.length > 0) { - // throw new HttpException('Deck with the new name already exists', HttpStatus.CONFLICT); - // } - await this.db .update(deck) .set({ bildname: newImagename, - updated: sql`CURRENT_TIMESTAMP`, // Setze 'updated' auf CURRENT_TIMESTAMP + updated: new Date(), // Setze 'updated' auf CURRENT_TIMESTAMP }) .where(and(eq(deck.bildid, bildId), eq(deck.user, user.email))); return { status: 'success', message: 'Image Entries renamed successfully' }; @@ -262,6 +257,7 @@ export class DrizzleService { reps?: number; lapses?: number; isGraduated?: boolean; + inserted?: string; }, user: User, ) { @@ -269,7 +265,8 @@ export class DrizzleService { if (typeof data.isGraduated === 'boolean') { updateData.isGraduated = Number(data.isGraduated); } - updateData.updated = sql`CURRENT_TIMESTAMP`; // Setze 'updated' auf CURRENT_TIMESTAMP + updateData.updated = new Date(); //sql`CURRENT_TIMESTAMP`; // Setze 'updated' auf CURRENT_TIMESTAMP + updateData.inserted = new Date(data.inserted as string); const result = await this.db .update(deck) diff --git a/api/src/db/schema.ts b/api/src/db/schema.ts index ec8f615..6d04d8b 100644 --- a/api/src/db/schema.ts +++ b/api/src/db/schema.ts @@ -1,4 +1,3 @@ -import { sql } from 'drizzle-orm'; import * as t from 'drizzle-orm/pg-core'; import { pgEnum, pgTable as table } from 'drizzle-orm/pg-core'; @@ -22,8 +21,8 @@ export const deck = table( lapses: t.integer('lapses'), isGraduated: t.integer('isgraduated'), user: t.varchar('user').notNull(), - inserted: t.varchar().default(sql`(CURRENT_TIMESTAMP)`), // Neue Spalte - updated: t.varchar().default(sql`(CURRENT_TIMESTAMP)`), // Neue Spalte + inserted: t.timestamp('inserted', { mode: 'date' }).defaultNow(), + updated: t.timestamp('updated', { mode: 'date' }).defaultNow(), }, table => [t.uniqueIndex('deck_idx').on(table.id)], ); @@ -35,6 +34,8 @@ export const users = table( email: t.varchar().notNull(), role: rolesEnum().default('guest'), sign_in_provider: t.varchar('sign_in_provider', { length: 50 }), + inserted: t.timestamp('inserted', { mode: 'date' }).defaultNow(), + updated: t.timestamp('updated', { mode: 'date' }).defaultNow(), }, table => [t.uniqueIndex('users_idx').on(table.id)], ); diff --git a/drizzle/0000_dapper_the_watchers.sql b/drizzle/0000_known_stepford_cuckoos.sql similarity index 64% rename from drizzle/0000_dapper_the_watchers.sql rename to drizzle/0000_known_stepford_cuckoos.sql index 3907cc7..5544a5f 100644 --- a/drizzle/0000_dapper_the_watchers.sql +++ b/drizzle/0000_known_stepford_cuckoos.sql @@ -1,6 +1,6 @@ CREATE TYPE "public"."roles" AS ENUM('admin', 'guest', 'pro');--> statement-breakpoint -CREATE TABLE "Deck" ( - "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "Deck_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), +CREATE TABLE "deck" ( + "id" integer PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "deck_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 2147483647 START WITH 1 CACHE 1), "deckname" varchar NOT NULL, "bildname" varchar, "bildid" varchar, @@ -13,10 +13,10 @@ CREATE TABLE "Deck" ( "factor" real, "reps" integer, "lapses" integer, - "isGraduated" integer, + "isgraduated" integer, "user" varchar NOT NULL, - "inserted" varchar DEFAULT (CURRENT_TIMESTAMP), - "updated" varchar DEFAULT (CURRENT_TIMESTAMP) + "inserted" timestamp DEFAULT now(), + "updated" timestamp DEFAULT now() ); --> statement-breakpoint CREATE TABLE "users" ( @@ -24,8 +24,10 @@ CREATE TABLE "users" ( "name" varchar(256), "email" varchar NOT NULL, "role" "roles" DEFAULT 'guest', - "sign_in_provider" varchar(50) + "sign_in_provider" varchar(50), + "inserted" timestamp DEFAULT now(), + "updated" timestamp DEFAULT now() ); --> statement-breakpoint -CREATE UNIQUE INDEX "deck_idx" ON "Deck" USING btree ("id");--> statement-breakpoint +CREATE UNIQUE INDEX "deck_idx" ON "deck" USING btree ("id");--> statement-breakpoint CREATE UNIQUE INDEX "users_idx" ON "users" USING btree ("id"); \ No newline at end of file diff --git a/drizzle/meta/0000_snapshot.json b/drizzle/meta/0000_snapshot.json index 3988ff1..f099ef2 100644 --- a/drizzle/meta/0000_snapshot.json +++ b/drizzle/meta/0000_snapshot.json @@ -1,11 +1,11 @@ { - "id": "ad8762e3-d7ad-4605-b2fc-b10a0de0f08a", + "id": "5c983d57-b2f3-4fb4-b99e-066574c486e3", "prevId": "00000000-0000-0000-0000-000000000000", "version": "7", "dialect": "postgresql", "tables": { - "public.Deck": { - "name": "Deck", + "public.deck": { + "name": "deck", "schema": "", "columns": { "id": { @@ -15,7 +15,7 @@ "notNull": true, "identity": { "type": "always", - "name": "Deck_id_seq", + "name": "deck_id_seq", "schema": "public", "increment": "1", "startWith": "1", @@ -97,8 +97,8 @@ "primaryKey": false, "notNull": false }, - "isGraduated": { - "name": "isGraduated", + "isgraduated": { + "name": "isgraduated", "type": "integer", "primaryKey": false, "notNull": false @@ -111,17 +111,17 @@ }, "inserted": { "name": "inserted", - "type": "varchar", + "type": "timestamp", "primaryKey": false, "notNull": false, - "default": "(CURRENT_TIMESTAMP)" + "default": "now()" }, "updated": { "name": "updated", - "type": "varchar", + "type": "timestamp", "primaryKey": false, "notNull": false, - "default": "(CURRENT_TIMESTAMP)" + "default": "now()" } }, "indexes": { @@ -194,6 +194,20 @@ "type": "varchar(50)", "primaryKey": false, "notNull": false + }, + "inserted": { + "name": "inserted", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" + }, + "updated": { + "name": "updated", + "type": "timestamp", + "primaryKey": false, + "notNull": false, + "default": "now()" } }, "indexes": { diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index b16cde9..a603d2a 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -5,8 +5,8 @@ { "idx": 0, "version": "7", - "when": 1738881267759, - "tag": "0000_dapper_the_watchers", + "when": 1738963581892, + "tag": "0000_known_stepford_cuckoos", "breakpoints": true } ]