Getting Phing's dbdeploy task to automatically rollback on delta error

3.3k views Asked by At

I am using Phing's dbdeploy task to manage my database schema. This is working fine, as long as there is no errors in the queries of my delta files.

However, if there is an error, dbdeploy will just run the delta files up to the query with the error and then abort. This causes me some frustration, because I have to manually rollback the entry in the changelog table then. If I don't, dbdeploy will assume the migration was successful on a subsequent try, so any retries will do nothing.

So the question is, is there any way to get dbdeploy use transactions or can you suggest any other way to have phing rollback automatically when an error occurs?

Note: I'm not that proficient with Phing, so if this involves writing a custom task, any example code or a url with further information is highly appreciated. Thanks

6

There are 6 answers

0
Foot On BEST ANSWER

(if you're still out there...) Regarding phing for a db dump task, use the db's dump utility and create a phing task. I use postgres mainly and have this in my phing build.xml:

<target name="db-dump" depends="">
    <php expression="date('Ymd-Hi')" returnProperty="phing.dump.ts"/>
    <exec command="pg_dump -h ${db.host} -U ${db.user} -O ${db.name} | gzip > ${db.dumppath}/${db.name}-${phing.dump.ts}.gz" />
</target>
0
Алик Нематов On

I know, this is very old thread, but maybe it will be use full for someone else. You can use try->catch statements to implement a solution for that. My example :

<trycatch>
    <try>
        <exec
            command="${progs.mysql} -h${db.live.host} -u${db.live.user} -p${db.live.password} ${db.live.name} &lt; ${db.live.output}/${build.dbdeploy.deployfile}"
            dir="${project.basedir}"
            checkreturn="true" />
            <echo>Live  database was upgraded successfully</echo>
    </try>    
    <catch>
            <echo>Errors in upgrading database</echo>
            <exec
            command="${progs.mysql} -h${db.live.host} -u${db.live.user} -p${db.live.password} ${db.live.name} &lt; ${db.live.output}/${build.dbdeploy.undofile}"
            dir="${project.basedir}"
            checkreturn="true" />
    </catch>
    </trycatch>
3
TomTom On

The "proper" way to do this is a backup before schema change, then rollback in case of error.

You dont say what db you use - but it would wonder me if all schema changes would be supported in transactions. Mos thigh end SQL databases (oracle, db2, sql server) dont do that in all cases for really good reasons. Transacitonal schema changes are REALLY hard and REALLY resouce intensive.

2
Steve Robillard On

Why not write a series of undo deltas and add a phing task that runs on failure of the other task?

1
Chris Suszyński On

The simplest way of solving your problem is to use pdoexec task which by default runs sql script in transaction. When error occurs the database engine will automatically rollback your changes (even those on change log table - database will be in previous state)

Example:

<pdosqlexec 
    url="pgsql:host=${db.host}
    dbname=${db.name}"
    userid="${db.user}"
    password="${db.pass}"
    src="${build.dbdeploy.deployfile}"
/>
1
Aborto Virtuale de la Veritano On

you really should take a look at capistrano. TomTom: you are missing something here: the backup before schema change of course has to be made - but what to do with the NEW data that was inserted meanwhile you were thinking that everything is ok? I do not say, tthat there is a good tool for this problem, but the problem exists in real life.