nakama/backend/scripts/deleteAllMusic.js

99 lines
4.4 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

/**
* Скрипт для удаления всех альбомов и треков из базы данных
*
* ВНИМАНИЕ: Этот скрипт безвозвратно удалит:
* - Все треки (Tracks)
* - Все альбомы (Albums)
* - Все избранные треки (FavoriteTrack)
* - Все ссылки на треки в постах (attachedTrack будет установлен в null)
*
* Использование:
* node backend/scripts/deleteAllMusic.js
*/
require('dotenv').config({ path: require('path').join(__dirname, '../.env') });
const mongoose = require('mongoose');
const Track = require('../models/Track');
const Album = require('../models/Album');
const FavoriteTrack = require('../models/FavoriteTrack');
const Post = require('../models/Post');
const config = require('../config/index');
async function deleteAllMusic() {
try {
console.log('🔌 Подключение к MongoDB...');
await mongoose.connect(config.mongoUri);
console.log('✅ Подключено к MongoDB\n');
// Подсчет перед удалением
const tracksCount = await Track.countDocuments();
const albumsCount = await Album.countDocuments();
const favoritesCount = await FavoriteTrack.countDocuments();
const postsWithTracksCount = await Post.countDocuments({ attachedTrack: { $ne: null } });
console.log('📊 Текущая статистика:');
console.log(` - Треков: ${tracksCount}`);
console.log(` - Альбомов: ${albumsCount}`);
console.log(` - Избранных треков: ${favoritesCount}`);
console.log(` - Постов с прикрепленными треками: ${postsWithTracksCount}\n`);
if (tracksCount === 0 && albumsCount === 0) {
console.log('✅ В базе данных нет треков и альбомов для удаления');
await mongoose.disconnect();
return;
}
console.log('🗑️ Начало удаления...\n');
// 1. Удалить все избранные треки
console.log('1⃣ Удаление избранных треков...');
const favoritesResult = await FavoriteTrack.deleteMany({});
console.log(` ✅ Удалено избранных треков: ${favoritesResult.deletedCount}`);
// 2. Обнулить attachedTrack во всех постах
console.log('\n2⃣ Обнуление прикрепленных треков в постах...');
const postsResult = await Post.updateMany(
{ attachedTrack: { $ne: null } },
{ $set: { attachedTrack: null } }
);
console.log(` ✅ Обновлено постов: ${postsResult.modifiedCount}`);
// 3. Удалить все треки
console.log('\n3⃣ Удаление треков...');
const tracksResult = await Track.deleteMany({});
console.log(` ✅ Удалено треков: ${tracksResult.deletedCount}`);
// 4. Удалить все альбомы
console.log('\n4⃣ Удаление альбомов...');
const albumsResult = await Album.deleteMany({});
console.log(` ✅ Удалено альбомов: ${albumsResult.deletedCount}`);
// Финальная статистика
console.log('\n📊 Финальная статистика:');
const finalTracksCount = await Track.countDocuments();
const finalAlbumsCount = await Album.countDocuments();
const finalFavoritesCount = await FavoriteTrack.countDocuments();
const finalPostsWithTracksCount = await Post.countDocuments({ attachedTrack: { $ne: null } });
console.log(` - Треков: ${finalTracksCount}`);
console.log(` - Альбомов: ${finalAlbumsCount}`);
console.log(` - Избранных треков: ${finalFavoritesCount}`);
console.log(` - Постов с прикрепленными треками: ${finalPostsWithTracksCount}`);
console.log('\n✅ Все альбомы и треки успешно удалены!');
} catch (error) {
console.error('❌ Ошибка при удалении:', error);
process.exit(1);
} finally {
await mongoose.disconnect();
console.log('\n🔌 Отключено от MongoDB');
process.exit(0);
}
}
// Запуск скрипта
deleteAllMusic();