p5.js-web-editor/server/migrations/db_reformat.js

210 lines
6.2 KiB
JavaScript
Raw Normal View History

/* eslint-disable */
2016-09-04 20:39:12 +00:00
import mongoose from 'mongoose';
import path from 'path';
2016-12-14 01:36:48 +00:00
import { uniqWith, isEqual } from 'lodash';
require('dotenv').config({path: path.resolve('.env')});
2016-09-04 20:39:12 +00:00
const ObjectId = mongoose.Types.ObjectId;
mongoose.connect('mongodb://localhost:27017/p5js-web-editor');
mongoose.connection.on('error', () => {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
import Project from '../models/project';
2016-10-01 18:01:32 +00:00
import User from '../models/user';
2016-09-04 20:39:12 +00:00
import s3 from '@auth0/s3';
let client = s3.createClient({
maxAsyncS3: 20,
s3RetryCount: 3,
s3RetryDelay: 1000,
multipartUploadThreshold: 20971520, // this is the default (20 MB)
multipartUploadSize: 15728640, // this is the default (15 MB)
s3Options: {
accessKeyId: `${process.env.AWS_ACCESS_KEY}`,
secretAccessKey: `${process.env.AWS_SECRET_KEY}`,
2017-04-13 18:39:03 +00:00
region: `${process.env.AWS_REGION}`
},
});
2016-12-14 01:36:48 +00:00
let s3Files = [];
Project.find({})
.exec((err, projects) => {
projects.forEach((project, projectIndex) => {
project.files.forEach((file, fileIndex) => {
if (file.url && !file.url.includes("https://rawgit.com/")) {
s3Files.push(file.url.split('/').pop());
}
});
});
console.log(s3Files.length);
2016-12-14 01:36:48 +00:00
s3Files = uniqWith(s3Files, isEqual);
console.log(s3Files.length);
});
const uploadedFiles = [];
const params = {'s3Params': {'Bucket': `${process.env.S3_BUCKET}`}};
let objectsResponse = client.listObjects(params);
objectsResponse.on('data', function(objects) {
objects.Contents.forEach(object => {
uploadedFiles.push(object.Key);
});
});
2016-12-14 01:12:17 +00:00
const filesToDelete = [];
objectsResponse.on('end', () => {
console.log(uploadedFiles.length);
uploadedFiles.forEach(fileKey => {
if (s3Files.indexOf(fileKey) === -1) {
//delete file
2016-12-14 01:32:57 +00:00
filesToDelete.push({Key: fileKey});
2016-12-14 01:12:17 +00:00
// console.log("would delete file: ", fileKey);
}
});
2016-12-14 01:45:30 +00:00
let params = {
Bucket: `${process.env.S3_BUCKET}`,
Delete: {
Objects: filesToDelete,
},
};
2016-12-14 01:47:19 +00:00
// let del = client.deleteObjects(params);
// del.on('err', (err) => {
// console.log(err);
// });
// del.on('end', () => {
// console.log('deleted extra S3 files!');
// });
2016-12-14 01:12:17 +00:00
console.log("To delete: ", filesToDelete.length);
console.log("Total S3 files: ", uploadedFiles.length);
2016-12-14 01:32:57 +00:00
console.log("Total S3 files in mongo: ", s3Files.length);
});
2016-09-09 02:02:42 +00:00
// let projectsNotToUpdate;
// Project.find({'files.name': 'root'})
// .exec((err, projects) => {
// projectsNotToUpdate = projects.map(project => project.id);
// console.log(projectsNotToUpdate);
2016-09-04 20:39:12 +00:00
2016-09-09 02:02:42 +00:00
// Project.find({})
// .exec((err, projects) => {
// projects.forEach( (project, projectIndex) => {
// if (!projectsNotToUpdate.find(projectId => projectId === project.id)) {
// const childIdArray = project.files.map(file => file._id.valueOf());
// const newId = new ObjectId();
// project.files.push({
// name: 'root',
// _id: newId,
// id: newId,
// fileType: 'folder',
// children: childIdArray,
// content: ''
// });
2016-09-04 20:39:12 +00:00
2016-09-09 02:02:42 +00:00
// project.files = project.files.map(file => {
// if (file.name === "sketch.js") {
// file.isSelected = true;
// return file;
// }
// return file;
// });
// project.save((err, savedProject) => {
// console.log('project', projectIndex, 'is saved.');
// });
// }
// });
// });
// });
2016-09-04 20:39:12 +00:00
// Project.find({'files.name': 'root'})
// .exec((err, projects) => {
// projects.forEach((project, projectIndex) => {
// project.files = project.files.map(file => {
// if (file.name === "sketch.js") {
// file.isSelected = true;
// return file;
// } else if (file.name === "root") {
// file.content = '';
// return file;
// }
// return file;
// });
// project.save((err, savedProject) => {
// console.log('project', projectIndex, 'is saved.');
// });
// });
// });
// const s3Bucket = `http://p5.js-webeditor.s3.amazonaws.com/`;
// const s3BucketHttps = `https://s3-us-west-2.amazonaws.com/p5.js-webeditor/`;
// Project.find({})
// .exec((err, projects) => {
// projects.forEach((project, projectIndex) => {
// project.files.forEach((file) => {
// if (file.url) {
// file.url = file.url.replace(s3Bucket, s3BucketHttps);
// console.log('Updating', file.name);
// console.log(file.url);
// }
// });
// project.save((err, savedProject) => {
// console.log('project', projectIndex, 'is saved.');
// });
// });
// });
2016-09-09 02:02:42 +00:00
2016-10-01 18:01:32 +00:00
// Project.find({})
// .exec((err, projects) => {
// projects.forEach((project, projectIndex) => {
// project.files.forEach((file) => {
// if (file.isSelected) {
// delete file.isSelected;
// }
2016-09-14 20:20:25 +00:00
2016-10-01 18:01:32 +00:00
// if (file.name === 'sketch.js') {
// file.isSelectedFile = true;
// console.log(file.name, 'is now selected');
// // file.save((err, savedFile) => {
// // console.log('file saved');
// // });
// } else {
// file.isSelectedFile = false;
// }
// // console.log('project', projectIndex);
// // if (file.isSelected) {
// // console.log('is selected remains');
// // }
2016-09-14 20:33:47 +00:00
// // if (file.isSelectedFile) {
2016-10-01 18:01:32 +00:00
// // console.log('changed to isSelected file');
// // }
// project.save((err, savedProject) => {
// console.log('project', projectIndex, 'is saved.');
// });
// });
// });
// });
2016-09-14 20:33:47 +00:00
// User.findOne({email: 'test@test.com'})
// .exec((err, user) => {
// console.log(user);
// user.password = '1234';
// user.save((err, savedUser) => {
// console.log('user saved');
// });
// });
// User.find({})
// .exec((err, users) => {
// users.forEach(user => {
// user.preferences.autorefresh = false;
// user.save((err, savedUser) => {
// console.log('user saved');
// });
// });
// });