From 45a071ab4f783a6db8ed2fb8bbf9e00c5357247f Mon Sep 17 00:00:00 2001 From: Andrey Kondratev <81143241+cockroach-eater@users.noreply.github.com> Date: Fri, 29 Aug 2025 14:49:40 +0500 Subject: [PATCH] wtf db --- .../memories/database_auth_fix_complete.md | 33 ++++++++++ src/database.ts | 66 ++++++++++++------- 2 files changed, 74 insertions(+), 25 deletions(-) create mode 100644 .serena/memories/database_auth_fix_complete.md diff --git a/.serena/memories/database_auth_fix_complete.md b/.serena/memories/database_auth_fix_complete.md new file mode 100644 index 0000000..3d11e90 --- /dev/null +++ b/.serena/memories/database_auth_fix_complete.md @@ -0,0 +1,33 @@ +# Database Authentication Issue Fixed + +## Problem +- PostgreSQL authentication was failing with error code 28P01 "password authentication failed for user 'quixotic'" +- The docker-compose.yml had default password `quixotic123` but .env.docker had different password `GGkM0a6r7pw3F65ZvYbbuOgbt7gO2AoW3JynEk7m9Go=` +- Secondary issue: table creation conflicts due to SERIAL sequences being created multiple times + +## Solution +1. **Password sync**: Removed postgres container and volume to reset with correct password from .env.docker + ```bash + docker compose down postgres + docker volume rm quixotic_postgres-data + docker compose up postgres -d + ``` + +2. **Table creation fix**: Modified database.ts to check table existence before creating: + - Added proper table existence check using information_schema + - Removed `CREATE TABLE IF NOT EXISTS` which wasn't handling SERIAL sequences properly + - Added logging for better debugging + +## Commands used +```bash +docker compose down postgres +docker volume rm quixotic_postgres-data +docker compose up postgres -d +docker compose build quixotic-app +docker compose up quixotic-app -d +``` + +## Result +- Database authentication working +- No more table creation conflicts +- App starts successfully with "Database tables already exist" message \ No newline at end of file diff --git a/src/database.ts b/src/database.ts index 8c0a43c..7e28d43 100644 --- a/src/database.ts +++ b/src/database.ts @@ -30,33 +30,49 @@ export class Database { private async init(): Promise { try { - // Users table - await this.pool.query(`CREATE TABLE IF NOT EXISTS users ( - id SERIAL PRIMARY KEY, - telegram_id BIGINT UNIQUE NOT NULL, - username TEXT, - first_name TEXT, - last_name TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - )`); + const tablesExist = await this.pool.query(` + SELECT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' + AND table_name = 'users' + ); + `); - // Search history table - await this.pool.query(`CREATE TABLE IF NOT EXISTS search_history ( - id SERIAL PRIMARY KEY, - user_id INTEGER REFERENCES users(id), - query TEXT NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - )`); + if (!tablesExist.rows[0].exists) { + console.log('Creating database tables...'); + + // Users table + await this.pool.query(`CREATE TABLE users ( + id SERIAL PRIMARY KEY, + telegram_id BIGINT UNIQUE NOT NULL, + username TEXT, + first_name TEXT, + last_name TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )`); - // Downloaded files table - await this.pool.query(`CREATE TABLE IF NOT EXISTS downloads ( - id SERIAL PRIMARY KEY, - user_id INTEGER REFERENCES users(id), - track_id TEXT NOT NULL, - title TEXT NOT NULL, - file_path TEXT, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP - )`); + // Search history table + await this.pool.query(`CREATE TABLE search_history ( + id SERIAL PRIMARY KEY, + user_id INTEGER REFERENCES users(id), + query TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )`); + + // Downloaded files table + await this.pool.query(`CREATE TABLE downloads ( + id SERIAL PRIMARY KEY, + user_id INTEGER REFERENCES users(id), + track_id TEXT NOT NULL, + title TEXT NOT NULL, + file_path TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + )`); + + console.log('Database tables created successfully'); + } else { + console.log('Database tables already exist'); + } } catch (error) { console.error('Database initialization error:', error); }