Skip to content

Commit df04afa

Browse files
committed
Fix drop/build primary indexes
1 parent 0740935 commit df04afa

2 files changed

Lines changed: 60 additions & 7 deletions

File tree

lib/ibmdb.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -851,27 +851,62 @@ IBMDB.prototype.buildIndexes = function(model) {
851851
var indexClauses = [];
852852
var definition = this.getModelDefinition(model);
853853
var indexes = definition.settings.indexes || {};
854+
/*!
855+
This module did not allow to define indexes the "new" way loopback wants to.
856+
- The new way to define indexes in loopback
857+
(https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html#indexes)
858+
"name_key": {
859+
"columns": "name",
860+
"unique": true
861+
}
862+
- The way the module previously accepted indexes:
863+
"name_key": {
864+
"keys" : {
865+
"name": 1
866+
}
867+
}
868+
The module now allows both ways to define the indexes.
869+
*/
854870
// Build model level indexes
855871
for (var index in indexes) {
856872
var i = indexes[index];
857873
var statement = new ParameterizedSQL('CREATE');
858874
if (i.kind) {
859875
statement.merge(i.kind);
860-
} else if (i.unique) {
876+
} else if ((i.options && i.options.unique && i.options.unique === true)
877+
|| i.unique) {
878+
// if index unique indicator is configured
861879
statement.merge('UNIQUE');
862880
}
863-
864-
var columns = i.columns.split(',').map(function(val) {
865-
return self.escapeName(val);
866-
});
881+
var indexedColumns = [];
882+
var columns = '';
883+
// if indexes are configured as "keys"
884+
if (i.keys) {
885+
// for each field in "keys" object
886+
for (var key in i.keys) {
887+
// index in asc order
888+
if (i.keys[key] !== -1) {
889+
indexedColumns.push(key);
890+
} else {
891+
// index in desc order
892+
indexedColumns.push(key + ' DESC');
893+
}
894+
}
895+
}
896+
if (indexedColumns.length) {
897+
columns = indexedColumns.join(',');
898+
} else if (i.columns) {
899+
columns = i.columns.split(',').map(function(val) {
900+
return self.escapeName(val);
901+
});
902+
}
867903

868904
statement.merge('INDEX ' + self.escapeName(index) + ' ON ');
869905
statement.merge(self.schema + '.' + self.tableEscaped(model));
870906
statement.merge('(' + columns + ')');
871907

872908
indexClauses.push(statement.sql);
873909
}
874-
875910
return indexClauses;
876911
};
877912

lib/migration.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,21 @@ module.exports = function(IBMDB) {
124124
};
125125

126126
IBMDB.prototype.dropIndexes = function(model, actualIndexes) {
127+
var self = this;
127128
var operations = [];
128129

129130
// If there are indexes for this table then we need to drop them.
130131
// Generate the statements here to drop all known indexes.
131132
if (actualIndexes) {
132133
actualIndexes.forEach(function(i) {
133-
var stmt = 'DROP INDEX ' + i.INDNAME;
134+
var isPrimaryIndex = i.UNIQUERULE === 'P';
135+
var stmt;
136+
if (isPrimaryIndex) {
137+
stmt = 'ALTER TABLE ' + self.schema + '.' +
138+
self.tableEscaped(model) + ' ' + 'DROP PRIMARY KEY';
139+
} else {
140+
stmt = 'DROP INDEX "' + i.INDNAME + '"';
141+
}
134142
operations.push(stmt);
135143
});
136144
}
@@ -145,6 +153,9 @@ module.exports = function(IBMDB) {
145153
var self = this;
146154
var sql = [];
147155
var tasks = [];
156+
var pks = this.idNames(model).map(function(i) {
157+
return self.columnEscaped(model, i);
158+
});
148159

149160
// Create the statements to drop all existing indexes before we start
150161
// altering the table.
@@ -166,6 +177,13 @@ module.exports = function(IBMDB) {
166177
// ------------------------------------------------------------------------
167178
sql = sql.concat(self.buildIndexes(model));
168179

180+
// add back the primary key index
181+
async.forEach(pks, function(pk) {
182+
sql.push('ALTER TABLE ' + self.schema + '.' +
183+
self.tableEscaped(model) + ' ' + 'ADD PRIMARY KEY (' +
184+
pk + ')');
185+
});
186+
169187
sql.forEach(function(i) {
170188
tasks.push(function(cb) {
171189
self.execute(i, function(err, results) {

0 commit comments

Comments
 (0)