From ddb27d5e4345288a2aec2114be40db63f837db67 Mon Sep 17 00:00:00 2001 From: Vishtar Date: Thu, 27 Jun 2024 21:41:08 +0400 Subject: [PATCH] fix: primary key added for onConflict in userSettings table --- src/db/migrations/0003_jittery_black_tom.sql | 1 + src/db/migrations/meta/0003_snapshot.json | 263 +++++++++++++++++++ src/db/migrations/meta/_journal.json | 7 + src/db/queries/upsertUser.ts | 2 +- src/db/queries/upsertUserSettings.ts | 2 +- src/db/schema/userSettings.ts | 5 +- src/utils/getDbConnection.ts | 4 +- src/web.ts | 2 +- 8 files changed, 281 insertions(+), 5 deletions(-) create mode 100644 src/db/migrations/0003_jittery_black_tom.sql create mode 100644 src/db/migrations/meta/0003_snapshot.json diff --git a/src/db/migrations/0003_jittery_black_tom.sql b/src/db/migrations/0003_jittery_black_tom.sql new file mode 100644 index 0000000..d2c6212 --- /dev/null +++ b/src/db/migrations/0003_jittery_black_tom.sql @@ -0,0 +1 @@ +ALTER TABLE "user_settings" ADD PRIMARY KEY ("user_id"); \ No newline at end of file diff --git a/src/db/migrations/meta/0003_snapshot.json b/src/db/migrations/meta/0003_snapshot.json new file mode 100644 index 0000000..9add3c5 --- /dev/null +++ b/src/db/migrations/meta/0003_snapshot.json @@ -0,0 +1,263 @@ +{ + "id": "2befea74-319c-4fb1-9887-6a034e94f927", + "prevId": "32f5d9cf-d77b-4e0d-b11b-9f449697d8b9", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.tokens": { + "name": "tokens", + "schema": "", + "columns": { + "token": { + "name": "token", + "type": "varchar(128)", + "primaryKey": true, + "notNull": true + }, + "wallet": { + "name": "wallet", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "ticker": { + "name": "ticker", + "type": "varchar(16)", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "tokens_wallet_wallets_address_fk": { + "name": "tokens_wallet_wallets_address_fk", + "tableFrom": "tokens", + "tableTo": "wallets", + "columnsFrom": ["wallet"], + "columnsTo": ["address"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "wallet-token": { + "name": "wallet-token", + "nullsNotDistinct": false, + "columns": ["wallet", "token"] + } + } + }, + "public.user_notifications": { + "name": "user_notifications", + "schema": "", + "columns": { + "wallet": { + "name": "wallet", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "jetton": { + "name": "jetton", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "timestamp": { + "name": "timestamp", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "price": { + "name": "price", + "type": "integer", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "user_notifications_wallet_wallets_address_fk": { + "name": "user_notifications_wallet_wallets_address_fk", + "tableFrom": "user_notifications", + "tableTo": "wallets", + "columnsFrom": ["wallet"], + "columnsTo": ["address"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "user_notifications_jetton_tokens_token_fk": { + "name": "user_notifications_jetton_tokens_token_fk", + "tableFrom": "user_notifications", + "tableTo": "tokens", + "columnsFrom": ["jetton"], + "columnsTo": ["token"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user_purchases": { + "name": "user_purchases", + "schema": "", + "columns": { + "wallet": { + "name": "wallet", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "jetton": { + "name": "jetton", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "timestamp": { + "name": "timestamp", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "price": { + "name": "price", + "type": "integer", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "user_purchases_wallet_wallets_address_fk": { + "name": "user_purchases_wallet_wallets_address_fk", + "tableFrom": "user_purchases", + "tableTo": "wallets", + "columnsFrom": ["wallet"], + "columnsTo": ["address"], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "user_purchases_jetton_tokens_token_fk": { + "name": "user_purchases_jetton_tokens_token_fk", + "tableFrom": "user_purchases", + "tableTo": "tokens", + "columnsFrom": ["jetton"], + "columnsTo": ["token"], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.user_settings": { + "name": "user_settings", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "language_code": { + "name": "language_code", + "type": "varchar(2)", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "user_settings_user_id_users_id_fk": { + "name": "user_settings_user_id_users_id_fk", + "tableFrom": "user_settings", + "tableTo": "users", + "columnsFrom": ["user_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.users": { + "name": "users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "username": { + "name": "username", + "type": "varchar(32)", + "primaryKey": false, + "notNull": true + }, + "timestamp": { + "name": "timestamp", + "type": "integer", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {} + }, + "public.wallets": { + "name": "wallets", + "schema": "", + "columns": { + "address": { + "name": "address", + "type": "varchar(128)", + "primaryKey": true, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": { + "wallets_user_id_users_id_fk": { + "name": "wallets_user_id_users_id_fk", + "tableFrom": "wallets", + "tableTo": "users", + "columnsFrom": ["user_id"], + "columnsTo": ["id"], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "user_id-address": { + "name": "user_id-address", + "nullsNotDistinct": false, + "columns": ["user_id", "address"] + } + } + } + }, + "enums": {}, + "schemas": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json index 4c1076c..07b7fbe 100644 --- a/src/db/migrations/meta/_journal.json +++ b/src/db/migrations/meta/_journal.json @@ -22,6 +22,13 @@ "when": 1719403374093, "tag": "0002_ordinary_puck", "breakpoints": true + }, + { + "idx": 3, + "version": "7", + "when": 1719509995995, + "tag": "0003_jittery_black_tom", + "breakpoints": true } ] } diff --git a/src/db/queries/upsertUser.ts b/src/db/queries/upsertUser.ts index 7fed63a..0e15222 100644 --- a/src/db/queries/upsertUser.ts +++ b/src/db/queries/upsertUser.ts @@ -2,5 +2,5 @@ import type { TDbConnection } from '../../types' import { users } from '../schema' export const upsertUser = (db: TDbConnection, values: typeof users.$inferInsert) => { - return db.insert(users).values(values).onConflictDoNothing({ target: users.id }) + return db.insert(users).values(values).onConflictDoNothing() } diff --git a/src/db/queries/upsertUserSettings.ts b/src/db/queries/upsertUserSettings.ts index 66c8dd1..a7f95ae 100644 --- a/src/db/queries/upsertUserSettings.ts +++ b/src/db/queries/upsertUserSettings.ts @@ -6,7 +6,7 @@ export const upsertUserSettings = (db: TDbConnection, values: typeof userSetting .insert(userSettings) .values(values) .onConflictDoUpdate({ - target: userSettings.languageCode, + target: userSettings.userId, set: { languageCode: values.languageCode }, }) } diff --git a/src/db/schema/userSettings.ts b/src/db/schema/userSettings.ts index 7679085..664ec96 100644 --- a/src/db/schema/userSettings.ts +++ b/src/db/schema/userSettings.ts @@ -4,6 +4,9 @@ import { users } from '.' export const userSettings = pgTable('user_settings', { userId: bigint('user_id', { mode: 'number' }) .notNull() - .references(() => users.id, { onDelete: 'cascade' }), + .references(() => users.id, { + onDelete: 'cascade', + }) + .primaryKey(), languageCode: varchar('language_code', { length: 2 }).notNull(), }) diff --git a/src/utils/getDbConnection.ts b/src/utils/getDbConnection.ts index b769902..123a210 100644 --- a/src/utils/getDbConnection.ts +++ b/src/utils/getDbConnection.ts @@ -4,7 +4,9 @@ import type { TDbConnection } from '../types' export const getDbConnection = async () => { const db = new PGlite('file://data/postgresql') - const connection = drizzle(db) + const connection = drizzle(db, { + logger: true, + }) // @ts-expect-error object restructuring didn't work that easy for types connection.close = () => db.close() diff --git a/src/web.ts b/src/web.ts index 9a0f422..f090cf0 100644 --- a/src/web.ts +++ b/src/web.ts @@ -4,7 +4,7 @@ import type { ParamsDictionary, Query } from 'express-serve-static-core' import { getDbConnection, getLogger, logError } from './utils' import { api } from './utils/parseTxData' import type { TRequestHandler } from './types' -import { upsertUser, insertUserAdress } from './db/queries' +import { insertUserAdress } from './db/queries' const app = express()