private static boolean createConstraintOnPostgres(ConnectionSource connectionSource,String constraintName,String constraintStatement) throws SQLException {
  Preconditions.checkArgument(isUnquotedSqlIdentifier(constraintName),"invalid constraint name: %s",constraintName);
  if (!(connectionSource.getDatabaseType() instanceof PostgresDatabaseType)) {
    logger.warn("Not creating constraint {}: DB is not Postgres",constraintName);
    return false;
  }
  String queryString="SELECT count(*) FROM pg_constraint WHERE conname = '" + constraintName + "';";
  long constraintCount=connectionSource.getReadOnlyConnection().queryForLong(queryString);
  if (constraintCount != 0) {
    assert constraintCount == 1;
    return false;
  }
  logger.info("Creating constraint {}",constraintName);
  connectionSource.getReadWriteConnection().executeStatement(constraintStatement,DatabaseConnection.DEFAULT_RESULT_FLAGS);
  assert connectionSource.getReadOnlyConnection().queryForLong(queryString) == 1;
  return true;
}

private static boolean createConstraintOnPostgres ( ConnectionSource connectionSource , String constraintName , String constraintStatement ) throws SQLException { Preconditions . checkArgument ( isUnquotedSqlIdentifier ( constraintName ) , "invalid constraint name: %s" , constraintName ) ; if ( ! ( connectionSource . getDatabaseType ( ) instanceof PostgresDatabaseType ) ) { logger . warn ( "Not creating constraint {}: DB is not Postgres" , constraintName ) ; return false ; } String queryString = "SELECT count(*) FROM pg_constraint WHERE conname = '" + constraintName + "';" ; long constraintCount = connectionSource . getReadOnlyConnection ( ) . queryForLong ( queryString ) ; if ( constraintCount != 0 ) { assert constraintCount == 1 ; return false ; } logger . info ( "Creating constraint {}" , constraintName ) ; connectionSource . getReadWriteConnection ( ) . executeStatement ( constraintStatement , DatabaseConnection . DEFAULT_RESULT_FLAGS ) ; assert connectionSource . getReadOnlyConnection ( ) . queryForLong ( queryString ) == 1 ; return true ; }
if using postgres and constaintname doesn ' t exist , it executes constraintstatement to create it .
