Ant task to check if a database (connection) exists?

3.1k Views Asked by At

is there a possibility in ANT to check whether a database (connection) exists or not without failing the build?

For example:

<target name="check-database-available">
    <sql
        classpath="${oracle.jar}" driver="oracle.jdbc.OracleDriver"
        url="jdbc:oracle:thin:@${my.db.host}:${my.db.port}:${my.db.sid}" 
        userid="${my.db.user}" 
        password="${my.db.pw}"
        onerror="continue" errorproperty="exit.status">
        select * from dual;
    </sql>
    <echo message="### exit status = ${exit.status}" />
</target>

This will always fail with BUILD FAILED and

java.sql.SQLException: ORA-01017: invalid username/password; logon denied

because the db does not exist yet. Setting "onerror" to "continue" and checking the "errorproperty" won't work as the task seems not to be executed.

2

There are 2 best solutions below

0
On

Here is a workaround which sets a db.present property (checkpresence.sql is a simple select statement)

<target name="check-db-presence">
  <echo message="Checking database presence at: ${db.url}"/>
  <delete file="tmp/db.present"/>
  <sql driver="${db.driver}" url="${db.url}"
                     userid="${db.userName}" password="${db.password}"
                     failOnConnectionError="false" onerror="continue" warningproperty="db.empty" errorproperty="db.empty" 
                      src="scripts/${db.platform}/checkpresence.sql"
                    print="true" output="tmp/db.present"/>
  <condition property="db.present">
    <available file="tmp/db.present"/>
  </condition>
</target>
1
On

Starting with Ant v 1.8.0 you can use the failOnConnectionError attribute with the SQL Task.

The description reads as follows:

If false, will only print a warning message and not execute any statement if the task fails to connect to the database.

That looks like it would solve your problem.