From 56860a6bef84b0e3c0c011f32720e273ece8b36d Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 8 Apr 2019 20:33:42 +0900 Subject: [PATCH] wip migration --- package.json | 5 +++++ src/migrate.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/migrate.ts diff --git a/package.json b/package.json index a988368b21..cc71744971 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "scripts": { "start": "node ./index.js", "init": "node ./built/init.js", + "migrate": "node ./built/migrate.js", "debug": "DEBUG=misskey:* node ./index.js", "build": "webpack && gulp build", "webpack": "webpack", @@ -66,6 +67,8 @@ "@types/lolex": "3.1.1", "@types/minio": "7.0.1", "@types/mocha": "5.2.6", + "@types/mongodb": "3.1.22", + "@types/monk": "6.0.0", "@types/node": "11.10.4", "@types/nodemailer": "4.6.6", "@types/nprogress": "0.0.29", @@ -168,6 +171,8 @@ "mocha": "6.0.2", "moji": "0.5.1", "moment": "2.24.0", + "mongodb": "3.2.3", + "monk": "6.0.6", "ms": "2.1.1", "nested-property": "0.0.7", "node-fetch": "2.3.0", diff --git a/src/migrate.ts b/src/migrate.ts new file mode 100644 index 0000000000..4e88642e88 --- /dev/null +++ b/src/migrate.ts @@ -0,0 +1,43 @@ +import mongo from 'monk'; +import config from './config'; +import { initDb } from './db/postgre'; +import { User } from './models/entities/user'; +import { getRepository } from 'typeorm'; +import generateUserToken from './server/api/common/generate-native-user-token'; + +const u = (config as any).mongodb.user ? encodeURIComponent((config as any).mongodb.user) : null; +const p = (config as any).mongodb.pass ? encodeURIComponent((config as any).mongodb.pass) : null; + +const uri = `mongodb://${u && p ? `${u}:${p}@` : ''}${(config as any).mongodb.host}:${(config as any).mongodb.port}/${(config as any).mongodb.db}`; + +const db = mongo(uri); + +const _User = db.get('users'); + +async function main() { + await initDb(); + const Users = getRepository(User); + + const allUsersCount = await _User.count(); + + for (let i = 0; i < allUsersCount; i++) { + const user = await _User.findOne({}, { + skip: i + }); + await Users.save({ + id: user._id.toHexString(), + createdAt: user.createdAt || new Date(), + username: user.username, + usernameLower: user.username.toLowerCase(), + host: user.host, + token: generateUserToken(), + password: user.password, + isAdmin: user.isAdmin, + autoAcceptFollowed: true, + autoWatch: false + }); + console.log(`USER (${i + 1}/${allUsersCount}) ${user.id} DONE`); + } +} + +main();