Beispiel #1
0
  /**
   * Cleans the schemas of all objects.
   *
   * @throws FlywayException when clean failed.
   */
  public void clean() throws FlywayException {
    if (cleanDisabled) {
      throw new FlywayException(
          "Unable to execute clean as it has been disabled with the \"flyway.cleanDisabled\" property.");
    }
    try {
      for (final FlywayCallback callback : callbacks) {
        new TransactionTemplate(connection)
            .execute(
                new TransactionCallback<Object>() {
                  @Override
                  public Object doInTransaction() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schemas[0]);
                    callback.beforeClean(connection);
                    return null;
                  }
                });
      }

      dbSupport.changeCurrentSchemaTo(schemas[0]);
      boolean dropSchemas = false;
      try {
        dropSchemas = metaDataTable.hasSchemasMarker();
      } catch (Exception e) {
        LOG.error("Error while checking whether the schemas should be dropped", e);
      }

      for (Schema schema : schemas) {
        if (!schema.exists()) {
          LOG.warn("Unable to clean unknown schema: " + schema);
          continue;
        }

        if (dropSchemas) {
          dropSchema(schema);
        } else {
          cleanSchema(schema);
        }
      }

      for (final FlywayCallback callback : callbacks) {
        new TransactionTemplate(connection)
            .execute(
                new TransactionCallback<Object>() {
                  @Override
                  public Object doInTransaction() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schemas[0]);
                    callback.afterClean(connection);
                    return null;
                  }
                });
      }
    } finally {
      dbSupport.restoreCurrentSchema();
    }
  }
  /**
   * Checks the result of the getCurrentSchema call.
   *
   * @param useProxy Flag indicating whether to check it using a proxy user or not.
   */
  private void checkCurrentSchema(boolean useProxy) throws Exception {
    Properties customProperties = getConnectionProperties();
    String user = customProperties.getProperty("oracle.user");
    String password = customProperties.getProperty("oracle.password");
    String url = customProperties.getProperty("oracle.url");

    String dataSourceUser = useProxy ? "\"flyway_proxy\"[" + user + "]" : user;

    DataSource dataSource =
        new DriverDataSource(
            Thread.currentThread().getContextClassLoader(), null, url, dataSourceUser, password);

    Connection connection = dataSource.getConnection();
    Schema currentSchema = new OracleDbSupport(connection).getCurrentSchema();
    connection.close();

    assertEquals(user.toUpperCase(), currentSchema.getName());
  }