@Override
  protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot)
      throws DatabaseException, InvalidExampleException {
    if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) {
      return;
    }

    if (foundObject instanceof Schema) {

      Database database = snapshot.getDatabase();
      Schema schema = (Schema) foundObject;

      List<CachedRow> tableMetaDataRs = null;
      try {
        tableMetaDataRs =
            ((JdbcDatabaseSnapshot) snapshot)
                .getMetaData()
                .getTables(
                    ((AbstractJdbcDatabase) database).getJdbcCatalogName(schema),
                    ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema),
                    null,
                    new String[] {"TABLE"});
        for (CachedRow row : tableMetaDataRs) {
          String tableName = row.getString("TABLE_NAME");
          Table tableExample =
              (Table)
                  new Table().setName(cleanNameFromDatabase(tableName, database)).setSchema(schema);

          schema.addDatabaseObject(tableExample);
        }
      } catch (SQLException e) {
        throw new DatabaseException(e);
      }
    }
  }
 public final String getJdbcSchemaName(final Schema schema) {
   if (schema == null) {
     return getJdbcSchemaName(getDefaultSchema());
   } else {
     return getJdbcSchemaName(new CatalogAndSchema(schema.getCatalogName(), schema.getName()));
   }
 }
Beispiel #3
0
  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Schema schema = (Schema) o;

    if (getCatalog() != null
        ? !getCatalog().equals(schema.getCatalog())
        : schema.getCatalog() != null) return false;
    if (getName() != null
        ? !getName().equalsIgnoreCase(schema.getName())
        : schema.getName() != null) return false;

    return true;
  }
  @Override
  protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot)
      throws DatabaseException, InvalidExampleException {
    Database database = snapshot.getDatabase();
    Relation relation = ((Column) example).getRelation();
    if (((Column) example).getComputed() != null && ((Column) example).getComputed()) {
      return example;
    }
    Schema schema = relation.getSchema();

    List<CachedRow> columnMetadataRs = null;
    try {

      JdbcDatabaseSnapshot.CachingDatabaseMetaData databaseMetaData =
          ((JdbcDatabaseSnapshot) snapshot).getMetaData();

      columnMetadataRs =
          databaseMetaData.getColumns(
              ((AbstractJdbcDatabase) database).getJdbcCatalogName(schema),
              ((AbstractJdbcDatabase) database).getJdbcSchemaName(schema),
              relation.getName(),
              example.getName());

      if (columnMetadataRs.size() > 0) {
        CachedRow data = columnMetadataRs.get(0);
        Column column = readColumn(data, relation, database);

        if (column != null
            && database instanceof MSSQLDatabase
            && database.getDatabaseMajorVersion() >= 8) {
          String sql;
          if (database.getDatabaseMajorVersion() >= 9) {
            // SQL Server 2005 or later
            // https://technet.microsoft.com/en-us/library/ms177541.aspx
            sql =
                "SELECT CAST([ep].[value] AS [nvarchar](MAX)) AS [REMARKS] "
                    + "FROM [sys].[extended_properties] AS [ep] "
                    + "WHERE [ep].[class] = 1 "
                    + "AND [ep].[major_id] = OBJECT_ID(N'"
                    + database.escapeStringForDatabase(
                        database.escapeTableName(
                            schema.getCatalogName(), schema.getName(), relation.getName()))
                    + "') "
                    + "AND [ep].[minor_id] = COLUMNPROPERTY([ep].[major_id], N'"
                    + database.escapeStringForDatabase(column.getName())
                    + "', 'ColumnId') "
                    + "AND [ep].[name] = 'MS_Description'";
          } else {
            // SQL Server 2000
            // https://technet.microsoft.com/en-us/library/aa224810%28v=sql.80%29.aspx
            sql =
                "SELECT CAST([p].[value] AS [ntext]) AS [REMARKS] "
                    + "FROM [dbo].[sysproperties] AS [p] "
                    + "WHERE [p].[id] = OBJECT_ID(N'"
                    + database.escapeStringForDatabase(
                        database.escapeTableName(
                            schema.getCatalogName(), schema.getName(), relation.getName()))
                    + "') "
                    + "AND [p].[smallid] = COLUMNPROPERTY([p].[id], N'"
                    + database.escapeStringForDatabase(column.getName())
                    + "', 'ColumnId') "
                    + "AND [p].[type] = 4 "
                    + "AND [p].[name] = 'MS_Description'";
          }

          List<String> remarks =
              ExecutorService.getInstance()
                  .getExecutor(snapshot.getDatabase())
                  .queryForList(new RawSqlStatement(sql), String.class);
          if (remarks != null && remarks.size() > 0) {
            column.setRemarks(StringUtils.trimToNull(remarks.iterator().next()));
          }
        }

        return column;
      } else {
        return null;
      }
    } catch (Exception e) {
      throw new DatabaseException(e);
    }
  }
  @Override
  public Change[] fixMissing(
      DatabaseObject missingObject,
      DiffOutputControl control,
      Database referenceDatabase,
      Database comparisonDatabase,
      ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();

    UniqueConstraint uc = (UniqueConstraint) missingObject;

    if (uc.getTable() == null) {
      return null;
    }

    AddUniqueConstraintChange change = new AddUniqueConstraintChange();
    change.setTableName(uc.getTable().getName());
    if (uc.getBackingIndex() != null && control.getIncludeTablespace()) {
      change.setTablespace(uc.getBackingIndex().getTablespace());
    }
    if (control.getIncludeCatalog()) {
      change.setCatalogName(uc.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
      change.setSchemaName(uc.getTable().getSchema().getName());
    }
    change.setConstraintName(uc.getName());
    change.setColumnNames(uc.getColumnNames());
    change.setDeferrable(uc.isDeferrable() ? Boolean.TRUE : null);
    change.setInitiallyDeferred(uc.isInitiallyDeferred() ? Boolean.TRUE : null);
    change.setDisabled(uc.isDisabled() ? Boolean.TRUE : null);

    if (comparisonDatabase instanceof OracleDatabase) {
      Index backingIndex = uc.getBackingIndex();
      if (backingIndex != null && backingIndex.getName() != null) {
        Change[] changes =
            ChangeGeneratorFactory.getInstance()
                .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);
        if (changes != null) {
          returnList.addAll(Arrays.asList(changes));

          change.setForIndexName(backingIndex.getName());
          Schema schema = backingIndex.getSchema();
          if (schema != null) {
            if (control.getIncludeCatalog()) {
              change.setForIndexCatalogName(schema.getCatalogName());
            }
            if (control.getIncludeSchema()) {
              change.setForIndexSchemaName(schema.getName());
            }
          }
        }
      }
    }

    Index backingIndex = uc.getBackingIndex();
    //        if (backingIndex == null) {
    //            Index exampleIndex = new Index().setTable(uc.getTable());
    //            for (String col : uc.getColumns()) {
    //                exampleIndex.getColumns().add(col);
    //            }
    //            control.setAlreadyHandledMissing(exampleIndex);
    //        } else {
    control.setAlreadyHandledMissing(backingIndex);
    //        }

    returnList.add(change);

    return returnList.toArray(new Change[returnList.size()]);
  }
  @Override
  public Change[] fixMissing(
      DatabaseObject missingObject,
      DiffOutputControl control,
      Database referenceDatabase,
      Database comparisonDatabase,
      ChangeGeneratorChain chain) {
    List<Change> returnList = new ArrayList<Change>();

    PrimaryKey pk = (PrimaryKey) missingObject;

    AddPrimaryKeyChange change = new AddPrimaryKeyChange();
    change.setTableName(pk.getTable().getName());
    if (control.getIncludeCatalog()) {
      change.setCatalogName(pk.getTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
      change.setSchemaName(pk.getTable().getSchema().getName());
    }
    change.setConstraintName(pk.getName());
    change.setColumnNames(pk.getColumnNames());
    if (control.getIncludeTablespace()) {
      change.setTablespace(pk.getTablespace());
    }

    if (referenceDatabase instanceof MSSQLDatabase
        && pk.getBackingIndex() != null
        && pk.getBackingIndex().getClustered() != null
        && !pk.getBackingIndex().getClustered()) {
      change.setClustered(false);
    }

    if (comparisonDatabase instanceof OracleDatabase
        || (comparisonDatabase instanceof DB2Database
            && pk.getBackingIndex() != null
            && !comparisonDatabase.isSystemObject(pk.getBackingIndex()))) {
      Index backingIndex = pk.getBackingIndex();
      if (backingIndex != null && backingIndex.getName() != null) {
        try {
          if (!control.getIncludeCatalog() && !control.getIncludeSchema()) {
            CatalogAndSchema schema =
                comparisonDatabase.getDefaultSchema().customize(comparisonDatabase);
            backingIndex
                .getTable()
                .setSchema(
                    schema.getCatalogName(),
                    schema
                        .getSchemaName()); // set table schema so it is found in the correct schema
          }
          if (referenceDatabase.equals(comparisonDatabase)
              || !SnapshotGeneratorFactory.getInstance().has(backingIndex, comparisonDatabase)) {
            Change[] fixes =
                ChangeGeneratorFactory.getInstance()
                    .fixMissing(backingIndex, control, referenceDatabase, comparisonDatabase);

            if (fixes != null) {
              for (Change fix : fixes) {
                if (fix != null) {
                  returnList.add(fix);
                }
              }
            }
          }
        } catch (Exception e) {
          throw new UnexpectedLiquibaseException(e);
        }

        change.setForIndexName(backingIndex.getName());
        Schema schema = backingIndex.getSchema();
        if (schema != null) {
          if (control.getIncludeCatalog()) {
            change.setForIndexCatalogName(schema.getCatalogName());
          }
          if (control.getIncludeSchema()) {
            change.setForIndexSchemaName(schema.getName());
          }
        }
      }
    }

    control.setAlreadyHandledMissing(pk.getBackingIndex());
    returnList.add(change);

    return returnList.toArray(new Change[returnList.size()]);
  }