<commit_before>const mongoose = require('mongoose');

const { info } = require('../src/logger');
const { connect, close } = require('../src/utils/database');
const { FileModel } = require('../src/services/fileStorage/model');

module.exports = {
	up: async function up() {
		await connect();
		// ////////////////////////////////////////////////////
		info('Updating files...');
		const allFiles = await FileModel.estimatedDocumentCount();
		const filesToUpdate = await FileModel.countDocuments({
			'permissions.0.refPermModel': 'user',
			'permissions.0.refId': { $exists: true },
		}).exec();
		info(`${allFiles} files exist. ${filesToUpdate} files have creator information.`);

		info('Adding "creator" attribute. This may take a while...');
		// We need to use the raw mongodb connection, because it allows us to let the DB handle
		// the attribute creation within an aggregate stage, whereas in Mongoose we would need to
		// do this in our code.
		await mongoose.connection.collection('files').updateMany({
			'permissions.0.refPermModel': 'user',
			'permissions.0.refId': { $exists: true },
		}, [
			{ $set: { creator: { $arrayElemAt: ['$permissions.refId', 0] } } },
		]);
		info('Done.');

		info('Updating file indexes...');
		await FileModel.syncIndexes();
		info('Done.');
		// ////////////////////////////////////////////////////
		await close();
	},

	down: async function down() {<commit_msg>Remove the need for direct mongo access<commit_after>const { info } = require('../src/logger');
const { connect, close } = require('../src/utils/database');
const { FileModel } = require('../src/services/fileStorage/model');

module.exports = {
	up: async function up() {
		await connect();
		// ////////////////////////////////////////////////////
		info('Updating files...');
		const allFiles = await FileModel.estimatedDocumentCount();
		const filesToUpdate = await FileModel.countDocuments({
			'permissions.0.refPermModel': 'user',
			'permissions.0.refId': { $exists: true },
		}).exec();
		info(`${allFiles} files exist. ${filesToUpdate} files have creator information.`);

		info('Adding "creator" attribute. This may take a while...');
		await FileModel.updateMany({
			'permissions.0.refPermModel': 'user',
			'permissions.0.refId': { $exists: true },
		}, [
			{ $set: { creator: { $arrayElemAt: ['$permissions.refId', 0] } } },
		]);
		info('Done.');

		info('Updating file indexes...');
		await FileModel.syncIndexes();
		info('Done.');
		// ////////////////////////////////////////////////////
		await close();
	},

	down: async function down() {
{'fraction_non_alphanumeric': 0.24542979385453131, 'fraction_numerical': 0.0038895371450797353, 'mean_word_length': 2.7060518731988474, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3226600', 'n_tokens_mistral': 797, 'n_tokens_neox': 741, 'n_words': 193}