alterTable()

in src/app/database.service.ts [67:102]


  alterTable(log: LogService, args: alterTableArgs) {
    log.info("Altering table", args);
    const table = this.getOrMakeTable(args.tableName);

    // Remove specified columns.
    if (args.removeColumns) {
      args.removeColumns.forEach(col => {
        const i = table.columns.findIndex(c => c.columnName == col.columnName);
        if (i == -1) {
          this.log.warn("ALTER TABLE", table.tableName, "FAILED TO DELETE NON-EXISTENT COLUMN", col.columnName);
        } else {
          this.log.debug("ALTER TABLE", table.tableName, "DELETE COLUMN", col.columnName);
          table.columns.splice(i, 1);
        }
      });
    }

    // Alter existing columns.
    if (args.alterColumns) {
      args.alterColumns.forEach(col => {
        const c = table.columns.find(c => c.columnName == col.columnName);
        if (c) {
          this.log.debug("ALTER TABLE", args.tableName, "MODIFY COLUMN", col.columnName, "MODIFY TYPE", col.columnType, "->", c.columnType);
          c.columnType = col.columnType;
        }
      });
    }

    // Add new columns.
    if (args.addColumns) {
      args.addColumns.forEach(col => {
        this.log.debug("ALTER TABLE", args.tableName, "ADD COLUMN", col.columnName, "TYPE", col.columnType);
        table.columns.push(col);
      });
    }
  }