slate/scripts/privacy-and-counts-repair.js

103 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-08-24 04:10:47 +03:00
import configs from "~/knexfile";
import knex from "knex";
import { v4 as uuid } from "uuid";
import * as Logging from "~/common/logging";
import * as Utilities from "~/node_common/utilities";
import * as Data from "~/node_common/data";
import * as Strings from "~/common/strings";
import * as Constants from "~/common/constants";
const envConfig = configs["development"];
const DB = knex(envConfig);
Logging.log(`RUNNING: files-migration.js`);
const makeFilesPublic = async () => {
await DB.from("files").update({ isPublic: false });
2021-08-24 04:10:47 +03:00
let publicFiles = await DB.from("files")
.whereIn("id", function () {
this.select("slate_files.fileId")
.from("slate_files")
.join("slates", "slates.id", "=", "slate_files.slateId")
.where("slates.isPublic", true);
})
.update({ isPublic: true });
};
const getSlateSubscriberAndFileCount = async () => {
const slates = await DB.select("*").from("slates");
for (let slate of slates) {
const subscriberCountFields = ["id", "subscriptions", "slateId", slate.id];
2021-08-24 04:10:47 +03:00
const subscriberCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
const fileCountFields = ["id", "slate_files", "slateId", slate.id];
2021-08-24 04:10:47 +03:00
const fileCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
const updateFields = [
"slates",
"fileCount",
...fileCountFields,
"subscriberCount",
...subscriberCountFields,
"id",
slate.id,
2021-08-24 04:10:47 +03:00
];
const update = await DB.raw(
`UPDATE ?? SET ?? = ${fileCount}, ?? = ${subscriberCount} WHERE ?? = ? RETURNING *`,
updateFields
);
}
};
const getUserFollowerAndSlateCount = async () => {
const users = await DB.select("*").from("users");
for (let user of users) {
const followerCountFields = ["id", "subscriptions", "userId", user.id];
2021-08-24 04:10:47 +03:00
const followerCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
const slateCountFields = ["id", "slates", "ownerId", user.id, "isPublic", true];
2021-08-24 04:10:47 +03:00
const slateCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ? AND ?? = ?)`;
const updateFields = [
"users",
"slateCount",
...slateCountFields,
"followerCount",
...followerCountFields,
"id",
user.id,
2021-08-24 04:10:47 +03:00
];
const update = await DB.raw(
`UPDATE ?? SET ?? = ${slateCount}, ?? = ${followerCount} WHERE ?? = ? RETURNING *`,
updateFields
);
}
};
const removeUnusedColumns = async () => {
await DB.schema.table("users", function (table) {
table.dropColumn("fileCount");
});
await DB.schema.table("files", function (table) {
table.dropColumn("likeCount");
});
await DB.schema.dropTable("likes");
};
2021-08-24 04:10:47 +03:00
const runScript = async () => {
// await makeFilesPublic();
2021-08-24 04:10:47 +03:00
// await getSlateSubscriberAndFileCount();
// await getUserFollowerAndSlateCount();
await removeUnusedColumns();
2021-08-24 04:10:47 +03:00
Logging.log("Finished running. Hit CTRL + C to quit");
};
runScript();