]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - scripts/mongo-to-postgre.js
5 // TODO: document this script
7 const program
= require('commander')
8 const eachSeries
= require('async/eachSeries')
9 const series
= require('async/series')
10 const waterfall
= require('async/waterfall')
11 const fs
= require('fs')
12 const path
= require('path')
13 const MongoClient
= require('mongodb').MongoClient
15 const constants
= require('../server/initializers/constants')
18 .option('-mh, --mongo-host [host]', 'MongoDB host', 'localhost')
19 .option('-mp, --mongo-port [weight]', 'MongoDB port', '27017')
20 .option('-md, --mongo-database [dbname]', 'MongoDB database')
23 if (!program
.mongoDatabase
) {
24 console
.error('The mongodb database is mandatory.')
28 const mongoUrl
= 'mongodb://' + program
.mongoHost
+ ':' + program
.mongoPort
+ '/' + program
.mongoDatabase
29 const dbSequelize
= require('../server/initializers/database')
31 console
.log('Connecting to ' + mongoUrl
)
32 MongoClient
.connect(mongoUrl
, function (err
, dbMongo
) {
35 console
.log('Connected to ' + mongoUrl
)
37 const videoMongo
= dbMongo
.collection('videos')
38 const userMongo
= dbMongo
.collection('users')
39 const podMongo
= dbMongo
.collection('pods')
41 podMongo
.count(function (err
, podsLength
) {
45 console
.error('You need to quit friends first.')
49 console
.log('Connecting to ' + dbSequelize
.sequelize
.config
.database
)
50 dbSequelize
.init(true, function (err
) {
53 console
.log('Connected to SQL database %s.', dbSequelize
.sequelize
.config
.database
)
57 dbSequelize
.sequelize
.sync({ force: true }).asCallback(next
)
61 migrateVideos(videoMongo
, dbSequelize
, next
)
65 migrateUsers(userMongo
, dbSequelize
, next
)
68 if (err
) console
.error(err
)
76 // ---------------------------------------------------------------------------
78 function migrateUsers (userMongo
, dbSequelize
, callback
) {
79 userMongo
.find().toArray(function (err
, mongoUsers
) {
80 if (err
) return callback(err
)
82 eachSeries(mongoUsers
, function (mongoUser
, callbackEach
) {
83 console
.log('Migrating user %s', mongoUser
.username
)
86 username: mongoUser
.username
,
87 password: mongoUser
.password
,
94 dbSequelize
.User
.create(userData
, options
).asCallback(callbackEach
)
99 function migrateVideos (videoMongo
, dbSequelize
, finalCallback
) {
100 videoMongo
.find().toArray(function (err
, mongoVideos
) {
101 if (err
) return finalCallback(err
)
103 eachSeries(mongoVideos
, function (mongoVideo
, callbackEach
) {
104 console
.log('Migrating video %s.', mongoVideo
.name
)
108 function startTransaction (callback
) {
109 dbSequelize
.sequelize
.transaction().asCallback(function (err
, t
) {
110 return callback(err
, t
)
114 function findOrCreatePod (t
, callback
) {
115 if (mongoVideo
.remoteId
=== null) return callback(null, t
, null)
119 host: mongoVideo
.podHost
122 host: mongoVideo
.podHost
127 dbSequelize
.Pod
.findOrCreate(query
).asCallback(function (err
, result
) {
128 // [ instance, wasCreated ]
129 return callback(err
, t
, result
[0])
133 function findOrCreateAuthor (t
, pod
, callback
) {
134 const podId
= pod
? pod
.id : null
135 const username
= mongoVideo
.author
149 dbSequelize
.Author
.findOrCreate(query
).asCallback(function (err
, result
) {
150 // [ instance, wasCreated ]
151 return callback(err
, t
, result
[0])
155 function findOrCreateTags (t
, author
, callback
) {
156 const tags
= mongoVideo
.tags
157 const tagInstances
= []
159 eachSeries(tags
, function (tag
, callbackEach
) {
170 dbSequelize
.Tag
.findOrCreate(query
).asCallback(function (err
, res
) {
171 if (err
) return callbackEach(err
)
173 // res = [ tag, isCreated ]
175 tagInstances
.push(tag
)
176 return callbackEach()
179 return callback(err
, t
, author
, tagInstances
)
183 function createVideoObject (t
, author
, tagInstances
, callback
) {
185 name: mongoVideo
.name
,
186 remoteId: mongoVideo
.remoteId
,
187 extname: mongoVideo
.extname
,
188 infoHash: mongoVideo
.magnet
.infoHash
,
189 description: mongoVideo
.description
,
191 duration: mongoVideo
.duration
,
192 createdAt: mongoVideo
.createdDate
195 const video
= dbSequelize
.Video
.build(videoData
)
197 return callback(null, t
, tagInstances
, video
)
200 function moveVideoFile (t
, tagInstances
, video
, callback
) {
201 const basePath
= constants
.CONFIG
.STORAGE
.VIDEOS_DIR
202 const src
= path
.join(basePath
, mongoVideo
._id
.toString()) + video
.extname
203 const dst
= path
.join(basePath
, video
.id
) + video
.extname
204 fs
.rename(src
, dst
, function (err
) {
205 if (err
) return callback(err
)
207 return callback(null, t
, tagInstances
, video
)
211 function insertVideoIntoDB (t
, tagInstances
, video
, callback
) {
216 video
.save(options
).asCallback(function (err
, videoCreated
) {
217 return callback(err
, t
, tagInstances
, videoCreated
)
221 function associateTagsToVideo (t
, tagInstances
, video
, callback
) {
222 const options
= { transaction: t
}
224 video
.setTags(tagInstances
, options
).asCallback(function (err
) {
225 return callback(err
, t
)
229 ], function (err
, t
) {
231 // Abort transaction?
234 return callbackEach(err
)
237 // Commit transaction
240 return callbackEach()