2021-03-07 23:53:54 +03:00
|
|
|
import configs from "~/knexfile";
|
|
|
|
import knex from "knex";
|
|
|
|
import { v4 as uuid } from "uuid";
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2021-03-07 23:53:54 +03:00
|
|
|
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);
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`RUNNING: files-migration.js`);
|
2021-03-07 23:53:54 +03:00
|
|
|
|
|
|
|
// MARK: - check what parameters are in each table
|
|
|
|
|
|
|
|
const printUsersTable = async () => {
|
|
|
|
const users = await DB.select("*").from("users");
|
|
|
|
let dataParams = {};
|
|
|
|
let fileParams = {};
|
|
|
|
let coverParams = {};
|
|
|
|
for (let user of users) {
|
|
|
|
for (let param of Object.keys(user.data)) {
|
|
|
|
if (!dataParams[param]) {
|
|
|
|
dataParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 03:59:02 +03:00
|
|
|
if (user.data?.library && user.data.library[0]?.children?.length) {
|
2021-03-07 23:53:54 +03:00
|
|
|
let library = user.data.library[0].children;
|
|
|
|
for (let file of library) {
|
|
|
|
for (let param of Object.keys(file)) {
|
|
|
|
if (!fileParams[param]) {
|
|
|
|
fileParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file.coverImage) {
|
|
|
|
for (let param of Object.keys(file.coverImage)) {
|
|
|
|
if (!coverParams[param]) {
|
|
|
|
coverParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({ dataParams: Object.keys(dataParams) });
|
|
|
|
Logging.log({ fileParams: Object.keys(fileParams) });
|
|
|
|
Logging.log({ coverParams: Object.keys(coverParams) });
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const printSlatesTable = async () => {
|
|
|
|
const slates = await DB.select("*").from("slates");
|
|
|
|
let dataParams = {};
|
|
|
|
let fileParams = {};
|
|
|
|
let coverParams = {};
|
|
|
|
for (let slate of slates) {
|
|
|
|
for (let param of Object.keys(slate.data)) {
|
|
|
|
if (!dataParams[param]) {
|
|
|
|
dataParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (slate.data.objects?.length) {
|
|
|
|
let objects = slate.data.objects;
|
|
|
|
for (let file of objects) {
|
|
|
|
for (let param of Object.keys(file)) {
|
|
|
|
if (!fileParams[param]) {
|
|
|
|
fileParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (file.coverImage) {
|
|
|
|
for (let param of Object.keys(file.coverImage)) {
|
|
|
|
if (!coverParams[param]) {
|
|
|
|
coverParams[param] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({ dataParams: Object.keys(dataParams) });
|
|
|
|
Logging.log({ fileParams: Object.keys(fileParams) });
|
|
|
|
Logging.log({ coverParams: Object.keys(coverParams) });
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// MARK: - add/modify tables
|
|
|
|
|
|
|
|
const addTables = async () => {
|
|
|
|
await DB.schema.table("deals", function (table) {
|
2021-04-21 05:11:38 +03:00
|
|
|
table.renameColumn("owner_user_id", "ownerId");
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
2021-04-21 05:11:38 +03:00
|
|
|
table.renameColumn("updated_at", "updatedAt");
|
2021-03-07 23:53:54 +03:00
|
|
|
});
|
|
|
|
|
2021-04-22 12:26:47 +03:00
|
|
|
await DB.schema.table("orphans", function (table) {
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
});
|
|
|
|
|
2021-04-22 12:26:47 +03:00
|
|
|
await DB.schema.table("global", function (table) {
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
});
|
|
|
|
|
2021-04-22 12:26:47 +03:00
|
|
|
await DB.schema.table("stats", function (table) {
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
});
|
|
|
|
|
2021-04-22 12:26:47 +03:00
|
|
|
await DB.schema.renameTable("activity", "old_activity");
|
|
|
|
|
|
|
|
await DB.schema.table("keys", function (table) {
|
|
|
|
table.renameColumn("owner_id", "ownerId");
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.createTable("files", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(DB.raw("uuid_generate_v4()"));
|
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(DB.raw("now()"));
|
|
|
|
table.string("cid").notNullable();
|
|
|
|
table.string("filename").nullable();
|
|
|
|
table.boolean("isPublic").notNullable().defaultTo(false);
|
|
|
|
table.jsonb("data").nullable();
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.createTable("slate_files", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(DB.raw("uuid_generate_v4()"));
|
|
|
|
table.uuid("fileId").references("id").inTable("files");
|
|
|
|
table.uuid("slateId").references("id").inTable("slates");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(DB.raw("now()"));
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.createTable("activity", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(DB.raw("uuid_generate_v4()"));
|
2021-04-22 12:26:47 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.table("activity", function (table) {
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.uuid("userId").references("id").inTable("users");
|
|
|
|
table.uuid("slateId").references("id").inTable("slates");
|
|
|
|
table.uuid("fileId").references("id").inTable("files");
|
|
|
|
table.string("type");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(DB.raw("now()"));
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.table("slates", function (table) {
|
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
table.renameColumn("updated_at", "updatedAt");
|
|
|
|
table.dropColumn("published_at");
|
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.boolean("isPublic").notNullable().defaultTo(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.table("users", function (table) {
|
2021-04-22 12:26:47 +03:00
|
|
|
table.string("email").unique().nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
table.renameColumn("updated_at", "lastActive");
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.table("subscriptions", function (table) {
|
|
|
|
table.renameColumn("created_at", "createdAt");
|
|
|
|
table.renameColumn("owner_user_id", "ownerId");
|
|
|
|
table.renameColumn("target_slate_id", "slateId");
|
|
|
|
table.renameColumn("target_user_id", "userId");
|
|
|
|
});
|
|
|
|
|
|
|
|
await DB.schema.table("subscriptions", function (table) {
|
|
|
|
table.uuid("ownerId").references("id").inTable("users").alter();
|
|
|
|
table.uuid("slateId").references("id").inTable("slates").alter();
|
|
|
|
table.uuid("userId").references("id").inTable("users").alter();
|
|
|
|
});
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished adding tables");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// MARK: - populate new tables
|
|
|
|
|
|
|
|
const migrateUsersTable = async (testing = false) => {
|
|
|
|
const users = await DB.select("*").from("users");
|
|
|
|
let count = 0;
|
|
|
|
for (let user of users) {
|
|
|
|
if (user.data.library) {
|
|
|
|
if (user.data?.library[0]?.children?.length) {
|
|
|
|
let library = user.data.library[0].children;
|
2021-04-22 12:26:47 +03:00
|
|
|
|
|
|
|
let libraryCids = {};
|
|
|
|
|
|
|
|
let libraryIds = library.map((file) => file.id.replace("data-", ""));
|
|
|
|
let conflicts = await DB.select("id").from("files").whereIn("id", libraryIds);
|
|
|
|
conflicts = conflicts.map((res) => res.id);
|
|
|
|
|
|
|
|
let newFiles = [];
|
2021-03-07 23:53:54 +03:00
|
|
|
for (let file of library) {
|
|
|
|
let cid;
|
|
|
|
if (file.cid) {
|
|
|
|
cid = file.cid;
|
|
|
|
} else if (file.ipfs.includes("/ipfs/")) {
|
|
|
|
cid = Strings.getCIDFromIPFS(file.ipfs);
|
|
|
|
} else {
|
|
|
|
cid = file.ipfs;
|
|
|
|
}
|
|
|
|
if (!cid) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("file does not have cid or ipfs");
|
|
|
|
Logging.log(file);
|
2021-03-07 23:53:54 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
let id = file.id.replace("data-", "");
|
2021-04-22 12:26:47 +03:00
|
|
|
|
|
|
|
if (id.length !== 36) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//NOTE(martina): to make sure there are no duplicate cids in the user's files
|
|
|
|
if (libraryCids[cid]) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`skipped duplicate cid ${cid} in user ${user.username} files`);
|
2021-03-07 23:53:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
libraryCids[cid] = true;
|
|
|
|
|
|
|
|
//NOTE(martina): to make sure there are no files in the user or in other users that have the same id (due to save copy). If so, change the id
|
|
|
|
const hasConflict = conflicts.includes(id);
|
|
|
|
conflicts.push(id);
|
|
|
|
if (hasConflict) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`changing id for saved copy ${id} in ${user.username} files`);
|
2021-03-07 23:53:54 +03:00
|
|
|
id = uuid();
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let newFile = {
|
|
|
|
id,
|
|
|
|
ownerId: user.id,
|
|
|
|
cid,
|
|
|
|
isPublic: file.public,
|
|
|
|
createdAt: file.date,
|
2021-04-22 12:26:47 +03:00
|
|
|
filename: file.file ? file.file.substring(0, 255) : "",
|
2021-03-07 23:53:54 +03:00
|
|
|
data: {
|
2021-04-22 12:26:47 +03:00
|
|
|
name: file.name ? file.name.substring(0, 255) : "",
|
2021-03-07 23:53:54 +03:00
|
|
|
blurhash: file.blurhash,
|
|
|
|
size: file.size,
|
|
|
|
type: file.type,
|
|
|
|
tags: file.tags, //daniel is working on this, so keep it here
|
|
|
|
link: file.link, //chris is working on this, so keep it here
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if (file.unityGameConfig || file.unityGameLoader) {
|
|
|
|
newFile.data.unity = {
|
|
|
|
config: file.unityGameConfig,
|
|
|
|
loader: file.unityGameLoader,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (file.coverImage) {
|
|
|
|
let coverImage = file.coverImage;
|
|
|
|
let newCoverImage = {
|
|
|
|
id: coverImage.id,
|
|
|
|
cid: coverImage.cid,
|
|
|
|
createdAt: coverImage.date,
|
|
|
|
filename: coverImage.file,
|
|
|
|
data: {
|
|
|
|
name: coverImage.name,
|
|
|
|
blurhash: coverImage.blurhash,
|
|
|
|
size: coverImage.size,
|
|
|
|
type: coverImage.type,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
newFile.data.coverImage = newCoverImage;
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
newFiles.push(newFile);
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
if (testing) {
|
2021-06-11 22:25:58 +03:00
|
|
|
// Logging.log(newFiles);
|
2021-04-22 12:26:47 +03:00
|
|
|
} else {
|
|
|
|
await DB.insert(newFiles).into("files");
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
// if (testing) {
|
|
|
|
// if (count >= 10) {
|
|
|
|
// return;
|
|
|
|
// }
|
|
|
|
// }
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
if (count % 100 === 0) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`${count} users done`);
|
2021-04-22 12:26:47 +03:00
|
|
|
}
|
2021-03-07 23:53:54 +03:00
|
|
|
count += 1;
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished migrating users table");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const migrateSlatesTable = async (testing = false) => {
|
2021-04-22 12:26:47 +03:00
|
|
|
const slates = await DB.select("*").from("slates").offset(3000);
|
2021-03-07 23:53:54 +03:00
|
|
|
let count = 0;
|
|
|
|
for (let slate of slates) {
|
2021-04-22 12:26:47 +03:00
|
|
|
if (!slate.data.ownerId) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({ slateMissingOwnerId: slate });
|
2021-04-22 12:26:47 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-07 23:53:54 +03:00
|
|
|
if (slate.data.objects) {
|
2021-04-22 12:26:47 +03:00
|
|
|
let objects = [];
|
|
|
|
let slateCids = {};
|
|
|
|
|
|
|
|
let fileIds = slate.data.objects
|
|
|
|
.filter((file) => file.id)
|
|
|
|
.map((file) => file.id.replace("data-", ""));
|
|
|
|
let matchingFiles = await DB.select("*").from("files").whereIn("id", fileIds);
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
for (let file of slate.data.objects) {
|
2021-04-22 12:26:47 +03:00
|
|
|
if (!file.url || !file.ownerId || !file.id) {
|
|
|
|
if (!file.ownerId) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({ fileMissingOwnerId: file });
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
continue;
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
let cid = Strings.urlToCid(file.url);
|
|
|
|
|
|
|
|
//NOTE(martina): make sure there are no duplicated cids in a slate
|
|
|
|
if (slateCids[cid]) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`found duplicate file cid ${cid} in slate`);
|
2021-03-07 23:53:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
slateCids[cid] = true;
|
|
|
|
|
|
|
|
const fileId = file.id.replace("data-", "");
|
|
|
|
let matchingFile = matchingFiles.find((item) => item.id === fileId);
|
|
|
|
|
|
|
|
if (!matchingFile) {
|
|
|
|
matchingFile = await DB.select("*")
|
|
|
|
.from("files")
|
|
|
|
.where({ cid: cid, ownerId: file.ownerId })
|
|
|
|
.first();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!matchingFile) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (
|
|
|
|
slate.data.ownerId === file.ownerId &&
|
|
|
|
(file.name !== file.title ||
|
|
|
|
!Strings.isEmpty(file.body) ||
|
|
|
|
!Strings.isEmpty(file.source) ||
|
|
|
|
!Strings.isEmpty(file.author))
|
|
|
|
) {
|
|
|
|
if (!matchingFile.data) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("Matching file did not have data");
|
2021-03-07 23:53:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!testing) {
|
|
|
|
await DB.from("files")
|
2021-04-22 12:26:47 +03:00
|
|
|
.where({ cid: cid, ownerId: slate.data.ownerId })
|
2021-03-07 23:53:54 +03:00
|
|
|
.update({
|
|
|
|
data: {
|
|
|
|
...matchingFile.data,
|
2021-04-22 12:26:47 +03:00
|
|
|
name: file.title ? file.title.substring(0, 255) : "",
|
2021-03-07 23:53:54 +03:00
|
|
|
body: file.body,
|
|
|
|
source: file.source,
|
|
|
|
author: file.author,
|
|
|
|
},
|
|
|
|
});
|
2021-04-22 12:26:47 +03:00
|
|
|
} else {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({
|
2021-04-22 12:26:47 +03:00
|
|
|
data: {
|
|
|
|
...matchingFile.data,
|
|
|
|
name: file.title ? file.title.substring(0, 255) : "",
|
|
|
|
body: file.body,
|
|
|
|
source: file.source,
|
|
|
|
author: file.author,
|
|
|
|
},
|
|
|
|
});
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
let existingSlateFile = await DB.select("*")
|
|
|
|
.from("slate_files")
|
|
|
|
.where({ fileId: matchingFile.id, slateId: slate.id })
|
|
|
|
.first();
|
|
|
|
if (!existingSlateFile) {
|
|
|
|
objects.push({ fileId: matchingFile.id, slateId: slate.id });
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
if (!testing) {
|
|
|
|
if (objects.length) {
|
|
|
|
await DB.insert(objects).into("slate_files");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (count % 100 === 0) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`${count} slates done`);
|
2021-04-22 12:26:47 +03:00
|
|
|
}
|
|
|
|
// if (testing) {
|
|
|
|
// if (count >= 50) {
|
|
|
|
// return;
|
|
|
|
// }
|
2021-06-11 22:25:58 +03:00
|
|
|
// Logging.log(objects);
|
2021-04-22 12:26:47 +03:00
|
|
|
// }
|
|
|
|
count += 1;
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished migrating slates table");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
//make a function that double checks correctness: finds any duplicate fileId, ownerId in files. and any duplicate fileid slateid in slate_files
|
|
|
|
|
|
|
|
/*
|
|
|
|
"CREATE_SLATE" - owner, slate
|
|
|
|
"CREATE_SLATE_OBJECT" - owner, slate, file
|
|
|
|
"CREATE_USER" - owner
|
|
|
|
"USER_DEAL" - owner
|
|
|
|
"SUBSCRIBE_USER" - owner, user
|
|
|
|
"SUBSCRIBE_SLATE" - owner, slate
|
|
|
|
*/
|
|
|
|
const migrateActivityTable = async (testing = false) => {
|
|
|
|
const query = await DB.select("*").from("old_activity");
|
|
|
|
const activity = JSON.parse(JSON.stringify(query));
|
|
|
|
let count = 0;
|
|
|
|
for (let event of activity) {
|
|
|
|
let type = event.data.type;
|
2021-04-22 12:26:47 +03:00
|
|
|
if (type !== "CREATE_SLATE_OBJECT") {
|
2021-03-07 23:53:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const id = event.id;
|
|
|
|
const createdAt = event.created_at;
|
|
|
|
let ownerId = event.data.actorUserId;
|
|
|
|
let userId, slateId, fileId;
|
2021-04-22 12:26:47 +03:00
|
|
|
|
|
|
|
slateId = event.data.context?.slate?.id;
|
|
|
|
fileId = event.data.context?.file?.id;
|
|
|
|
if (!slateId || !fileId) {
|
2021-06-11 22:25:58 +03:00
|
|
|
// Logging.log(event.data);
|
2021-04-22 12:26:47 +03:00
|
|
|
continue;
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
2021-04-22 12:26:47 +03:00
|
|
|
fileId = fileId.replace("data-", "");
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
if (!testing) {
|
|
|
|
try {
|
|
|
|
await DB.insert({
|
|
|
|
id,
|
|
|
|
ownerId,
|
|
|
|
userId,
|
|
|
|
slateId,
|
|
|
|
fileId,
|
|
|
|
type,
|
|
|
|
createdAt,
|
|
|
|
}).into("activity");
|
|
|
|
} catch (e) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(e);
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (testing) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log({
|
2021-04-22 12:26:47 +03:00
|
|
|
id,
|
|
|
|
ownerId,
|
|
|
|
userId,
|
|
|
|
slateId,
|
|
|
|
fileId,
|
|
|
|
type,
|
|
|
|
createdAt,
|
|
|
|
});
|
2021-03-07 23:53:54 +03:00
|
|
|
if (count === 10) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished migrating activity table");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// MARK: - adding new fields and reformatting
|
|
|
|
|
|
|
|
const modifySlatesTable = async (testing = false) => {
|
|
|
|
const slates = await DB.select("*").from("slates");
|
|
|
|
for (let slate of slates) {
|
|
|
|
const id = slate.id;
|
|
|
|
const ownerId = slate.data.ownerId;
|
|
|
|
if (!ownerId) {
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(slate);
|
2021-03-07 23:53:54 +03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!testing) {
|
|
|
|
await DB.from("slates").where("id", id).update({ ownerId, isPublic: slate.data.public });
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished modify slates table");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const modifyUsersTable = async (testing = false) => {
|
|
|
|
const users = await DB.select("*").from("users");
|
|
|
|
let count = 0;
|
|
|
|
for (let user of users) {
|
|
|
|
const id = user.id;
|
|
|
|
let data = {
|
|
|
|
photo: user.data.photo,
|
|
|
|
body: user.data.body,
|
|
|
|
name: user.data.name,
|
|
|
|
tokens: user.data.tokens,
|
|
|
|
settings: {
|
|
|
|
allow_automatic_data_storage: user.data.allow_automatic_data_storage,
|
|
|
|
allow_encrypted_data_storage: user.data.allow_encrypted_data_storage,
|
|
|
|
allow_filecoin_directory_listing: user.data.allow_filecoin_directory_listing,
|
|
|
|
settings_deals_auto_approve: user.data.settings_deals_auto_approve,
|
|
|
|
},
|
|
|
|
onboarding: user.data.onboarding,
|
|
|
|
status: user.data.status,
|
|
|
|
};
|
|
|
|
if (testing) {
|
|
|
|
if (count === 10) {
|
|
|
|
return;
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(data);
|
2021-03-07 23:53:54 +03:00
|
|
|
} else {
|
|
|
|
await Data.updateUserById({ id, data });
|
|
|
|
}
|
|
|
|
}
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("finished modify users table");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// MARK: - deleting original data source
|
|
|
|
|
|
|
|
const cleanUsersTable = async () => {
|
|
|
|
const users = await DB.select("*").from("users");
|
|
|
|
for (let user of users) {
|
|
|
|
const id = user.id;
|
2021-05-18 03:59:02 +03:00
|
|
|
let data = {
|
|
|
|
photo: user.data.photo,
|
|
|
|
body: user.data.body,
|
|
|
|
name: user.data.name,
|
|
|
|
tokens: user.data.tokens,
|
|
|
|
settings: user.data.settings,
|
|
|
|
onboarding: user.data.onboarding,
|
|
|
|
status: user.data.status,
|
|
|
|
};
|
2021-06-11 22:25:58 +03:00
|
|
|
// Logging.log(data);
|
2021-05-18 03:59:02 +03:00
|
|
|
await DB.from("users").where("id", id).update({ data });
|
2021-03-07 23:53:54 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const cleanSlatesTable = async () => {
|
|
|
|
const slates = await DB.select("*").from("slates");
|
|
|
|
for (let slate of slates) {
|
2021-05-18 03:59:02 +03:00
|
|
|
let data = {
|
|
|
|
body: slate.data.body,
|
|
|
|
name: slate.data.name,
|
|
|
|
preview: slate.data.preview,
|
|
|
|
tags: slate.data.tags,
|
|
|
|
};
|
2021-08-12 19:00:03 +03:00
|
|
|
|
|
|
|
const { id } = slate;
|
2021-03-07 23:53:54 +03:00
|
|
|
await DB.from("slates").where("id", id).update({ data });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const dropOldTables = async () => {
|
|
|
|
await Promise.all([
|
|
|
|
DB.schema.dropTable("old_activity"),
|
|
|
|
DB.schema.dropTable("trusted"),
|
|
|
|
DB.schema.dropTable("pending"),
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
|
|
|
// MARK: - reset table
|
|
|
|
|
|
|
|
const wipeNewTables = async () => {
|
|
|
|
let numDeleted = await DB("slate_files").del();
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`${numDeleted} deleted from slate_files`);
|
2021-03-07 23:53:54 +03:00
|
|
|
numDeleted = await DB("activity").del();
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`${numDeleted} deleted from activity`);
|
2021-03-07 23:53:54 +03:00
|
|
|
numDeleted = await DB("files").del();
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`${numDeleted} deleted from files`);
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const runScript = async () => {
|
|
|
|
//NOTE(martina): if need to reset
|
|
|
|
// await wipeNewTables();
|
|
|
|
|
|
|
|
//NOTE(martina): before starting, make sure you have all the parameters accounted for
|
2021-04-22 12:26:47 +03:00
|
|
|
// await printUsersTable();
|
|
|
|
// await printSlatesTable();
|
2021-03-07 23:53:54 +03:00
|
|
|
|
|
|
|
//NOTE(martina): add tables
|
2021-05-18 03:59:02 +03:00
|
|
|
// await addTables();
|
2021-03-07 23:53:54 +03:00
|
|
|
|
|
|
|
//NOTE(martina): put data into new tables
|
2021-04-22 12:26:47 +03:00
|
|
|
// await DB("slate_files").del();
|
2021-03-07 23:53:54 +03:00
|
|
|
let testing = false;
|
|
|
|
// await migrateUsersTable(testing);
|
|
|
|
// await migrateSlatesTable(testing);
|
|
|
|
// await migrateActivityTable(testing);
|
|
|
|
|
|
|
|
//NOTE(martina): fill in new fields and reformat
|
2021-04-22 12:26:47 +03:00
|
|
|
// await modifySlatesTable(testing);
|
|
|
|
// await modifyUsersTable(testing);
|
2021-03-07 23:53:54 +03:00
|
|
|
|
|
|
|
//NOTE(martina): once certain you don't need the data anymore, delete the original data
|
2021-05-18 03:59:02 +03:00
|
|
|
// await cleanUsersTable();
|
|
|
|
// await cleanSlatesTable();
|
|
|
|
await dropOldTables();
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log("Finished running. Hit CTRL + C to quit");
|
2021-03-07 23:53:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
runScript();
|