View Javadoc

1   package liquibase.precondition.core;
2   
3   import liquibase.changelog.ChangeSet;
4   import liquibase.changelog.DatabaseChangeLog;
5   import liquibase.database.Database;
6   import liquibase.exception.PreconditionErrorException;
7   import liquibase.exception.PreconditionFailedException;
8   import liquibase.exception.ValidationErrors;
9   import liquibase.exception.Warnings;
10  import liquibase.precondition.Precondition;
11  import liquibase.snapshot.DatabaseSnapshotGeneratorFactory;
12  import liquibase.util.StringUtils;
13  
14  public class ViewExistsPrecondition implements Precondition {
15      private String schemaName;
16      private String viewName;
17  
18      public String getSchemaName() {
19          return schemaName;
20      }
21  
22      public void setSchemaName(String schemaName) {
23          this.schemaName = StringUtils.trimToNull(schemaName);
24      }
25  
26      public String getViewName() {
27          return viewName;
28      }
29  
30      public void setViewName(String viewName) {
31          this.viewName = viewName;
32      }
33  
34      @Override
35      public Warnings warn(Database database) {
36          return new Warnings();
37      }
38  
39      @Override
40      public ValidationErrors validate(Database database) {
41          return new ValidationErrors();
42      }
43  
44      @Override
45      public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet)
46              throws PreconditionFailedException, PreconditionErrorException {
47          try {
48              if (!DatabaseSnapshotGeneratorFactory.getInstance().getGenerator(database)
49                      .hasView(getSchemaName(), getViewName(), database)) {
50                  throw new PreconditionFailedException("View "
51                          + database.escapeTableName(getSchemaName(), getViewName()) + " does not exist", changeLog, this);
52              }
53          } catch (PreconditionFailedException e) {
54              throw e;
55          } catch (Exception e) {
56              throw new PreconditionErrorException(e, changeLog, this);
57          }
58      }
59  
60      @Override
61      public String getName() {
62          return "viewExists";
63      }
64  }