Files
tg_tinder_bot/migrations/1631980000000_add_profile_views_table.ts
2025-09-18 11:42:18 +09:00

45 lines
1.6 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.

import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;
export async function up(pgm: MigrationBuilder): Promise<void> {
// Создание таблицы profile_views для хранения информации о просмотренных профилях
pgm.createTable('profile_views', {
id: { type: 'uuid', primaryKey: true, default: pgm.func('uuid_generate_v4()') },
viewer_id: {
type: 'uuid',
notNull: true,
references: 'users',
onDelete: 'CASCADE'
},
viewed_profile_id: {
type: 'uuid',
notNull: true,
references: 'profiles(user_id)',
onDelete: 'CASCADE'
},
view_date: { type: 'timestamp', notNull: true, default: pgm.func('now()') },
view_type: { type: 'varchar(20)', notNull: true, default: 'browse' }, // browse, match, like, etc.
});
// Создание индекса для быстрого поиска по паре (просмотревший - просмотренный)
pgm.createIndex('profile_views', ['viewer_id', 'viewed_profile_id'], {
unique: true,
name: 'profile_views_viewer_viewed_idx'
});
// Индекс для быстрого поиска по viewer_id
pgm.createIndex('profile_views', ['viewer_id'], {
name: 'profile_views_viewer_idx'
});
// Индекс для быстрого поиска по viewed_profile_id
pgm.createIndex('profile_views', ['viewed_profile_id'], {
name: 'profile_views_viewed_idx'
});
}
export async function down(pgm: MigrationBuilder): Promise<void> {
pgm.dropTable('profile_views', { cascade: true });
}