All files / src/builders table.js

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 7561x 1x 1x 1x 1x 1x   1x 1x 1x 1x   1x   1x 9x 9x 9x 8x       1x                 9x 9x 9x 9x 9x   9x 9x         9x         9x 59x   59x   59x 59x 59x     59x 9x 9x 50x 7x 7x   7x 7x 4x   3x   43x 1x       9x       9x 9x 9x       9x       9x 9x       9x 9x       9x 9x                                                                                                                                                                                                                                                     9x 9x 9x 9x 9x   9x 9x         9x 12x   12x 11x 11x 11x 11x     11x   11x 2x 2x   2x 2x         2x 2x   9x 9x 9x         9x                                                                                                                                                                 9x 9x 9x 9x 9x 9x   9x 9x         9x 36x   36x 4x 4x           9x 7x 7x     2x   2x 2x 2x         2x 76x   76x 4x     76x   76x 7x 1x     7x 69x 4x 3x   4x 4x     4x 4x 1x   3x         2x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             1x  
const _ = require('lodash');
const async = require('async');
const util = require('util');
const objectHash = require('object-hash');
const readlineSync = require('readline-sync');
const deepDiff = require('deep-diff').diff;
 
const buildError = require('../orm/apollo_error.js');
const schemer = require('../validators/schema');
const parser = require('../utils/parser');
const normalizer = require('../utils/normalizer');
 
const ElassandraBuilder = require('./elassandra');
 
const TableBuilder = function f(driver, properties) {
  this._driver = driver;
  this._properties = properties;
  if (this._properties.esclient) {
    this._es_builder = new ElassandraBuilder(this._properties.esclient);
  }
};
 
