wtf db
This commit is contained in:
33
.serena/memories/database_auth_fix_complete.md
Normal file
33
.serena/memories/database_auth_fix_complete.md
Normal file
@@ -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
|
||||||
@@ -30,33 +30,49 @@ export class Database {
|
|||||||
|
|
||||||
private async init(): Promise<void> {
|
private async init(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Users table
|
const tablesExist = await this.pool.query(`
|
||||||
await this.pool.query(`CREATE TABLE IF NOT EXISTS users (
|
SELECT EXISTS (
|
||||||
id SERIAL PRIMARY KEY,
|
SELECT FROM information_schema.tables
|
||||||
telegram_id BIGINT UNIQUE NOT NULL,
|
WHERE table_schema = 'public'
|
||||||
username TEXT,
|
AND table_name = 'users'
|
||||||
first_name TEXT,
|
);
|
||||||
last_name TEXT,
|
`);
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)`);
|
|
||||||
|
|
||||||
// Search history table
|
if (!tablesExist.rows[0].exists) {
|
||||||
await this.pool.query(`CREATE TABLE IF NOT EXISTS search_history (
|
console.log('Creating database tables...');
|
||||||
id SERIAL PRIMARY KEY,
|
|
||||||
user_id INTEGER REFERENCES users(id),
|
// Users table
|
||||||
query TEXT NOT NULL,
|
await this.pool.query(`CREATE TABLE users (
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
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
|
// Search history table
|
||||||
await this.pool.query(`CREATE TABLE IF NOT EXISTS downloads (
|
await this.pool.query(`CREATE TABLE search_history (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
user_id INTEGER REFERENCES users(id),
|
user_id INTEGER REFERENCES users(id),
|
||||||
track_id TEXT NOT NULL,
|
query TEXT NOT NULL,
|
||||||
title TEXT NOT NULL,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
file_path TEXT,
|
)`);
|
||||||
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) {
|
} catch (error) {
|
||||||
console.error('Database initialization error:', error);
|
console.error('Database initialization error:', error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user