This commit is contained in:
Andrey Kondratev
2025-08-29 14:49:40 +05:00
parent 816b0ec672
commit 45a071ab4f
2 changed files with 74 additions and 25 deletions

View File

@@ -30,33 +30,49 @@ export class Database {
private async init(): Promise<void> {
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);
}