TableBuilder.prototype = {
  _confirm_migration(message) {
    let permission = 'y';
    if (message && !this._properties.disableTTYConfirmation) {
      permission = readlineSync.question(util.format('Migration: %s (y/n): ', message));
    }
    return permission.toLowerCase();
  },
  get_table(callback) {
    const properties = this._properties;
    const keyspaceName = properties.keyspace;
    const tableName = properties.table_name;
    const dbSchema = { fields: {}, typeMaps: {}, staticMaps: {} };
    const query = 'SELECT * FROM system_schema.columns WHERE table_name = ? AND keyspace_name = ?;';
 
    this._driver.execute_query(query, [tableName, keyspaceName], (err, resultColumns) => {
      Iif (err) {
        callback(buildError('model.tablecreation.dbschemaquery', err));
        return;
      }
 
      Iif (!resultColumns.rows || resultColumns.rows.length === 0) {
        callback();
        return;
      }
 
      for (let r = 0; r < resultColumns.rows.length; r++) {
        const row = resultColumns.rows[r];
 
        dbSchema.fields[row.column_name] = parser.extract_type(row.type);
 
        const typeMapDef = parser.extract_typeDef(row.type);
        Eif (typeMapDef.length > 0) {
          dbSchema.typeMaps[row.column_name] = typeMapDef;
        }
 
        if (row.kind === 'partition_key') {
          if (!dbSchema.key) dbSchema.key = [[]];
          dbSchema.key[0][row.position] = row.column_name;
        } else if (row.kind === 'clustering') {
          if (!dbSchema.key) dbSchema.key = [[]];
          if (!dbSchema.clustering_order) dbSchema.clustering_order = {};
 
          dbSchema.key[row.position + 1] = row.column_name;
          if (row.clustering_order && row.clustering_order.toLowerCase() === 'desc') {
            dbSchema.clustering_order[row.column_name] = 'DESC';
          } else {
            dbSchema.clustering_order[row.column_name] = 'ASC';
          }
        } else if (row.kind === 'static') {
          dbSchema.staticMaps[row.column_name] = true;
        }
      }
 
      callback(null, dbSchema);
    });
  },
 
  get_table_schema(callback) {
    this.get_table((err, dbSchema) => {
      Iif (err) {
        callback(err);
        return;
      }
      Iif (!dbSchema) {
        callback();
        return;
      }
      this.get_indexes((err1, indexSchema) => {
        Iif (err1) {
          callback(err1);
          return;
        }
        this.get_mviews((err2, viewSchema) => {
          Iif (err2) {
            callback(err2);
            return;
          }
          Object.assign(dbSchema, indexSchema, viewSchema);
          callback(null, dbSchema);
        });
      });
    });
  },
 
  create_table(schema, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    const rows = [];
    let fieldType;
    Object.keys(schema.fields).forEach((k) => {
      if (schema.fields[k].virtual) {
        return;
      }
      let segment = '';
      fieldType = schemer.get_field_type(schema, k);
      if (schema.fields[k].typeDef) {
        segment = util.format('"%s" %s%s', k, fieldType, schema.fields[k].typeDef);
      } else {
        segment = util.format('"%s" %s', k, fieldType);
      }
 
      if (schema.fields[k].static) {
        segment += ' STATIC';
      }
 
      rows.push(segment);
    });
 
    const clauses = parser.get_primary_key_clauses(schema);
 
    const query = util.format(
      'CREATE TABLE IF NOT EXISTS "%s" (%s , PRIMARY KEY((%s)%s))%s;',
      tableName,
      rows.join(' , '),
      clauses.partitionKeyClause,
      clauses.clusteringKeyClause,
      clauses.clusteringOrderClause,
    );
 
    this._driver.execute_definition_query(query, (err, result) => {
      if (err) {
        callback(buildError('model.tablecreation.dbcreate', err));
        return;
      }
      callback(null, result);
    });
  },
 
  alter_table(operation, fieldname, type, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    if (operation === 'ALTER') type = util.format('TYPE %s', type);
    else if (operation === 'DROP') type = '';
 
    const query = util.format('ALTER TABLE "%s" %s "%s" %s;', tableName, operation, fieldname, type);
    this._driver.execute_definition_query(query, callback);
  },
 
  _drop_table(tableName, callback) {
    const query = util.format('DROP TABLE IF EXISTS "%s";', tableName);
    this._driver.execute_definition_query(query, (err) => {
      if (err) {
        callback(buildError('model.tablecreation.dbdrop', err));
        return;
      }
      callback();
    });
  },
 
  drop_table(materializedViews, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    const message = util.format(
      'Schema for table "%s" has changed in a way where alter migration is not possible, all data in the table will be lost, are you sure you want to drop the table?',
      tableName,
    );
    const permission = this._confirm_migration(message);
    if (permission !== 'y') {
      callback(buildError('model.tablecreation.schemamismatch', tableName, 'migration suspended, please apply the change manually'));
      return;
    }
    if (!materializedViews) {
      this._drop_table(tableName, callback);
      return;
    }
 
    const mviews = Object.keys(materializedViews);
    this.drop_mviews(mviews, (err) => {
      if (err) {
        callback(err);
        return;
      }
 
      this._drop_table(tableName, callback);
    });
  },
 
  drop_recreate_table(modelSchema, materializedViews, callback) {
    if (this._es_builder) {
      const indexName = `${this._properties.keyspace}_${this._properties.table_name}`;
      this._es_builder.delete_index(indexName, () => {
        this.drop_table(materializedViews, (err1) => {
          if (err1) {
            callback(err1);
            return;
          }
          this.create_table(modelSchema, callback);
        });
      });
      return;
    }
    this.drop_table(materializedViews, (err1) => {
      if (err1) {
        callback(err1);
        return;
      }
      this.create_table(modelSchema, callback);
    });
  },
 
  get_indexes(callback) {
    const properties = this._properties;
    const keyspaceName = properties.keyspace;
    const tableName = properties.table_name;
    const dbSchema = {};
    const query = 'SELECT * FROM system_schema.indexes WHERE table_name = ? AND keyspace_name = ?;';
 
    this._driver.execute_query(query, [tableName, keyspaceName], (err, resultIndexes) => {
      Iif (err) {
        callback(buildError('model.tablecreation.dbschemaquery', err));
        return;
      }
 
      for (let r = 0; r < resultIndexes.rows.length; r++) {
        const row = resultIndexes.rows[r];
 
        if (row.index_name && row.options.target) {
          const indexOptions = row.options;
          let target = indexOptions.target;
          target = target.replace(/["\s]/g, '');
          delete indexOptions.target;
 
          // keeping track of index names to drop index when needed
          if (!dbSchema.index_names) dbSchema.index_names = {};
 
          if (row.kind === 'CUSTOM') {
            const using = indexOptions.class_name;
            delete indexOptions.class_name;
 
            if (!dbSchema.custom_indexes) dbSchema.custom_indexes = [];
            const customIndexObject = {
              on: target,
              using,
              options: indexOptions,
            };
            dbSchema.custom_indexes.push(customIndexObject);
            dbSchema.index_names[objectHash(customIndexObject)] = row.index_name;
          } else {
            if (!dbSchema.indexes) dbSchema.indexes = [];
            dbSchema.indexes.push(target);
            dbSchema.index_names[target] = row.index_name;
          }
        }
      }
 
      callback(null, dbSchema);
    });
  },
 
  _create_index_query(tableName, indexName) {
    let query;
    const indexExpression = indexName.replace(/["\s]/g, '').split(/[()]/g);
    if (indexExpression.length > 1) {
      indexExpression[0] = indexExpression[0].toLowerCase();
      query = util.format(
        'CREATE INDEX IF NOT EXISTS ON "%s" (%s("%s"));',
        tableName,
        indexExpression[0],
        indexExpression[1],
      );
    } else {
      query = util.format(
        'CREATE INDEX IF NOT EXISTS ON "%s" ("%s");',
        tableName,
        indexExpression[0],
      );
    }
    return query;
  },
 
  create_indexes(indexes, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    async.eachSeries(indexes, (idx, next) => {
      const query = this._create_index_query(tableName, idx);
      this._driver.execute_definition_query(query, (err, result) => {
        if (err) next(buildError('model.tablecreation.dbindexcreate', err));
        else next(null, result);
      });
    }, callback);
  },
 
  _create_custom_index_query(tableName, customIndex) {
    let query = util.format(
      'CREATE CUSTOM INDEX IF NOT EXISTS ON "%s" ("%s") USING \'%s\'',
      tableName,
      customIndex.on,
      customIndex.using,
    );
 
    if (Object.keys(customIndex.options).length > 0) {
      query += ' WITH OPTIONS = {';
      Object.keys(customIndex.options).forEach((key) => {
        query += util.format("'%s': '%s', ", key, customIndex.options[key]);
      });
      query = query.slice(0, -2);
      query += '}';
    }
 
    query += ';';
 
    return query;
  },
 
  create_custom_indexes(customIndexes, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    async.eachSeries(customIndexes, (idx, next) => {
      const query = this._create_custom_index_query(tableName, idx);
      this._driver.execute_definition_query(query, (err, result) => {
        if (err) next(buildError('model.tablecreation.dbindexcreate', err));
        else next(null, result);
      });
    }, callback);
  },
 
  drop_indexes(indexes, callback) {
    async.each(indexes, (idx, next) => {
      const query = util.format('DROP INDEX IF EXISTS "%s";', idx);
      this._driver.execute_definition_query(query, next);
    }, (err) => {
      if (err) callback(buildError('model.tablecreation.dbindexdrop', err));
      else callback();
    });
  },
 
  get_mviews(callback) {
    const properties = this._properties;
    const keyspaceName = properties.keyspace;
    const tableName = properties.table_name;
    const dbSchema = {};
    let query = 'SELECT view_name,base_table_name,where_clause FROM system_schema.views WHERE keyspace_name=?;';
 
    this._driver.execute_query(query, [keyspaceName], (err, resultViews) => {
      Iif (err) {
        callback(buildError('model.tablecreation.dbschemaquery', err));
        return;
      }
 
      for (let r = 0; r < resultViews.rows.length; r++) {
        const row = resultViews.rows[r];
 
        if (row.view_name && row.base_table_name === tableName) {
          if (!dbSchema.materialized_views) dbSchema.materialized_views = {};
          dbSchema.materialized_views[row.view_name] = {
            where_clause: row.where_clause,
          };
        }
      }
 
      if (!dbSchema.materialized_views) {
        callback(null, dbSchema);
        return;
      }
 
      query = 'SELECT * FROM system_schema.columns WHERE keyspace_name=? and table_name IN ?;';
 
      const viewNames = Object.keys(dbSchema.materialized_views);
      this._driver.execute_query(query, [keyspaceName, viewNames], (err1, resultMatViews) => {
        Iif (err1) {
          callback(buildError('model.tablecreation.dbschemaquery', err1));
          return;
        }
 
        for (let r = 0; r < resultMatViews.rows.length; r++) {
          const row = resultMatViews.rows[r];
 
          if (!dbSchema.materialized_views[row.table_name].select) {
            dbSchema.materialized_views[row.table_name].select = [];
          }
 
          dbSchema.materialized_views[row.table_name].select.push(row.column_name);
 
          if (row.kind === 'partition_key') {
            if (!dbSchema.materialized_views[row.table_name].key) {
              dbSchema.materialized_views[row.table_name].key = [[]];
            }
 
            dbSchema.materialized_views[row.table_name].key[0][row.position] = row.column_name;
          } else if (row.kind === 'clustering') {
            if (!dbSchema.materialized_views[row.table_name].key) {
              dbSchema.materialized_views[row.table_name].key = [[]];
            }
            Eif (!dbSchema.materialized_views[row.table_name].clustering_order) {
              dbSchema.materialized_views[row.table_name].clustering_order = {};
            }
 
            dbSchema.materialized_views[row.table_name].key[row.position + 1] = row.column_name;
            if (row.clustering_order && row.clustering_order.toLowerCase() === 'desc') {
              dbSchema.materialized_views[row.table_name].clustering_order[row.column_name] = 'DESC';
            } else {
              dbSchema.materialized_views[row.table_name].clustering_order[row.column_name] = 'ASC';
            }
          }
        }
 
        callback(null, dbSchema);
      });
    });
  },
 
  _create_materialized_view_query(tableName, viewName, viewSchema) {
    const rows = [];
 
    for (let k = 0; k < viewSchema.select.length; k++) {
      if (viewSchema.select[k] === '*') rows.push(util.format('%s', viewSchema.select[k]));
      else rows.push(util.format('"%s"', viewSchema.select[k]));
    }
 
    const whereClause = viewSchema.where_clause || parser.get_mview_where_clause(this._properties.schema, viewSchema);
    const clauses = parser.get_primary_key_clauses(viewSchema);
 
    const query = util.format(
      'CREATE MATERIALIZED VIEW IF NOT EXISTS "%s" AS SELECT %s FROM "%s" WHERE %s PRIMARY KEY((%s)%s)%s;',
      viewName,
      rows.join(' , '),
      tableName,
      whereClause,
      clauses.partitionKeyClause,
      clauses.clusteringKeyClause,
      clauses.clusteringOrderClause,
    );
 
    return query;
  },
 
  create_mviews(materializedViews, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    async.eachSeries(Object.keys(materializedViews), (viewName, next) => {
      const query = this._create_materialized_view_query(
        tableName,
        viewName,
        materializedViews[viewName],
      );
      this._driver.execute_definition_query(query, (err, result) => {
        if (err) next(buildError('model.tablecreation.matviewcreate', err));
        else next(null, result);
      });
    }, callback);
  },
 
  drop_mviews(mviews, callback) {
    async.each(mviews, (view, next) => {
      const query = util.format('DROP MATERIALIZED VIEW IF EXISTS "%s";', view);
      this._driver.execute_definition_query(query, next);
    }, (err) => {
      if (err) callback(buildError('model.tablecreation.matviewdrop', err));
      else callback();
    });
  },
 
  _apply_alter_operations(alterOperations, dbSchema, normalizedModelSchema, normalizedDBSchema, callback) {
    // it should create/drop indexes/custom_indexes/materialized_views that are added/removed in model schema
    // remove common indexes/custom_indexes/materialized_views from normalizedModelSchema and normalizedDBSchema
    // then drop all remaining indexes/custom_indexes/materialized_views from normalizedDBSchema
    // and add all remaining indexes/custom_indexes/materialized_views from normalizedModelSchema
    const properties = this._properties;
    const tableName = properties.table_name;
    const addedIndexes = _.difference(normalizedModelSchema.indexes, normalizedDBSchema.indexes);
    const removedIndexes = _.difference(normalizedDBSchema.indexes, normalizedModelSchema.indexes);
    const removedIndexNames = [];
    removedIndexes.forEach((removedIndex) => {
      removedIndexNames.push(dbSchema.index_names[removedIndex]);
    });
 
    const addedCustomIndexes = _.filter(
      normalizedModelSchema.custom_indexes,
      (obj) => (!_.find(normalizedDBSchema.custom_indexes, obj)),
    );
    const removedCustomIndexes = _.filter(
      normalizedDBSchema.custom_indexes,
      (obj) => (!_.find(normalizedModelSchema.custom_indexes, obj)),
    );
    removedCustomIndexes.forEach((removedIndex) => {
      removedIndexNames.push(dbSchema.index_names[objectHash(removedIndex)]);
    });
 
    const addedMaterializedViewsNames = _.filter(
      Object.keys(normalizedModelSchema.materialized_views),
      (viewName) => (!_.isEqual(
        normalizedDBSchema.materialized_views[viewName],
        normalizedModelSchema.materialized_views[viewName],
      )),
    );
 
    const removedMaterializedViewNames = _.filter(
      Object.keys(normalizedDBSchema.materialized_views),
      (viewName) => (!_.isEqual(
        normalizedDBSchema.materialized_views[viewName],
        normalizedModelSchema.materialized_views[viewName],
      )),
    );
 
    const addedMaterializedViews = {};
    addedMaterializedViewsNames.forEach((viewName) => {
      addedMaterializedViews[viewName] = normalizedModelSchema.materialized_views[viewName];
    });
 
    // remove altered materialized views
    if (removedMaterializedViewNames.length > 0) {
      const message = util.format(
        'Schema for table "%s" has removed materialized_views: %j, are you sure you want to drop them?',
        tableName,
        removedMaterializedViewNames,
      );
      const permission = this._confirm_migration(message);
      if (permission !== 'y') {
        callback(buildError('model.tablecreation.schemamismatch', tableName, 'migration suspended, please apply the change manually'));
        return;
      }
    }
 
    this.drop_mviews(removedMaterializedViewNames, (err2) => {
      if (err2) {
        callback(err2);
        return;
      }
 
      if (removedIndexNames.length > 0) {
        const message = util.format(
          'Schema for table "%s" has removed indexes: %j, are you sure you want to drop them?',
          tableName,
          removedIndexNames,
        );
        const permission = this._confirm_migration(message);
        if (permission !== 'y') {
          callback(buildError('model.tablecreation.schemamismatch', tableName, 'migration suspended, please apply the change manually'));
          return;
        }
      }
 
      // remove altered indexes by index name
      this.drop_indexes(removedIndexNames, (err3) => {
        if (err3) {
          callback(err3);
          return;
        }
 
        // now apply alterOperations here
        async.eachSeries(alterOperations, (alterOperation, next) => {
          const permission = this._confirm_migration(alterOperation.message);
          if (permission !== 'y') {
            callback(buildError('model.tablecreation.schemamismatch', tableName, 'migration suspended, please apply the change manually'));
            return;
          }
          this.alter_table(alterOperation.operation, alterOperation.fieldName, alterOperation.type, next);
        }, (err4) => {
          if (err4) {
            callback(err4);
            return;
          }
 
          // add altered indexes
          // eslint-disable-next-line max-nested-callbacks
          this.create_indexes(addedIndexes, (err5) => {
            if (err5) {
              callback(err5);
              return;
            }
 
            // add altered custom indexes
            // eslint-disable-next-line max-nested-callbacks
            this.create_custom_indexes(addedCustomIndexes, (err6) => {
              if (err6) {
                callback(err6);
                return;
              }
 
              // add altered materialized_views
              this.create_mviews(addedMaterializedViews, callback);
            });
          });
        });
      });
    });
  },
 
  init_alter_operations(modelSchema, dbSchema, normalizedModelSchema, normalizedDBSchema, callback) {
    const properties = this._properties;
    const tableName = properties.table_name;
    const alterOperations = [];
    const differences = deepDiff(normalizedDBSchema.fields, normalizedModelSchema.fields);
    let droppedFields = false;
    async.eachSeries(differences, (diff, next) => {
      const fieldName = diff.path[0];
      if (diff.kind === 'N') {
        const message = util.format(
          'Schema for table "%s" has added field "%s", are you sure you want to alter to add the field?',
          tableName,
          fieldName,
        );
        alterOperations.push({
          fieldName,
          message,
          operation: 'ADD',
          type: parser.extract_altered_type(normalizedModelSchema, diff),
        });
        next();
        return;
      }
      if (diff.kind === 'D') {
        const message = util.format(
          'Schema for table "%s" has removed field "%s", all data in the field will lost, are you sure you want to alter to drop the field?',
          tableName,
          fieldName,
        );
        alterOperations.push({
          fieldName,
          message,
          operation: 'DROP',
        });
        droppedFields = true;
        normalizer.remove_dependent_views_from_normalized_schema(normalizedDBSchema, dbSchema, fieldName);
        next();
        return;
      }
      if (diff.kind === 'E') {
        // check if the alter field type is possible, otherwise try D and then N
        if (diff.path[1] === 'type') {
          // check if field part of primary key
          if (normalizedDBSchema.key[0].includes(fieldName) || normalizedDBSchema.key.indexOf(fieldName) > 0) {
            // alter field type impossible
            next(new Error('alter_impossible'));
          } else if (['text', 'ascii', 'bigint', 'boolean', 'decimal',
            'double', 'float', 'inet', 'int', 'timestamp', 'timeuuid',
            'uuid', 'varchar', 'varint'].includes(diff.lhs) && diff.rhs === 'blob') {
            // alter field type possible
            const message = util.format(
              'Schema for table "%s" has new type for field "%s", are you sure you want to alter to update the field type?',
              tableName,
              fieldName,
            );
            alterOperations.push({
              fieldName,
              message,
              operation: 'ALTER',
              type: diff.rhs,
            });
            next();
          } else if (diff.lhs === 'int' && diff.rhs === 'varint') {
            // alter field type possible
            const message = util.format(
              'Schema for table "%s" has new type for field "%s", are you sure you want to alter to update the field type?',
              tableName,
              fieldName,
            );
            alterOperations.push({
              fieldName,
              message,
              operation: 'ALTER',
              type: diff.rhs,
            });
            next();
          } else if (diff.lhs === 'timeuuid' && diff.rhs === 'uuid') {
            // alter field type possible
            const message = util.format(
              'Schema for table "%s" has new type for field "%s", are you sure you want to alter to update the field type?',
              tableName,
              fieldName,
            );
            alterOperations.push({
              fieldName,
              message,
              operation: 'ALTER',
              type: diff.rhs,
            });
            next();
          } else {
            // alter type impossible
            const message = util.format(
              'Schema for table "%s" has new type for field "%s", all data in the field will be lost, are you sure you want to drop the field & recreate it?',
              tableName,
              fieldName,
            );
            alterOperations.push({
              fieldName,
              message,
              operation: 'DROP',
            });
            alterOperations.push({
              fieldName,
              operation: 'ADD',
              type: parser.extract_altered_type(normalizedModelSchema, diff),
            });
            droppedFields = true;
            normalizer.remove_dependent_views_from_normalized_schema(normalizedDBSchema, dbSchema, fieldName);
            next();
          }
        } else {
          // alter type impossible
          const message = util.format(
            'Schema for table "%s" has new type for field "%s", all data in the field will be lost, are you sure you want to drop the field & recreate it?',
            tableName,
            fieldName,
          );
          alterOperations.push({
            fieldName,
            message,
            operation: 'DROP',
          });
          alterOperations.push({
            fieldName,
            operation: 'ADD',
            type: parser.extract_altered_type(normalizedModelSchema, diff),
          });
          droppedFields = true;
          normalizer.remove_dependent_views_from_normalized_schema(normalizedDBSchema, dbSchema, fieldName);
          next();
        }
        return;
      }
 
      next();
    }, (err) => {
      if (err) {
        callback(err);
        return;
      }
      if (droppedFields && this._es_builder) {
        const indexName = `${properties.keyspace}_${properties.table_name}`;
        this._es_builder.delete_index(indexName, () => {
          this._apply_alter_operations(alterOperations, dbSchema, normalizedModelSchema, normalizedDBSchema, callback);
        });
        return;
      }
      this._apply_alter_operations(alterOperations, dbSchema, normalizedModelSchema, normalizedDBSchema, callback);
    });
  },
};
 
module.exports = TableBuilder;