protected void compareIndexDescription(
     TableDescription xmlTableDescription, TableDescription databaseTableDescription) {
   for (int i = 0; i < xmlTableDescription.getIndexSize(); i++) {
     IndexDescription xmlIndexDescription = xmlTableDescription.getIndex(i);
     IndexDescription databaseIndexDescription = null;
     if (xmlIndexDescription.getIndexName() != null) {
       databaseIndexDescription =
           databaseTableDescription.getIndex(xmlIndexDescription.getIndexName());
     } else {
       for (IndexDescription each : databaseTableDescription.getIndices()) {
         if (indexColumnsEqual(xmlIndexDescription, each)) {
           databaseIndexDescription = each;
           break;
         }
       }
       if (databaseIndexDescription == null)
         databaseIndexDescription = databaseTableDescription.getPrimaryKey();
     }
     compareSingleIndexDescription(xmlIndexDescription, databaseIndexDescription);
   }
 }
  /** Install the manager. */
  public void install() throws ManifoldCFException {
    // First, get the authority manager table name and name column
    IAuthorityGroupManager authMgr = AuthorityGroupManagerFactory.make(threadContext);

    // Always use a loop, and no transaction, as we may need to retry due to upgrade
    while (true) {
      Map existing = getTableSchema(null, null);
      if (existing == null) {
        // Install the "objects" table.
        HashMap map = new HashMap();
        map.put(nameField, new ColumnDescription("VARCHAR(32)", true, false, null, null, false));
        map.put(
            descriptionField,
            new ColumnDescription("VARCHAR(255)", false, true, null, null, false));
        map.put(
            classNameField, new ColumnDescription("VARCHAR(255)", false, false, null, null, false));
        map.put(
            groupNameField,
            new ColumnDescription(
                "VARCHAR(32)",
                false,
                true,
                authMgr.getTableName(),
                authMgr.getGroupNameColumn(),
                false));
        map.put(maxCountField, new ColumnDescription("BIGINT", false, false, null, null, false));
        map.put(configField, new ColumnDescription("LONGTEXT", false, true, null, null, false));
        performCreate(map, null);
      } else {
        // Upgrade code
        ColumnDescription cd = (ColumnDescription) existing.get(groupNameField);
        if (cd == null) {
          Map addMap = new HashMap();
          addMap.put(
              groupNameField,
              new ColumnDescription(
                  "VARCHAR(32)",
                  false,
                  true,
                  authMgr.getTableName(),
                  authMgr.getGroupNameColumn(),
                  false));
          performAlter(addMap, null, null, null);
        }
        // Get rid of the authorityName field.  When we do this we need to copy into the group name
        // field, adding groups if they don't yet exist first
        cd = (ColumnDescription) existing.get(authorityNameField);
        if (cd != null) {
          ArrayList params = new ArrayList();
          IResultSet set =
              performQuery(
                  "SELECT " + nameField + "," + authorityNameField + " FROM " + getTableName(),
                  null,
                  null,
                  null);
          for (int i = 0; i < set.getRowCount(); i++) {
            IResultRow row = set.getRow(i);
            String repoName = (String) row.getValue(nameField);
            String authName = (String) row.getValue(authorityNameField);
            if (authName != null && authName.length() > 0) {
              // Attempt to create a matching auth group.  This will fail if the group
              // already exists
              IAuthorityGroup grp = authMgr.create();
              grp.setName(authName);
              try {
                authMgr.save(grp);
              } catch (ManifoldCFException e) {
                if (e.getErrorCode() == ManifoldCFException.INTERRUPTED) throw e;
                // Fall through; the row exists already
              }
              Map<String, String> map = new HashMap<String, String>();
              map.put(groupNameField, authName);
              params.clear();
              String query =
                  buildConjunctionClause(
                      params, new ClauseDescription[] {new UnitaryClause(nameField, repoName)});
              performUpdate(map, " WHERE " + query, params, null);
            }
          }
          List<String> deleteList = new ArrayList<String>();
          deleteList.add(authorityNameField);
          performAlter(null, null, deleteList, null);
        }
      }

      // Install dependent tables.
      historyManager.install(getTableName(), nameField);
      throttleSpecManager.install(getTableName(), nameField);

      // Index management
      IndexDescription authorityIndex = new IndexDescription(false, new String[] {groupNameField});
      IndexDescription classIndex = new IndexDescription(false, new String[] {classNameField});

      // Get rid of indexes that shouldn't be there
      Map indexes = getTableIndexes(null, null);
      Iterator iter = indexes.keySet().iterator();
      while (iter.hasNext()) {
        String indexName = (String) iter.next();
        IndexDescription id = (IndexDescription) indexes.get(indexName);

        if (authorityIndex != null && id.equals(authorityIndex)) authorityIndex = null;
        else if (classIndex != null && id.equals(classIndex)) classIndex = null;
        else if (indexName.indexOf("_pkey") == -1)
          // This index shouldn't be here; drop it
          performRemoveIndex(indexName);
      }

      // Add the ones we didn't find
      if (authorityIndex != null) performAddIndex(null, authorityIndex);

      if (classIndex != null) performAddIndex(null, classIndex);

      break;
    }
  }
 private boolean indexColumnsEqual(
     IndexDescription xmlIndexDescription, IndexDescription databaseIndexDescription) {
   boolean columnsOK =
       xmlIndexDescription.getColumnSize() == databaseIndexDescription.getColumnSize();
   if (columnsOK) {
     for (int i = 0; i < xmlIndexDescription.getColumnSize(); i++) {
       String xmlColumn = xmlIndexDescription.getColumn(i);
       String dbColumn = databaseIndexDescription.getColumn(i);
       if (xmlColumn.equalsIgnoreCase(dbColumn)) {
         assertTrue(
             "Table: "
                 + xmlIndexDescription.getTableName()
                 + "... Wrong Orderdirection! Column:"
                 + xmlIndexDescription.getColumn(i)
                 + " expected OrderDirection: "
                 + xmlIndexDescription.getOrderDirection(i)
                 + " databaseOrderDirection: "
                 + databaseIndexDescription.getOrderDirection(i),
             xmlIndexDescription.getOrderDirection(i)
                 == databaseIndexDescription.getOrderDirection(i));
       } else {
         columnsOK = false;
       }
     }
   }
   return columnsOK;
 }
 protected void compareSingleIndexDescription(
     IndexDescription xmlIndexDescription, IndexDescription databaseIndexDescription) {
   if (xmlIndexDescription == null && databaseIndexDescription == null) return;
   if (xmlIndexDescription == null) {
     assertTrue(
         "Table: "
             + databaseIndexDescription.getTableName()
             + "... IndexName not expected! "
             + databaseIndexDescription.getIndexName()
             + " databaseIndexName: "
             + databaseIndexDescription.getIndexName(),
         false);
     return;
   }
   if (databaseIndexDescription == null) {
     assertTrue(
         "Table: "
             + xmlIndexDescription.getTableName()
             + "...DatabaseIndexName not found!! Expected IndexName: "
             + xmlIndexDescription.getIndexName(),
         false);
     return;
   }
   if (xmlIndexDescription.isFunctionBased() || databaseIndexDescription.isFunctionBased()) {
     log("function based not yet supported"); // todo [RSt] nyi
     return;
   }
   if (xmlIndexDescription.getIndexName() == null
       || xmlIndexDescription
           .getIndexName()
           .equalsIgnoreCase(databaseIndexDescription.getIndexName())) {
     boolean columnsOK = indexColumnsEqual(xmlIndexDescription, databaseIndexDescription);
     if (!columnsOK) {
       assertTrue(
           "Table: "
               + xmlIndexDescription.getTableName()
               + ", index: "
               + xmlIndexDescription.getIndexName()
               + "... Columns differ! expected: "
               + xmlIndexDescription.getColumns()
               + " but was "
               + databaseIndexDescription.getColumns(),
           false);
     }
   } else {
     assertTrue(
         "Table: "
             + xmlIndexDescription.getTableName()
             + "... Wrong Indexname! Expected IndexName: "
             + xmlIndexDescription.getIndexName()
             + " databaseIndexName: "
             + databaseIndexDescription.getIndexName(),
         false);
   }
 }