aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/initializers/migrations/0040-video-remote-id.js
blob: 5cf856b2e3835d5753b6e1f88d8272bc98db5fcc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
  Use remote id as identifier
*/

const each = require('async/each')
const map = require('lodash/map')
const mongoose = require('mongoose')
const readline = require('readline')

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
})

const logger = require('../../helpers/logger')
const friends = require('../../lib/friends')

const Pod = mongoose.model('Pod')
const Video = mongoose.model('Video')

exports.up = function (callback) {
  Pod.find({}).lean().exec(function (err, pods) {
    if (err) return callback(err)

    // We need to quit friends first
    if (pods.length === 0) {
      return setVideosRemoteId(callback)
    }

    const timeout = setTimeout(function () {
      throw new Error('You need to enter a value!')
    }, 10000)

    rl.question('I am sorry but I need to quit friends for upgrading. Do you want to continue? (yes/*)', function (answer) {
      if (answer !== 'yes') throw new Error('I cannot continue.')

      clearTimeout(timeout)
      rl.close()

      const urls = map(pods, 'url')
      logger.info('Saying goodbye to: ' + urls.join(', '))

      friends.quitFriends(function () {
        setVideosRemoteId(callback)
      })
    })
  })
}

exports.down = function (callback) {
  throw new Error('Not implemented.')
}

function setVideosRemoteId (callback) {
  Video.find({}, function (err, videos) {
    if (err) return callback(err)

    each(videos, function (video, callbackEach) {
      video.remoteId = null
      video.save(callbackEach)
    }, callback)
  })
}