wtf db
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user