mainly functional matching
This commit is contained in:
70
scripts/createProfileViewsTable.ts
Normal file
70
scripts/createProfileViewsTable.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// Скрипт для создания таблицы 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());
|
||||
Reference in New Issue
Block a user