View Javadoc

1   package liquibase.precondition.core;
2   
3   import liquibase.changelog.DatabaseChangeLog;
4   import liquibase.changelog.ChangeSet;
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  
12  /**
13   * Precondition for specifying the type of database (oracle, mysql, etc.).
14   */
15  
16  public class DBMSPrecondition implements Precondition {
17      private String type;
18  
19      public DBMSPrecondition() {
20      }
21  
22      public String getType() {
23          return type;
24      }
25  
26      public void setType(String atype) {
27          this.type = atype.toLowerCase();
28      }
29  
30      public Warnings warn(Database database) {
31          return new Warnings();
32      }
33  
34      public ValidationErrors validate(Database database) {
35          return new ValidationErrors();
36      }
37  
38      public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet)
39              throws PreconditionFailedException, PreconditionErrorException {
40          try {
41              String dbType = database.getTypeName();
42              if (!type.equals(dbType)) {
43                  throw new PreconditionFailedException("DBMS Precondition failed: expected " + type + ", got " + dbType,
44                          changeLog, this);
45              }
46          } catch (PreconditionFailedException e) {
47              throw e;
48          } catch (Exception e) {
49              throw new PreconditionErrorException(e, changeLog, this);
50          }
51      }
52  
53      public String getName() {
54          return "dbms";
55      }
56  
57  }