Files
tg_tinder_bot/scripts/createProfileViewsTable.ts
2025-09-18 11:42:18 +09:00

71 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Скрипт для создания таблицы profile_views
// Функция для ручного запуска создания таблицы profile_views
async function createProfileViewsTable() {
const client = await require('../database/connection').pool.connect();
try {
console.log('Создание таблицы profile_views...');
// Проверяем, существует ли уже таблица profile_views
const tableCheck = await client.query(`
SELECT EXISTS (
SELECT FROM information_schema.tables
WHERE table_schema = 'public'
AND table_name = 'profile_views'
);
`);
if (tableCheck.rows[0].exists) {
console.log('Таблица profile_views уже существует, пропускаем создание');
return;
}
// Начинаем транзакцию
await client.query('BEGIN');
// Создаем таблицу profile_views
await client.query(`
CREATE TABLE profile_views (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
viewer_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
viewed_profile_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
view_date TIMESTAMP NOT NULL DEFAULT NOW(),
view_type VARCHAR(20) NOT NULL DEFAULT 'browse'
);
`);
// Создаем индекс для быстрого поиска по паре (просмотревший - просмотренный)
await client.query(`
CREATE UNIQUE INDEX profile_views_viewer_viewed_idx ON profile_views (viewer_id, viewed_profile_id);
`);
// Индекс для быстрого поиска по viewer_id
await client.query(`
CREATE INDEX profile_views_viewer_idx ON profile_views (viewer_id);
`);
// Индекс для быстрого поиска по viewed_profile_id
await client.query(`
CREATE INDEX profile_views_viewed_idx ON profile_views (viewed_profile_id);
`);
// Фиксируем транзакцию
await client.query('COMMIT');
console.log('Таблица profile_views успешно создана!');
} catch (error) {
// В случае ошибки откатываем транзакцию
await client.query('ROLLBACK');
console.error('Произошла ошибка при создании таблицы profile_views:', error);
} finally {
// Освобождаем клиента
client.release();
}
}
// Запускаем функцию создания таблицы
createProfileViewsTable()
.then(() => console.log('Скрипт выполнен'))
.catch(err => console.error('Ошибка выполнения скрипта:', err))
.finally(() => process.exit());