removeColumn.test.js
5.81 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const chai = require('chai');
const expect = chai.expect;
const Support = require('../support');
const DataTypes = require('../../../lib/data-types');
const dialect = Support.getTestDialect();
describe(Support.getTestDialectTeaser('QueryInterface'), () => {
beforeEach(function() {
this.sequelize.options.quoteIdenifiers = true;
this.queryInterface = this.sequelize.getQueryInterface();
});
afterEach(async function() {
await Support.dropTestSchemas(this.sequelize);
});
describe('removeColumn', () => {
describe('(without a schema)', () => {
beforeEach(async function() {
await this.queryInterface.createTable('users', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
firstName: {
type: DataTypes.STRING,
defaultValue: 'Someone'
},
lastName: {
type: DataTypes.STRING
},
manager: {
type: DataTypes.INTEGER,
references: {
model: 'users',
key: 'id'
}
},
email: {
type: DataTypes.STRING,
unique: true
}
});
});
it('should be able to remove a column with a default value', async function() {
await this.queryInterface.removeColumn('users', 'firstName');
const table = await this.queryInterface.describeTable('users');
expect(table).to.not.have.property('firstName');
});
it('should be able to remove a column without default value', async function() {
await this.queryInterface.removeColumn('users', 'lastName');
const table = await this.queryInterface.describeTable('users');
expect(table).to.not.have.property('lastName');
});
it('should be able to remove a column with a foreign key constraint', async function() {
await this.queryInterface.removeColumn('users', 'manager');
const table = await this.queryInterface.describeTable('users');
expect(table).to.not.have.property('manager');
});
it('should be able to remove a column with primaryKey', async function() {
await this.queryInterface.removeColumn('users', 'manager');
const table0 = await this.queryInterface.describeTable('users');
expect(table0).to.not.have.property('manager');
await this.queryInterface.removeColumn('users', 'id');
const table = await this.queryInterface.describeTable('users');
expect(table).to.not.have.property('id');
});
// From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', async function() {
await this.queryInterface.removeColumn('users', 'email');
const table = await this.queryInterface.describeTable('users');
expect(table).to.not.have.property('email');
});
}
});
describe('(with a schema)', () => {
beforeEach(async function() {
await this.sequelize.createSchema('archive');
await this.queryInterface.createTable({
tableName: 'users',
schema: 'archive'
}, {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true
},
firstName: {
type: DataTypes.STRING,
defaultValue: 'Someone'
},
lastName: {
type: DataTypes.STRING
},
email: {
type: DataTypes.STRING,
unique: true
}
});
});
it('should be able to remove a column with a default value', async function() {
await this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'firstName'
);
const table = await this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
expect(table).to.not.have.property('firstName');
});
it('should be able to remove a column without default value', async function() {
await this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'lastName'
);
const table = await this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
expect(table).to.not.have.property('lastName');
});
it('should be able to remove a column with primaryKey', async function() {
await this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'id');
const table = await this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
expect(table).to.not.have.property('id');
});
// From MSSQL documentation on ALTER COLUMN:
// The modified column cannot be any one of the following:
// - Used in a CHECK or UNIQUE constraint.
// https://docs.microsoft.com/en-us/sql/t-sql/statements/alter-table-transact-sql#arguments
if (dialect !== 'mssql') {
it('should be able to remove a column with unique contraint', async function() {
await this.queryInterface.removeColumn({
tableName: 'users',
schema: 'archive'
}, 'email');
const table = await this.queryInterface.describeTable({
tableName: 'users',
schema: 'archive'
});
expect(table).to.not.have.property('email');
});
}
});
});
});