@@ -123,129 +123,19 @@ module.exports = function(IBMDB) {
123123 return operations ;
124124 } ;
125125
126- IBMDB . prototype . addIndexes = function ( model , actualIndexes ) {
127- var ai = { } ;
128- var self = this ;
129- var m = this . getModelDefinition ( model ) ;
130- var indexes = m . settings . indexes || { } ;
131- var indexNames = Object . keys ( indexes ) . filter ( function ( name ) {
132- return ! ! m . settings . indexes [ name ] ;
133- } ) ;
126+ IBMDB . prototype . dropIndexes = function ( model , actualIndexes ) {
134127 var operations = [ ] ;
135- var propNames = Object . keys ( m . properties ) . filter ( function ( name ) {
136- return ! ! m . properties [ name ] ;
137- } ) ;
138- var sql = [ ] ;
139- var type = '' ;
140128
129+ // If there are indexes for this table then we need to drop them.
130+ // Generate the statements here to drop all known indexes.
141131 if ( actualIndexes ) {
142132 actualIndexes . forEach ( function ( i ) {
143- var name = i . INDNAME ;
144- if ( ! ai [ name ] ) {
145- ai [ name ] = {
146- info : i ,
147- columns : [ ] ,
148- } ;
149- }
150-
151- i . COLNAMES . split ( / \+ \s * / ) . forEach ( function ( columnName , j ) {
152- // This is a bit of a dirty way to get around this but DB2 returns
153- // column names as a string started with and separated by a '+'.
154- // The code below will strip out the initial '+' then store the
155- // actual column names.
156- if ( j > 0 )
157- ai [ name ] . columns [ j - 1 ] = columnName ;
158- } ) ;
133+ var stmt = 'DROP INDEX ' + i . INDNAME ;
134+ operations . push ( stmt ) ;
159135 } ) ;
160136 }
161- var aiNames = Object . keys ( ai ) ;
162- // remove indexes
163- aiNames . forEach ( function ( indexName ) {
164- if ( ai [ indexName ] . info . UNIQUERULE === 'P' || // indexName === 'PRIMARY' ||
165- ( m . properties [ indexName ] && self . id ( model , indexName ) ) ) return ;
166137
167- if ( indexNames . indexOf ( indexName ) === - 1 && ! m . properties [ indexName ] ||
168- m . properties [ indexName ] && ! m . properties [ indexName ] . index ) {
169- if ( ai [ indexName ] . info . UNIQUERULE === 'P' ) {
170- operations . push ( 'DROP PRIMARY KEY' ) ;
171- } else if ( ai [ indexName ] . info . UNIQUERULE === 'U' ) {
172- operations . push ( 'DROP UNIQUE ' + indexName ) ;
173- }
174- } else {
175- // first: check single (only type and kind)
176- if ( m . properties [ indexName ] && ! m . properties [ indexName ] . index ) {
177- // TODO
178- return ;
179- }
180- // second: check multiple indexes
181- var orderMatched = true ;
182- if ( indexNames . indexOf ( indexName ) !== - 1 ) {
183- m . settings . indexes [ indexName ] . columns . split ( / , \s * / ) . forEach (
184- function ( columnName , i ) {
185- if ( ai [ indexName ] . columns [ i ] !== columnName )
186- orderMatched = false ;
187- } ) ;
188- }
189-
190- if ( ! orderMatched ) {
191- if ( ai [ indexName ] . info . UNIQUERULE === 'P' ) {
192- operations . push ( 'DROP PRIMARY KEY' ) ;
193- } else if ( ai [ indexName ] . info . UNIQUERULE === 'U' ) {
194- operations . push ( 'DROP UNIQUE ' + indexName ) ;
195- }
196-
197- delete ai [ indexName ] ;
198- }
199- }
200- } ) ;
201-
202- if ( operations . length ) {
203- // Add the ALTER TABLE statement to the list of tasks to perform later.
204- sql . push ( 'ALTER TABLE ' + self . schema + '.' +
205- self . tableEscaped ( model ) + ' ' + operations . join ( ' ' ) + ';' ) ;
206- }
207-
208- // add single-column indexes
209- propNames . forEach ( function ( propName ) {
210- var i = m . properties [ propName ] . index ;
211- if ( ! i ) {
212- return ;
213- }
214- var found = ai [ propName ] && ai [ propName ] . info ;
215- if ( ! found ) {
216- var pName = propName ;
217- type = '' ;
218- if ( i . type ) {
219- type = i . type ;
220- }
221- sql . push ( 'CREATE ' + type + ' INDEX ' + pName + ' ON ' +
222- self . schema + '.' + self . tableEscaped ( model ) +
223- '(\"' + pName + '\") ' ) ;
224- }
225- } ) ;
226-
227- // add multi-column indexes
228- indexNames . forEach ( function ( indexName ) {
229- var i = m . settings . indexes [ indexName ] ;
230- var found = ai [ indexName ] && ai [ indexName ] . info ;
231- if ( ! found ) {
232- var iName = indexName ;
233- var type = '' ;
234- if ( i . type ) {
235- type = i . type ;
236- }
237- var stmt = 'CREATE ' + type + 'INDEX ' + iName + ' ON ' +
238- self . schema + '.' + self . tableEscaped ( model ) + '(' ;
239-
240- var splitNames = i . columns . split ( / , \s * / ) ;
241- var colNames = splitNames . join ( '\",\"' ) ;
242-
243- stmt += '\"' + colNames + '\")' ;
244-
245- sql . push ( stmt ) ;
246- }
247- } ) ;
248- return sql ;
138+ return operations ;
249139 } ;
250140
251141 IBMDB . prototype . alterTable = function ( model , actualFields , actualIndexes ,
@@ -256,6 +146,12 @@ module.exports = function(IBMDB) {
256146 var sql = [ ] ;
257147 var tasks = [ ] ;
258148
149+ // Create the statements to drop all existing indexes before we start
150+ // altering the table.
151+ sql = sql . concat ( self . dropIndexes ( model , actualIndexes ) ) ;
152+
153+ // Add/Modify and drop column statements for ALTER TABLE are generated
154+ // prior to re-building the indexes as defined in the model.
259155 var operations = self . getAddModifyColumns ( model , actualFields ) ;
260156 operations = operations . concat ( self . getDropColumns ( model , actualFields ) ) ;
261157
@@ -264,7 +160,11 @@ module.exports = function(IBMDB) {
264160 sql . push ( 'ALTER TABLE ' + self . schema + '.' +
265161 self . tableEscaped ( model ) + ' ' + operations . join ( ' ' ) + ';' ) ;
266162 }
267- sql = sql . concat ( self . addIndexes ( model , actualIndexes ) ) ;
163+
164+ // Now that the column altering statments have been added we can add in the
165+ // indexes again.
166+ // ------------------------------------------------------------------------
167+ sql = sql . concat ( self . buildIndexes ( model ) ) ;
268168
269169 sql . forEach ( function ( i ) {
270170 tasks . push ( function ( cb ) {
0 commit comments