/** Drops all objects owned by the connected user. */
  @Override
  public void dropDatabaseObjects(final CatalogAndSchema schemaToDrop) throws LiquibaseException {
    ObjectQuotingStrategy currentStrategy = this.getObjectQuotingStrategy();
    this.setObjectQuotingStrategy(ObjectQuotingStrategy.QUOTE_ALL_OBJECTS);
    try {
      DatabaseSnapshot snapshot;
      try {
        final SnapshotControl snapshotControl = new SnapshotControl(this);
        final Set<Class<? extends DatabaseObject>> typesToInclude =
            snapshotControl.getTypesToInclude();

        // We do not need to remove indexes and primary/unique keys explicitly. They should be
        // removed
        // as part of tables.
        typesToInclude.remove(Index.class);
        typesToInclude.remove(PrimaryKey.class);
        typesToInclude.remove(UniqueConstraint.class);

        if (supportsForeignKeyDisable()) {
          // We do not remove ForeignKey because they will be disabled and removed as parts of
          // tables.
          typesToInclude.remove(ForeignKey.class);
        }

        final long createSnapshotStarted = System.currentTimeMillis();
        snapshot =
            SnapshotGeneratorFactory.getInstance()
                .createSnapshot(schemaToDrop, this, snapshotControl);
        LogFactory.getLogger()
            .debug(
                String.format(
                    "Database snapshot generated in %d ms. Snapshot includes: %s",
                    System.currentTimeMillis() - createSnapshotStarted, typesToInclude));
      } catch (LiquibaseException e) {
        throw new UnexpectedLiquibaseException(e);
      }

      final long changeSetStarted = System.currentTimeMillis();
      DiffResult diffResult =
          DiffGeneratorFactory.getInstance()
              .compare(
                  new EmptyDatabaseSnapshot(this),
                  snapshot,
                  new CompareControl(snapshot.getSnapshotControl().getTypesToInclude()));
      List<ChangeSet> changeSets =
          new DiffToChangeLog(
                  diffResult,
                  new DiffOutputControl(true, true, false).addIncludedSchema(schemaToDrop))
              .generateChangeSets();
      LogFactory.getLogger()
          .debug(
              String.format(
                  "ChangeSet to Remove Database Objects generated in %d ms.",
                  System.currentTimeMillis() - changeSetStarted));

      boolean previousAutoCommit = this.getAutoCommitMode();
      this.commit(); // clear out currently executed statements
      this.setAutoCommit(false); // some DDL doesn't work in autocommit mode
      final boolean reEnableFK = supportsForeignKeyDisable() && disableForeignKeyChecks();
      try {
        for (ChangeSet changeSet : changeSets) {
          changeSet.setFailOnError(false);
          for (Change change : changeSet.getChanges()) {
            if (change instanceof DropTableChange) {
              ((DropTableChange) change).setCascadeConstraints(true);
            }
            SqlStatement[] sqlStatements = change.generateStatements(this);
            for (SqlStatement statement : sqlStatements) {
              ExecutorService.getInstance().getExecutor(this).execute(statement);
            }
          }
          this.commit();
        }
      } finally {
        if (reEnableFK) {
          enableForeignKeyChecks();
        }
      }

      ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(this).destroy();
      LockServiceFactory.getInstance().getLockService(this).destroy();

      this.setAutoCommit(previousAutoCommit);

    } finally {
      this.setObjectQuotingStrategy(currentStrategy);
      this.commit();
    }
  }
Example #2
0
  protected void handleRollbackNode(ParsedNode rollbackNode, ResourceAccessor resourceAccessor)
      throws ParsedNodeException {
    String changeSetId = rollbackNode.getChildValue(null, "changeSetId", String.class);
    if (changeSetId != null) {
      String changeSetAuthor = rollbackNode.getChildValue(null, "changeSetAuthor", String.class);
      String changeSetPath = rollbackNode.getChildValue(null, "changeSetPath", getFilePath());

      ChangeSet changeSet =
          this.getChangeLog().getChangeSet(changeSetPath, changeSetAuthor, changeSetId);
      if (changeSet == null) { // check from root
        changeSet =
            getChangeLog()
                .getRootChangeLog()
                .getChangeSet(changeSetPath, changeSetAuthor, changeSetId);
        if (changeSet == null) {
          throw new ParsedNodeException(
              "Change set "
                  + new ChangeSet(
                          changeSetId,
                          changeSetAuthor,
                          false,
                          false,
                          changeSetPath,
                          null,
                          null,
                          null)
                      .toString(false)
                  + " does not exist");
        }
      }
      for (Change change : changeSet.getChanges()) {
        rollback.getChanges().add(change);
      }
      return;
    }

    boolean foundValue = false;
    for (ParsedNode childNode : rollbackNode.getChildren()) {
      Change rollbackChange = toChange(childNode, resourceAccessor);
      if (rollbackChange != null) {
        addRollbackChange(rollbackChange);
        foundValue = true;
      }
    }

    Object value = rollbackNode.getValue();
    if (value != null) {
      if (value instanceof String) {
        String finalValue = StringUtils.trimToNull((String) value);
        if (finalValue != null) {
          String[] strings = StringUtils.processMutliLineSQL(finalValue, true, true, ";");
          for (String string : strings) {
            addRollbackChange(new RawSQLChange(string));
            foundValue = true;
          }
        }
      } else {
        throw new ParsedNodeException(
            "Unexpected object: " + value.getClass().getName() + " '" + value.toString() + "'");
      }
    }
    if (!foundValue) {
      addRollbackChange(new EmptyChange());
    }
  }