Skip to content

Commit

Permalink
fix: primary key added for onConflict in userSettings table
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishtar committed Jun 27, 2024
1 parent e06f107 commit ddb27d5
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/db/migrations/0003_jittery_black_tom.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE "user_settings" ADD PRIMARY KEY ("user_id");
263 changes: 263 additions & 0 deletions src/db/migrations/meta/0003_snapshot.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
}
7 changes: 7 additions & 0 deletions src/db/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
]
}
2 changes: 1 addition & 1 deletion src/db/queries/upsertUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
2 changes: 1 addition & 1 deletion src/db/queries/upsertUserSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
})
}
5 changes: 4 additions & 1 deletion src/db/schema/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
4 changes: 3 additions & 1 deletion src/utils/getDbConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit ddb27d5

Please sign in to comment.