sync.test.js
4.85 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'use strict';
const chai = require('chai'),
Sequelize = require('../../../index'),
expect = chai.expect,
Support = require(__dirname + '/../support');
describe(Support.getTestDialectTeaser('Model'), () => {
describe('sync', () => {
beforeEach(function() {
this.testSync = this.sequelize.define('testSync', {
dummy: Sequelize.STRING
});
return this.testSync.drop();
});
it('should remove a column if it exists in the databases schema but not the model', function() {
const User = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
});
return this.sequelize.sync()
.then(() => {
this.sequelize.define('testSync', {
name: Sequelize.STRING
});
})
.then(() => this.sequelize.sync({alter: true}))
.then(() => User.describe())
.then(data => {
expect(data).to.not.have.ownProperty('age');
expect(data).to.have.ownProperty('name');
});
});
it('should add a column if it exists in the model but not the database', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING
});
return this.sequelize.sync()
.then(() => this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
}))
.then(() => this.sequelize.sync({alter: true}))
.then(() => testSync.describe())
.then(data => expect(data).to.have.ownProperty('age'));
});
it('should change a column if it exists in the model but is different in the database', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.INTEGER
});
return this.sequelize.sync()
.then(() => this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.STRING
}))
.then(() => this.sequelize.sync({alter: true}))
.then(() => testSync.describe())
.then(data => {
expect(data).to.have.ownProperty('age');
expect(data.age.type).to.have.string('CHAR'); // CHARACTER VARYING, VARCHAR(n)
});
});
it('should not alter table if data type does not change', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.STRING
});
return this.sequelize.sync()
.then(() => testSync.create({name: 'test', age: '1'}))
.then(() => this.sequelize.sync({alter: true}))
.then(() => testSync.findOne())
.then(data => {
expect(data.dataValues.name).to.eql('test');
expect(data.dataValues.age).to.eql('1');
});
});
it('should properly create composite index without affecting individual fields', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.STRING
}, {indexes: [{unique: true, fields: ['name', 'age']}]});
return this.sequelize.sync()
.then(() => testSync.create({name: 'test'}))
.then(() => testSync.create({name: 'test2'}))
.then(() => testSync.create({name: 'test3'}))
.then(() => testSync.create({age: '1'}))
.then(() => testSync.create({age: '2'}))
.then(() => testSync.create({name: 'test', age: '1'}))
.then(() => testSync.create({name: 'test', age: '2'}))
.then(() => testSync.create({name: 'test2', age: '2'}))
.then(() => testSync.create({name: 'test3', age: '2'}))
.then(() => testSync.create({name: 'test3', age: '1'}))
.then(data => {
expect(data.dataValues.name).to.eql('test3');
expect(data.dataValues.age).to.eql('1');
});
});
it('should properly create composite index that fails on constraint violation', function() {
const testSync = this.sequelize.define('testSync', {
name: Sequelize.STRING,
age: Sequelize.STRING
}, {indexes: [{unique: true, fields: ['name', 'age']}]});
return this.sequelize.sync()
.then(() => testSync.create({name: 'test', age: '1'}))
.then(() => testSync.create({name: 'test', age: '1'}))
.then(data => expect(data).not.to.be.ok, error => expect(error).to.be.ok);
});
it('should properly alter tables when there are foreign keys', function() {
const foreignKeyTestSyncA = this.sequelize.define('foreignKeyTestSyncA', {
dummy: Sequelize.STRING
});
const foreignKeyTestSyncB = this.sequelize.define('foreignKeyTestSyncB', {
dummy: Sequelize.STRING
});
foreignKeyTestSyncA.hasMany(foreignKeyTestSyncB);
foreignKeyTestSyncB.belongsTo(foreignKeyTestSyncA);
return this.sequelize.sync({ alter: true })
.then(() => this.sequelize.sync({ alter: true }));
});
});
});