Question

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

Was it helpful?

Solution

(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>

OTHER TIPS

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}"
/>

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>

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top