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 05:14:12 +03:00
|
|
|
|
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) {
|
2021-08-24 05:14:12 +03:00
|
|
|
const subscriberCountFields = ["id", "subscriptions", "slateId", slate.id];
|
2021-08-24 04:10:47 +03:00
|
|
|
const subscriberCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
|
|
|
|
|
2021-08-24 05:14:12 +03:00
|
|
|
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",
|
2021-08-24 05:14:12 +03:00
|
|
|
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) {
|
2021-08-24 05:14:12 +03:00
|
|
|
const followerCountFields = ["id", "subscriptions", "userId", user.id];
|
2021-08-24 04:10:47 +03:00
|
|
|
const followerCount = `(SELECT COUNT(??) FROM ?? WHERE ?? = ?)`;
|
|
|
|
|
2021-08-24 05:14:12 +03:00
|
|
|
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",
|
2021-08-24 05:14:12 +03:00
|
|
|
user.id,
|
2021-08-24 04:10:47 +03:00
|
|
|
];
|
|
|
|
const update = await DB.raw(
|
|
|
|
`UPDATE ?? SET ?? = ${slateCount}, ?? = ${followerCount} WHERE ?? = ? RETURNING *`,
|
|
|
|
updateFields
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-08-24 05:14:12 +03:00
|
|
|
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 () => {
|
2021-08-24 05:14:12 +03:00
|
|
|
// await makeFilesPublic();
|
2021-08-24 04:10:47 +03:00
|
|
|
// await getSlateSubscriberAndFileCount();
|
|
|
|
// await getUserFollowerAndSlateCount();
|
2021-08-24 05:14:12 +03:00
|
|
|
await removeUnusedColumns();
|
2021-08-24 04:10:47 +03:00
|
|
|
|
|
|
|
Logging.log("Finished running. Hit CTRL + C to quit");
|
|
|
|
};
|
|
|
|
|
|
|
|
runScript();
|