forked from gimenete/sequelize-sync-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
72 lines (66 loc) · 2.4 KB
/
index.js
File metadata and controls
72 lines (66 loc) · 2.4 KB
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
69
70
71
72
var dbdiff = require('dbdiff')
module.exports = function(Sequelize) {
if (Sequelize.prototype.syncDiff) return
var sequelizeMethods = ['define']
var modelMethods = ['belongsTo', 'hasOne', 'hasMany', 'belongsToMany']
sequelizeMethods.forEach(function(name) {
var func = Sequelize.prototype[name]
Sequelize.prototype[name] = function() {
var args = Array.prototype.slice.call(arguments)
this.diff_actions = this.diff_actions || []
this.diff_actions.push({ method: name, args: args })
return func.apply(this, args)
}
})
modelMethods.forEach(function(name) {
var func = Sequelize.Model.prototype[name]
Sequelize.Model.prototype[name] = function() {
var args = Array.prototype.slice.call(arguments)
var sequelize = this.sequelize
sequelize.diff_actions = sequelize.diff_actions || []
sequelize.diff_actions.push({ method: name, args: args, model: this.name })
return func.apply(this, args)
}
})
Sequelize.prototype.syncDiff = function(database, username, password, options) {
var self = this
var sequelize = new Sequelize(database, username, password, options || this.options)
this.diff_actions.forEach(function(action) {
if (!action.model) {
sequelize[action.method].apply(sequelize, action.args)
} else {
var model = sequelize.model(action.model)
action.args[0] = sequelize.model(action.args[0].name)
model[action.method].apply(model, action.args)
}
})
return sequelize.sync({ force: true })
.then(function() {
var arr = []
dbdiff.logger = function(msg) {
arr.push(msg)
}
var masterConfig = {
host: self.config.host,
port: self.config.port,
user: self.config.username,
password: self.config.password,
database: self.config.database,
ssl: self.config.ssl
};
var dummyConfig = {
host: sequelize.config.host,
port: sequelize.config.port,
user: sequelize.config.username,
password: sequelize.config.password,
database: sequelize.config.database,
ssl: sequelize.config.ssl
};
return new Promise(function (resolve, reject) {
dbdiff.compareDatabases(masterConfig, dummyConfig, function(err) {
err ? reject(err) : resolve(arr.join('\n'))
})
})
})
}
}