From abd7a1ca2e058854e6335b54f4119f009961a5ab Mon Sep 17 00:00:00 2001 From: Pitu Date: Thu, 25 Mar 2021 00:09:33 +0900 Subject: chore: move database migration script --- src/api/databaseMigration.js | 140 ----------------------------------- src/api/scripts/databaseMigration.js | 135 +++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 140 deletions(-) delete mode 100644 src/api/databaseMigration.js create mode 100644 src/api/scripts/databaseMigration.js (limited to 'src/api') diff --git a/src/api/databaseMigration.js b/src/api/databaseMigration.js deleted file mode 100644 index 1e3518e..0000000 --- a/src/api/databaseMigration.js +++ /dev/null @@ -1,140 +0,0 @@ -require('dotenv').config(); - -const nodePath = require('path'); -const moment = require('moment'); -const jetpack = require('fs-jetpack'); -const ThumbUtil = require('./utils/ThumbUtil'); - -const oldDb = require('knex')({ - client: 'sqlite3', - connection: { - filename: nodePath.join(__dirname, '../../', 'db') - }, - useNullAsDefault: true -}); - -const newDb = require('knex')({ - client: 'sqlite3', - connection: { - filename: nodePath.join(__dirname, '../../database/', 'database.sqlite') - }, - postProcessResponse: result => { - const booleanFields = [ - 'enabled', - 'enableDownload', - 'isAdmin', - 'nsfw' - ]; - - const processResponse = row => { - Object.keys(row).forEach(key => { - if (booleanFields.includes(key)) { - if (row[key] === 0) row[key] = false; - else if (row[key] === 1) row[key] = true; - } - }); - return row; - }; - - if (Array.isArray(result)) return result.map(row => processResponse(row)); - if (typeof result === 'object') return processResponse(result); - return result; - }, - useNullAsDefault: true -}); - -const start = async () => { - console.log('Starting migration, this may take a few minutes...'); // Because I half assed it - console.log('Please do NOT kill the process. Wait for it to finish.'); - - await jetpack.removeAsync(nodePath.join(__dirname, '../../uploads/thumbs')); - await jetpack.dirAsync(nodePath.join(__dirname, '../../uploads/thumbs/square')); - console.log('Finished deleting old thumbnails to create new ones'); - - const users = await oldDb.table('users').where('username', '<>', 'root'); - for (const user of users) { - const now = moment.utc().toDate(); - const userToInsert = { - id: user.id, - username: user.username, - password: user.password, - enabled: user.enabled, - isAdmin: false, - apiKey: user.token, - passwordEditedAt: now, - apiKeyEditedAt: now, - createdAt: now, - editedAt: now - }; - await newDb.table('users').insert(userToInsert); - } - console.log('Finished migrating users...'); - - const albums = await oldDb.table('albums'); - for (const album of albums) { - if (!album.enabled || album.enabled == 0) continue; - const now = moment.utc().toDate(); - const albumToInsert = { - id: album.id, - userId: album.userid, - name: album.name, - zippedAt: album.zipGeneratedAt ? moment.unix(album.zipGeneratedAt).toDate() : null, - createdAt: moment.unix(album.timestamp).toDate(), - editedAt: moment.unix(album.editedAt).toDate() - }; - const linkToInsert = { - userId: album.userid, - albumId: album.id, - identifier: album.identifier, - views: 0, - enabled: true, - enableDownload: true, - createdAt: now, - editedAt: now - }; - await newDb.table('albums').insert(albumToInsert); - const insertedId = await newDb.table('links').insert(linkToInsert); - await newDb.table('albumsLinks').insert({ - albumId: album.id, - linkId: insertedId[0] - }); - } - console.log('Finished migrating albums...'); - - const files = await oldDb.table('files'); - const filesToInsert = []; - const albumsFilesToInsert = []; - for (const file of files) { - const fileToInsert = { - id: file.id, - userId: file.userid, - name: file.name, - original: file.original, - type: file.type, - size: file.size, - hash: file.hash, - ip: file.ip, - createdAt: moment.unix(file.timestamp).toDate(), - editedAt: moment.unix(file.timestamp).toDate() - }; - filesToInsert.push(fileToInsert); - if (file.albumid) { - albumsFilesToInsert.push({ - albumId: file.albumid, - fileId: file.id - }); - } - - const filename = file.name; - if (!jetpack.exists(nodePath.join(__dirname, '../../uploads', filename))) continue; - ThumbUtil.generateThumbnails(filename); - } - await newDb.batchInsert('files', filesToInsert, 20); - await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); - console.log('Finished migrating files...'); - - console.log('Finished migrating everything. '); - process.exit(0); -}; - -start(); diff --git a/src/api/scripts/databaseMigration.js b/src/api/scripts/databaseMigration.js new file mode 100644 index 0000000..8fab0ac --- /dev/null +++ b/src/api/scripts/databaseMigration.js @@ -0,0 +1,135 @@ +require('dotenv').config(); + +const nodePath = require('path'); +const moment = require('moment'); +const jetpack = require('fs-jetpack'); +const ThumbUtil = require('../utils/ThumbUtil'); + +const oldDb = require('knex')({ + client: 'sqlite3', + connection: { + filename: nodePath.join(__dirname, '../../', 'db') + }, + useNullAsDefault: true +}); + +const newDb = require('knex')({ + client: 'sqlite3', + connection: { + filename: nodePath.join(__dirname, '../../database/', 'database.sqlite') + }, + postProcessResponse: result => { + const booleanFields = ['enabled', 'enableDownload', 'isAdmin', 'nsfw', 'generateZips', 'publicMode', 'userAccounts']; + + const processResponse = row => { + Object.keys(row).forEach(key => { + if (booleanFields.includes(key)) { + if (row[key] === 0) row[key] = false; + else if (row[key] === 1) row[key] = true; + } + }); + return row; + }; + + if (Array.isArray(result)) return result.map(row => processResponse(row)); + if (typeof result === 'object') return processResponse(result); + return result; + }, + useNullAsDefault: true +}); + +const start = async () => { + console.log('Starting migration, this may take a few minutes...'); // Because I half assed it + console.log('Please do NOT kill the process. Wait for it to finish.'); + + await jetpack.removeAsync(nodePath.join(__dirname, '../../uploads/thumbs')); + await jetpack.dirAsync(nodePath.join(__dirname, '../../uploads/thumbs/square')); + console.log('Finished deleting old thumbnails to create new ones'); + + const users = await oldDb.table('users').where('username', '<>', 'root'); + for (const user of users) { + const now = moment.utc().toDate(); + const userToInsert = { + id: user.id, + username: user.username, + password: user.password, + enabled: user.enabled, + isAdmin: false, + apiKey: user.token, + passwordEditedAt: now, + apiKeyEditedAt: now, + createdAt: now, + editedAt: now + }; + await newDb.table('users').insert(userToInsert); + } + console.log('Finished migrating users...'); + + const albums = await oldDb.table('albums'); + for (const album of albums) { + if (!album.enabled || album.enabled == 0) continue; + const now = moment.utc().toDate(); + const albumToInsert = { + id: album.id, + userId: album.userid, + name: album.name, + zippedAt: album.zipGeneratedAt ? moment.unix(album.zipGeneratedAt).toDate() : null, + createdAt: moment.unix(album.timestamp).toDate(), + editedAt: moment.unix(album.editedAt).toDate() + }; + const linkToInsert = { + userId: album.userid, + albumId: album.id, + identifier: album.identifier, + views: 0, + enabled: true, + enableDownload: true, + createdAt: now, + editedAt: now + }; + await newDb.table('albums').insert(albumToInsert); + const insertedId = await newDb.table('links').insert(linkToInsert); + await newDb.table('albumsLinks').insert({ + albumId: album.id, + linkId: insertedId[0] + }); + } + console.log('Finished migrating albums...'); + + const files = await oldDb.table('files'); + const filesToInsert = []; + const albumsFilesToInsert = []; + for (const file of files) { + const fileToInsert = { + id: file.id, + userId: file.userid, + name: file.name, + original: file.original, + type: file.type, + size: file.size, + hash: file.hash, + ip: file.ip, + createdAt: moment.unix(file.timestamp).toDate(), + editedAt: moment.unix(file.timestamp).toDate() + }; + filesToInsert.push(fileToInsert); + if (file.albumid) { + albumsFilesToInsert.push({ + albumId: file.albumid, + fileId: file.id + }); + } + + const filename = file.name; + if (!jetpack.exists(nodePath.join(__dirname, '../../uploads', filename))) continue; + ThumbUtil.generateThumbnails(filename); + } + await newDb.batchInsert('files', filesToInsert, 20); + await newDb.batchInsert('albumsFiles', albumsFilesToInsert, 20); + console.log('Finished migrating files...'); + + console.log('Finished migrating everything. '); + process.exit(0); +}; + +start(); -- cgit v1.2.3