@Override
  protected NameSpaceTablesInformation performTablesMigration(
      Metadata metadata,
      DatabaseInformation existingDatabase,
      ExecutionOptions options,
      Dialect dialect,
      Formatter formatter,
      Set<String> exportIdentifiers,
      boolean tryToCreateCatalogs,
      boolean tryToCreateSchemas,
      Set<Identifier> exportedCatalogs,
      Namespace namespace,
      GenerationTarget[] targets) {
    final NameSpaceTablesInformation tablesInformation =
        new NameSpaceTablesInformation(
            metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper());

    if (schemaFilter.includeNamespace(namespace)) {
      createSchemaAndCatalog(
          existingDatabase,
          options,
          dialect,
          formatter,
          tryToCreateCatalogs,
          tryToCreateSchemas,
          exportedCatalogs,
          namespace,
          targets);
      final NameSpaceTablesInformation tables = existingDatabase.getTablesInformation(namespace);
      for (Table table : namespace.getTables()) {
        if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
          checkExportIdentifier(table, exportIdentifiers);
          final TableInformation tableInformation = tables.getTableInformation(table);
          if (tableInformation == null) {
            createTable(table, dialect, metadata, formatter, options, targets);
          } else if (tableInformation != null && tableInformation.isPhysicalTable()) {
            tablesInformation.addTableInformation(tableInformation);
            migrateTable(table, tableInformation, dialect, metadata, formatter, options, targets);
          }
        }
      }

      for (Table table : namespace.getTables()) {
        if (schemaFilter.includeTable(table) && table.isPhysicalTable()) {
          final TableInformation tableInformation = tablesInformation.getTableInformation(table);
          if (tableInformation == null
              || (tableInformation != null && tableInformation.isPhysicalTable())) {
            applyIndexes(table, tableInformation, dialect, metadata, formatter, options, targets);
            applyUniqueKeys(
                table, tableInformation, dialect, metadata, formatter, options, targets);
          }
        }
      }
    }
    return tablesInformation;
  }
Example #2
0
  public Iterator sqlAlterStrings(
      Dialect dialect,
      Mapping p,
      TableInformation tableInfo,
      String defaultCatalog,
      String defaultSchema)
      throws HibernateException {

    StringBuilder root =
        new StringBuilder("alter table ")
            .append(getQualifiedName(dialect, defaultCatalog, defaultSchema))
            .append(' ')
            .append(dialect.getAddColumnString());

    Iterator iter = getColumnIterator();
    List results = new ArrayList();

    while (iter.hasNext()) {
      final Column column = (Column) iter.next();
      final ColumnInformation columnInfo =
          tableInfo.getColumn(Identifier.toIdentifier(column.getName(), column.isQuoted()));

      if (columnInfo == null) {
        // the column doesnt exist at all.
        StringBuilder alter =
            new StringBuilder(root.toString())
                .append(' ')
                .append(column.getQuotedName(dialect))
                .append(' ')
                .append(column.getSqlType(dialect, p));

        String defaultValue = column.getDefaultValue();
        if (defaultValue != null) {
          alter.append(" default ").append(defaultValue);
        }

        if (column.isNullable()) {
          alter.append(dialect.getNullColumnString());
        } else {
          alter.append(" not null");
        }

        if (column.isUnique()) {
          String keyName = Constraint.generateName("UK_", this, column);
          UniqueKey uk = getOrCreateUniqueKey(keyName);
          uk.addColumn(column);
          alter.append(dialect.getUniqueDelegate().getColumnDefinitionUniquenessFragment(column));
        }

        if (column.hasCheckConstraint() && dialect.supportsColumnCheck()) {
          alter.append(" check(").append(column.getCheckConstraint()).append(")");
        }

        String columnComment = column.getComment();
        if (columnComment != null) {
          alter.append(dialect.getColumnComment(columnComment));
        }

        alter.append(dialect.getAddColumnSuffixString());

        results.add(alter.toString());
      }
    }

    if (results.isEmpty()) {
      Logger.getLogger(SchemaUpdate.class)
          .debugf("No alter strings for table : %s", getQuotedName());
    }

    return results.iterator();
  }