1 | 'use strict'
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var EventEmitter = require('events')
|
14 | var Migration = require('./migration')
|
15 | var migrate = require('./migrate')
|
16 | var inherits = require('inherits')
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | module.exports = MigrationSet
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | function MigrationSet (store) {
|
33 | this.store = store
|
34 | this.migrations = []
|
35 | this.map = {}
|
36 | this.lastRun = null
|
37 | };
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | inherits(MigrationSet, EventEmitter)
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 | MigrationSet.prototype.addMigration = function (title, up, down) {
|
55 | var migration
|
56 | if (!(title instanceof Migration)) {
|
57 | migration = new Migration(title, up, down)
|
58 | } else {
|
59 | migration = title
|
60 | }
|
61 |
|
62 |
|
63 | if (this.map[migration.title]) {
|
64 | this.map[migration.title].up = migration.up
|
65 | this.map[migration.title].down = migration.down
|
66 | this.map[migration.title].description = migration.description
|
67 | return
|
68 | }
|
69 |
|
70 | this.migrations.push(migration)
|
71 | this.map[migration.title] = migration
|
72 | }
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | MigrationSet.prototype.save = function (fn) {
|
81 | this.store.save(this, (err) => {
|
82 | if (err) return fn(err)
|
83 | this.emit('save')
|
84 | fn(null)
|
85 | })
|
86 | }
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | MigrationSet.prototype.down = function (migrationName, fn) {
|
96 | this.migrate('down', migrationName, fn)
|
97 | }
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | MigrationSet.prototype.up = function (migrationName, fn) {
|
107 | this.migrate('up', migrationName, fn)
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | MigrationSet.prototype.migrate = function (direction, migrationName, fn) {
|
119 | if (typeof migrationName === 'function') {
|
120 | fn = migrationName
|
121 | migrationName = null
|
122 | }
|
123 | migrate(this, direction, migrationName, fn)
|
124 | }
|