]>
Commit | Line | Data |
---|---|---|
00d6a41e C |
1 | #!/usr/bin/env node |
2 | ||
3 | 'use strict' | |
4 | ||
5 | // TODO: document this script | |
6 | ||
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 | |
14 | ||
15 | const constants = require('../server/initializers/constants') | |
16 | ||
17 | program | |
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') | |
21 | .parse(process.argv) | |
22 | ||
23 | if (!program.mongoDatabase) { | |
24 | console.error('The mongodb database is mandatory.') | |
25 | process.exit(-1) | |
26 | } | |
27 | ||
28 | const mongoUrl = 'mongodb://' + program.mongoHost + ':' + program.mongoPort + '/' + program.mongoDatabase | |
29 | const dbSequelize = require('../server/initializers/database') | |
30 | ||
31 | console.log('Connecting to ' + mongoUrl) | |
32 | MongoClient.connect(mongoUrl, function (err, dbMongo) { | |
33 | if (err) throw err | |
34 | ||
35 | console.log('Connected to ' + mongoUrl) | |
36 | ||
37 | const videoMongo = dbMongo.collection('videos') | |
38 | const userMongo = dbMongo.collection('users') | |
39 | const podMongo = dbMongo.collection('pods') | |
40 | ||
41 | podMongo.count(function (err, podsLength) { | |
42 | if (err) throw err | |
43 | ||
44 | if (podsLength > 0) { | |
45 | console.error('You need to quit friends first.') | |
46 | process.exit(-1) | |
47 | } | |
48 | ||
49 | console.log('Connecting to ' + dbSequelize.sequelize.config.database) | |
50 | dbSequelize.init(true, function (err) { | |
51 | if (err) throw err | |
52 | ||
53 | console.log('Connected to SQL database %s.', dbSequelize.sequelize.config.database) | |
54 | ||
55 | series([ | |
56 | function (next) { | |
57 | dbSequelize.sequelize.sync({ force: true }).asCallback(next) | |
58 | }, | |
59 | ||
60 | function (next) { | |
61 | migrateVideos(videoMongo, dbSequelize, next) | |
62 | }, | |
63 | ||
64 | function (next) { | |
65 | migrateUsers(userMongo, dbSequelize, next) | |
66 | } | |
67 | ], function (err) { | |
68 | if (err) console.error(err) | |
69 | ||
70 | process.exit(0) | |
71 | }) | |
72 | }) | |
73 | }) | |
74 | }) | |
75 | ||
76 | // --------------------------------------------------------------------------- | |
77 | ||
78 | function migrateUsers (userMongo, dbSequelize, callback) { | |
79 | userMongo.find().toArray(function (err, mongoUsers) { | |
80 | if (err) return callback(err) | |
81 | ||
82 | eachSeries(mongoUsers, function (mongoUser, callbackEach) { | |
83 | console.log('Migrating user %s', mongoUser.username) | |
84 | ||
85 | const userData = { | |
86 | username: mongoUser.username, | |
87 | password: mongoUser.password, | |
88 | role: mongoUser.role | |
89 | } | |
90 | const options = { | |
91 | hooks: false | |
92 | } | |
93 | ||
94 | dbSequelize.User.create(userData, options).asCallback(callbackEach) | |
95 | }, callback) | |
96 | }) | |
97 | } | |
98 | ||
99 | function migrateVideos (videoMongo, dbSequelize, finalCallback) { | |
100 | videoMongo.find().toArray(function (err, mongoVideos) { | |
101 | if (err) return finalCallback(err) | |
102 | ||
103 | eachSeries(mongoVideos, function (mongoVideo, callbackEach) { | |
104 | console.log('Migrating video %s.', mongoVideo.name) | |
105 | ||
106 | waterfall([ | |
107 | ||
108 | function startTransaction (callback) { | |
109 | dbSequelize.sequelize.transaction().asCallback(function (err, t) { | |
110 | return callback(err, t) | |
111 | }) | |
112 | }, | |
113 | ||
114 | function findOrCreatePod (t, callback) { | |
115 | if (mongoVideo.remoteId === null) return callback(null, t, null) | |
116 | ||
117 | const query = { | |
118 | where: { | |
119 | host: mongoVideo.podHost | |
120 | }, | |
121 | defaults: { | |
122 | host: mongoVideo.podHost | |
123 | }, | |
124 | transaction: t | |
125 | } | |
126 | ||
127 | dbSequelize.Pod.findOrCreate(query).asCallback(function (err, result) { | |
128 | // [ instance, wasCreated ] | |
129 | return callback(err, t, result[0]) | |
130 | }) | |
131 | }, | |
132 | ||
133 | function findOrCreateAuthor (t, pod, callback) { | |
134 | const podId = pod ? pod.id : null | |
135 | const username = mongoVideo.author | |
136 | ||
137 | const query = { | |
138 | where: { | |
139 | podId, | |
140 | name: username | |
141 | }, | |
142 | defaults: { | |
143 | podId, | |
144 | name: username | |
145 | }, | |
146 | transaction: t | |
147 | } | |
148 | ||
149 | dbSequelize.Author.findOrCreate(query).asCallback(function (err, result) { | |
150 | // [ instance, wasCreated ] | |
151 | return callback(err, t, result[0]) | |
152 | }) | |
153 | }, | |
154 | ||
155 | function findOrCreateTags (t, author, callback) { | |
156 | const tags = mongoVideo.tags | |
157 | const tagInstances = [] | |
158 | ||
159 | eachSeries(tags, function (tag, callbackEach) { | |
160 | const query = { | |
161 | where: { | |
162 | name: tag | |
163 | }, | |
164 | defaults: { | |
165 | name: tag | |
166 | }, | |
167 | transaction: t | |
168 | } | |
169 | ||
170 | dbSequelize.Tag.findOrCreate(query).asCallback(function (err, res) { | |
171 | if (err) return callbackEach(err) | |
172 | ||
173 | // res = [ tag, isCreated ] | |
174 | const tag = res[0] | |
175 | tagInstances.push(tag) | |
176 | return callbackEach() | |
177 | }) | |
178 | }, function (err) { | |
179 | return callback(err, t, author, tagInstances) | |
180 | }) | |
181 | }, | |
182 | ||
183 | function createVideoObject (t, author, tagInstances, callback) { | |
184 | const videoData = { | |
185 | name: mongoVideo.name, | |
186 | remoteId: mongoVideo.remoteId, | |
187 | extname: mongoVideo.extname, | |
188 | infoHash: mongoVideo.magnet.infoHash, | |
189 | description: mongoVideo.description, | |
190 | authorId: author.id, | |
191 | duration: mongoVideo.duration, | |
192 | createdAt: mongoVideo.createdDate | |
193 | } | |
194 | ||
195 | const video = dbSequelize.Video.build(videoData) | |
196 | ||
197 | return callback(null, t, tagInstances, video) | |
198 | }, | |
199 | ||
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) | |
206 | ||
207 | return callback(null, t, tagInstances, video) | |
208 | }) | |
209 | }, | |
210 | ||
211 | function insertVideoIntoDB (t, tagInstances, video, callback) { | |
212 | const options = { | |
213 | transaction: t | |
214 | } | |
215 | ||
216 | video.save(options).asCallback(function (err, videoCreated) { | |
217 | return callback(err, t, tagInstances, videoCreated) | |
218 | }) | |
219 | }, | |
220 | ||
221 | function associateTagsToVideo (t, tagInstances, video, callback) { | |
222 | const options = { transaction: t } | |
223 | ||
224 | video.setTags(tagInstances, options).asCallback(function (err) { | |
225 | return callback(err, t) | |
226 | }) | |
227 | } | |
228 | ||
229 | ], function (err, t) { | |
230 | if (err) { | |
231 | // Abort transaction? | |
232 | if (t) t.rollback() | |
233 | ||
234 | return callbackEach(err) | |
235 | } | |
236 | ||
237 | // Commit transaction | |
238 | t.commit() | |
239 | ||
240 | return callbackEach() | |
241 | }) | |
242 | }, finalCallback) | |
243 | }) | |
244 | } |