summaryrefslogtreecommitdiff
path: root/lib/migrations/20200321153000-fix-account-deletion.js
blob: 3b5a4224a9cfa71f4bb51085d6fdd610192f445b (plain)
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
64
65
66
67
68
'use strict'
const { spawnSync } = require('child_process')
const path = require('path')
module.exports = {
  up: function (queryInterface, Sequelize) {
    const cleanup = spawnSync('./bin/cleanup', { cwd: path.resolve(__dirname, '../../') })
    if (cleanup.status !== 0) {
      throw new Error('Unable to cleanup')
    }
    return queryInterface.addConstraint('Notes', ['ownerId'], {
      type: 'foreign key',
      name: 'Notes_owner_fkey',
      references: {
        table: 'Users',
        field: 'id'
      },
      onDelete: 'cascade'
    }).then(function () {
      return queryInterface.addConstraint('Revisions', ['noteId'], {
        type: 'foreign key',
        name: 'Revisions_note_fkey',
        references: {
          table: 'Notes',
          field: 'id'
        },
        onDelete: 'cascade'
      })
    }).then(function () {
      return queryInterface.addConstraint('Authors', ['noteId'], {
        type: 'foreign key',
        name: 'Author_note_fkey',
        references: {
          table: 'Notes',
          field: 'id'
        },
        onDelete: 'cascade'
      })
    }).then(function () {
      return queryInterface.addConstraint('Authors', ['userId'], {
        type: 'foreign key',
        name: 'Author_user_fkey',
        references: {
          table: 'Users',
          field: 'id'
        },
        onDelete: 'cascade'
      })
    }).catch(function (error) {
      if (error.message.toLowerCase().includes('duplicate key on write or update')) {
        // eslint-disable-next-line no-console
        console.log('Migration has already run… ignoring.')
      } else {
        throw error
      }
    })
  },

  down: function (queryInterface, Sequelize) {
    return queryInterface.removeConstraint('Notes', 'Notes_owner_fkey')
      .then(function () {
        return queryInterface.removeConstraint('Revisions', 'Revisions_note_fkey')
      }).then(function () {
        return queryInterface.removeConstraint('Authors', 'Author_note_fkey')
      }).then(function () {
        return queryInterface.removeConstraint('Authors', 'Author_user_fkey')
      })
  }
}