Hi Antoine,
Thank you very much for providing me this example. unfortunately I've been
asked to use ANT only without using unix shell scripts. I have tried to tackle
it as individual modules using ANT script running the 1st iteration of the
mysql scripts against newschema1. I need to run each iteration against all 4
schemas, then it would do a version check and if version check wasn't met then
it would start 2nd iteration and so on.
I would appreciate if you could take a look at the script and assist. I need to
create a recursive loop which I can't do and also some error checking module as
well as some how linking these modules together so that they depend on each
other etc... At the moment I have to run each target on its own.
Please feel free to change my novice attempt at writing an ANT script.
Thank you
<!-- ***************************************
Restore MySQL database
**************************************** -->
<target name="restore-db" >
<!-- Clean up the database by deleting and then creating it. -->
<antcall target="delete_db"/>
<antcall target="create_db"/>
<!-- Import data from the backup file. -->
<exec executable="${my.sql}" input="db_dumps/db1.sql">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema1>"/>
</exec>
<exec executable="${my.sql}" input="db_dumps/db2.sql">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema2>"/>
</exec>
<exec executable="${my.sql}" input="db_dumps/db3.sql">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema3>"/>
</exec>
<exec executable="${my.sql}" input="db_dumps/db4.sql">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema4>"/>
</exec>
</target>
<target name="delete_db">
<exec executable="${my.sql}">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="--force"/>
<arg value="drop"/>
<arg value="<newschema1>"/>
<arg value="<newschema2>"/>
<arg value="<newschema3>"/>
<arg value="<newschema4>"/>
</exec>
</target>
<target name="create_db">
<exec executable="${my.sql}">
<arg value="--user=root"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="--force"/>
<arg value="create"/>
<arg value="<newschema1>"/>
<arg value="<newschema2>"/>
<arg value="<newschema3>"/>
<arg value="<newschema4>"/>
</exec>
</target>
<!-- ***************************************
Get database version
**************************************** -->
<target name="get-db-version" >
<exec executable="${my.sql}" outputproperty="get-db-version.out"
input="getdbver.sql">
<arg line="--host=${server.ip}"/>
<arg line="--user=root"/>
<arg line="--password=<password>"/>
<arg line="-D${newschema1}"/>
</exec>
<echo>Is at ${get-db-version.out}</echo>
</target>
<!-- ************************************************
Check database upgrade scripts exists
*********************************************** -->
<target name="db-version" if="get-db-version.out" depends="get-db-version" >
<echo>Running upgrade from ${get-db-version.out}</echo>
<condition property="script1.exists">
<available filepath="<directory>" file="script1.sql"/>
</condition>
<antcall target="run-script1"/>
<condition property="script2.exists">
<available filepath="<directory>" file="script2.sql"/>
</condition>
<antcall target="run-script2"/>
<condition property="script3.exists">
<available filepath="<directory>" file="script3.sql"/>
</condition>
<antcall target="run-script3"/>
<condition property="script4.exists">
<available filepath="<directory>" file="script4.sql"/>
</condition>
<antcall target="run-script4"/>
</target>
<!-- ***************************************
Run database upgrade scripts
**************************************** -->
<target name="run-script1" if="script1.exists" >
<echo>Running sql in dir <directory></echo>
<exec executable="${my.sql}" input="<directory>/script1.sql"
failonerror="true">
<arg value="--user=<username>"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema1>"/>
</exec>
</target>
<target name="run-script2" if="script2.exists" >
<echo>Running sql in dir <directory></echo>
<exec executable="${my.sql}" input="<directory>/script2.sql"
failonerror="true">
<arg value="--user=<username>"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema1>"/>
</exec>
</target>
<target name="run-script3" if="script3.exists" >
<echo>Running sql in dir <directory></echo>
<exec executable="${my.sql}" input="<directory>/script3.sql"
failonerror="true">
<arg value="--user=<username>"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema1>"/>
</exec>
</target>
<target name="run-script4" if="script4.exists" >
<echo>Running sql in dir <directory></echo>
<exec executable="${my.sql}" input="<directory>/script4.sql"
failonerror="true">
<arg value="--user=<username>"/>
<arg value="--password=<password>"/>
<arg value="--host=localhost" />
<arg value="--port=3306" />
<arg value="<newschema1>"/>
</exec>
</target>
Date: Thu, 4 Nov 2010 10:45:35 -0700
From: [email protected]
To: [email protected]
Subject: Re: How to script database upgrade using ANT script
On 11/4/10 1:35 PM, Java Jboss wrote:
>
> Hi Antoine,
>
> Thank you for this update. I am using a linux server. I've been asked to do
> this in ANT only. Can you possibly provide me an example of how a similar
> process might be scripted using ANT?
>
> As I have never used ANT or any other similar products before I am using
> Google for help.
I suggest that you start by reading the manual under
http://ant.apache.org/manual/ first.
You would start by writing a build file for your work looking like that
<project name="databaseupgrade">
<target name="all" depends="createdatabase,loaddump,runscripts"/>
<target name="init">
<property name="databasename" value="mynewdatabase"/>
</target>
<target name="createdatabase" depends="init">
<exec command="sh" failonerror="true">
<arg value="createdatabase.sh"/>
<arg value="${databasename}"/>
</exec>
</target>
<target name="loaddump">
<!-- some action goes here -->
</target>
<target name="runscripts">
<!-- some action gets here too -->
</target>
</project>
>
> Thank you.
>
>
> Date: Thu, 4 Nov 2010 09:46:31 -0700
> From: [hidden email]
> To: [hidden email]
> Subject: Re: How to script database upgrade using ANT script
>
> Hi,
>
> you will need to use the<exec/> task for steps like creating the
> database and importing the dump into the new schema.
> Once your database is up, you can use the<sqlexec/> task to run DDL and
> DML statements.
> To check whether a script exists, there is a task called<available/>
> which can set a property if a file is available.
> For error checking, you can run<exec/> with failonerror="true" to stop
> your build if a script fails.
> You need to test before hand whether the system commands or the scripts
> that you will run to create the database and import the dump are
> returning proper exit statuses. (setting ERRORLEVEL on Windows or $? on
> UNIX).
>
> Regards,
>
> Antoine
>
> On 11/4/10 12:28 PM, Java Jboss wrote:
>
>> Hi,
>>
>> I’m a complete newbie to ANT and I need to write an ant script which
>> upgrades a database version from base level 1.00 to a given version number
>> by running a series of mysql (ver 5.1.45) scripts.
>>
>> Using ANT script I need to:
>> 1) create a database
>> 2) import mysql database(s) dump to a new schema
>> 3) check current database version
>> 4) then check if sql script1 exists
>> 4.1) if "No" then check for next sql script until it has checked for all
>> scripts then "end"
>> 4.2) if "yes" then run first sql script then check if second sql script
>> exists - if "yes" then run second sql script and so on until the required
>> database version is reached.
>> 5) check database version is at correct level then “end” or else start the
>> loop again
>> 6) how can I script error checking also
>>
>> Thank you all in advance.
>>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
>
>
>
>
> View message @
> http://ant.1045680.n5.nabble.com/How-to-script-database-upgrade-using-ANT-script-tp3250375p3250409.html
> To unsubscribe from How to script database upgrade using ANT script, click
> here.
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]
View message @
http://ant.1045680.n5.nabble.com/How-to-script-database-upgrade-using-ANT-script-tp3250375p3250502.html
To unsubscribe from How to script database upgrade using ANT script, click
here.
--
View this message in context:
http://ant.1045680.n5.nabble.com/How-to-script-database-upgrade-using-ANT-script-tp3250375p3250988.html
Sent from the Ant - Users mailing list archive at Nabble.com.