-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration-dsl.js
180 lines (146 loc) · 5.17 KB
/
migration-dsl.js
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
var _ = require("lodash");
var addPromiseInterface = require('./utils').addPromiseInterface;
//lets you attach further metadata to column definition
//e.g. 'references product(id) on delete cascade'
var getColumnMetadata = function(property){
return _.has(property, 'addSQL') ? property.addSQL : "";
};
//duplicated from sql-ddl-sync Sync closure
var createColumn = function (collection, name, property, Dialect, driver) {
var type = Dialect.getType(collection, property, driver);
if (type === false) {
return false;
}
if (typeof type == "string") {
type = { value : type };
}
var meta = getColumnMetadata(property);
return {
value : Dialect.escapeId(name) + " " + type.value + " " + meta,
before : type.before
};
};
function MigrationDSL(driver) {
this.driver = driver;
this.Dialect = require("sql-ddl-sync").dialect(driver.dialect);
this.Dialect.escapeId = driver.query.escapeId;
}
MigrationDSL.prototype = {
_createColumn: createColumn,
//----- Migration DSL functions
//duplicated and altered from sql-ddl-sync Sync closure
createTable: addPromiseInterface(
function (collectionName, options, cb) {
var columns = [];
var keys = [];
for (var k in options) {
var col = createColumn(collectionName, k, options[k], this.Dialect, this.driver);
if (col === false) {
return cb(new Error("Unknown type for property '" + k + "'"));
}
// `primary` is deprecated in favour of `key`
if (options[k].key || options[k].primary) {
keys.push(k);
}
if (typeof this.Dialect.processKeys == "function") {
keys = this.Dialect.processKeys(keys);
}
columns.push(col.value);
}
this.Dialect.createCollection(this.driver, collectionName, columns, keys, cb);
}
),
renameTable: addPromiseInterface(
function (collectionName, newCollectionName, cb) {
this.Dialect.renameCollection(this.driver, collectionName, newCollectionName, cb);
}
),
addColumn: addPromiseInterface(
function (collectionName, options, cb) {
var columnName = _.keys(options)[0]
var column = this._createColumn(collectionName, columnName, options[columnName], this.Dialect, this.driver);
this.Dialect.addCollectionColumn(this.driver, collectionName, column.value, null, cb);
}
),
renameColumn: addPromiseInterface(
function (collectionName, oldName, newName, cb) {
this.Dialect.renameCollectionColumn(this.driver, collectionName, oldName, newName, cb);
}
),
addIndex: addPromiseInterface(
function (indexName, options, cb) {
this.Dialect.addIndex(this.driver, indexName, options.unique, options.table, options.columns, cb);
}
),
dropIndex: addPromiseInterface(
function (collectionName, indexName, cb) {
this.Dialect.removeIndex(this.driver, indexName, collectionName, cb);
}
),
dropColumn: addPromiseInterface(
function (collectionName, columnName, cb) {
this.Dialect.dropCollectionColumn(this.driver, collectionName, columnName, cb);
}
),
dropTable: addPromiseInterface(
function (collectionName, cb) {
this.Dialect.dropCollection(this.driver, collectionName, cb);
}
),
addPrimaryKey: addPromiseInterface(
function (collectionName, columnName, cb) {
this.Dialect.addPrimaryKey(this.driver, collectionName, columnName, cb);
}
),
dropPrimaryKey: addPromiseInterface(
function (collectionName, columnName, cb) {
this.Dialect.dropPrimaryKey(this.driver, collectionName, columnName, cb);
}
),
addForeignKey: addPromiseInterface(
function (collectionName, options, cb) {
this.Dialect.addForeignKey(this.driver, collectionName, options, cb);
}
),
addForeignKeyConstraint: addPromiseInterface(
function (collectionName, options, cb) {
let onDeleteMethod = "";
switch (options.onDelete) {
case "CASCADE":
onDeleteMethod = "ON DELETE CASCADE";
break;
case "SET_NULL":
onDeleteMethod = "ON DELETE SET NULL";
}
const sql = `ALTER TABLE ?? ADD CONSTRAINT ?? FOREIGN KEY(??) REFERENCES ?? (??) ${onDeleteMethod};`;
this.driver.execQuery(sql, [collectionName, options.constraintName, options.name, options.references.table, options.references.column], cb);
}
),
dropForeignKey: addPromiseInterface(
function (collectionName, columnName, cb) {
this.Dialect.dropForeignKey(this.driver, collectionName, columnName, cb);
}
),
dropForeignKeyConstraint: addPromiseInterface(
function (collectionName, constraintName, cb) {
const sql = "ALTER TABLE ?? DROP CONSTRAINT ??;";
this.driver.execQuery(sql, [collectionName, constraintName], cb);
}
),
hasTable: addPromiseInterface(
function(collectionName, cb) {
this.Dialect.hasCollection(this.driver, collectionName, cb);
}
),
getColumns: addPromiseInterface(
function (collectionName, cb) {
this.Dialect.getCollectionProperties(this.driver, collectionName, cb);
}
),
execQuery: addPromiseInterface(
function (query, args, cb) {
this.driver.execQuery(query, args, cb);
}
)
};
module.exports = MigrationDSL;