| 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.DatabaseException; | 
  | 7 |  |  import liquibase.exception.PreconditionErrorException; | 
  | 8 |  |  import liquibase.exception.PreconditionFailedException; | 
  | 9 |  |  import liquibase.exception.ValidationErrors; | 
  | 10 |  |  import liquibase.exception.Warnings; | 
  | 11 |  |  import liquibase.precondition.Precondition; | 
  | 12 |  |  import liquibase.snapshot.DatabaseSnapshotGeneratorFactory; | 
  | 13 |  |  import liquibase.util.StringUtils; | 
  | 14 |  |   | 
  | 15 | 8 |  public class ForeignKeyExistsPrecondition implements Precondition { | 
  | 16 |  |      private String schemaName; | 
  | 17 |  |      private String foreignKeyTableName; | 
  | 18 |  |      private String foreignKeyName; | 
  | 19 |  |   | 
  | 20 |  |      public String getSchemaName() { | 
  | 21 | 0 |          return schemaName; | 
  | 22 |  |      } | 
  | 23 |  |   | 
  | 24 |  |      public void setSchemaName(String schemaName) { | 
  | 25 | 0 |          this.schemaName = StringUtils.trimToNull(schemaName); | 
  | 26 | 0 |      } | 
  | 27 |  |   | 
  | 28 |  |      public String getForeignKeyTableName() { | 
  | 29 | 0 |          return foreignKeyTableName; | 
  | 30 |  |      } | 
  | 31 |  |   | 
  | 32 |  |      public void setForeignKeyTableName(String foreignKeyTableName) { | 
  | 33 | 0 |          this.foreignKeyTableName = foreignKeyTableName; | 
  | 34 | 0 |      } | 
  | 35 |  |   | 
  | 36 |  |      public String getForeignKeyName() { | 
  | 37 | 0 |          return foreignKeyName; | 
  | 38 |  |      } | 
  | 39 |  |   | 
  | 40 |  |      public void setForeignKeyName(String foreignKeyName) { | 
  | 41 | 0 |          this.foreignKeyName = foreignKeyName; | 
  | 42 | 0 |      } | 
  | 43 |  |   | 
  | 44 |  |      @Override | 
  | 45 |  |      public Warnings warn(Database database) { | 
  | 46 | 0 |          return new Warnings(); | 
  | 47 |  |      } | 
  | 48 |  |   | 
  | 49 |  |      @Override | 
  | 50 |  |      public ValidationErrors validate(Database database) { | 
  | 51 | 0 |          return new ValidationErrors(); | 
  | 52 |  |      } | 
  | 53 |  |   | 
  | 54 |  |      @Override | 
  | 55 |  |      public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) | 
  | 56 |  |              throws PreconditionFailedException, PreconditionErrorException { | 
  | 57 |  |          try { | 
  | 58 |  |              boolean checkPassed; | 
  | 59 | 0 |              if (getForeignKeyTableName() == null) { | 
  | 60 | 0 |                  checkPassed = DatabaseSnapshotGeneratorFactory.getInstance() | 
  | 61 |  |                          .createSnapshot(database, getSchemaName(), null).getForeignKey(getForeignKeyName()) != null; | 
  | 62 |  |              } else {  | 
  | 63 | 0 |                  checkPassed = DatabaseSnapshotGeneratorFactory | 
  | 64 |  |                          .getInstance() | 
  | 65 |  |                          .getGenerator(database) | 
  | 66 |  |                          .getForeignKeyByForeignKeyTable(getSchemaName(), getForeignKeyTableName(), getForeignKeyName(), | 
  | 67 |  |                                  database) != null; | 
  | 68 |  |              } | 
  | 69 | 0 |              if (!checkPassed) { | 
  | 70 | 0 |                  String message = "Foreign Key " + database.escapeStringForDatabase(getForeignKeyName()); | 
  | 71 | 0 |                  if (getForeignKeyTableName() != null) { | 
  | 72 | 0 |                      message += " on table " + getForeignKeyTableName(); | 
  | 73 |  |                  } | 
  | 74 | 0 |                  message += " does not exist"; | 
  | 75 | 0 |                  throw new PreconditionFailedException(message, changeLog, this); | 
  | 76 |  |              } | 
  | 77 | 0 |          } catch (DatabaseException e) { | 
  | 78 | 0 |              throw new PreconditionErrorException(e, changeLog, this); | 
  | 79 | 0 |          } | 
  | 80 | 0 |      } | 
  | 81 |  |   | 
  | 82 |  |      @Override | 
  | 83 |  |      public String getName() { | 
  | 84 | 8 |          return "foreignKeyConstraintExists"; | 
  | 85 |  |      } | 
  | 86 |  |  } |