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

6

6

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

Lafond answered 16/3, 2010 at 11:27 Comment(1)
I think phings dbdeploy at its current state is inferior to projects which concentrate on db-versioning only. See #3325071 for exampleLait
S
3

(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>
Saleratus answered 7/4, 2011 at 20:2 Comment(0)
A
3

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}"
/>
Ale answered 30/12, 2011 at 22:35 Comment(1)
Does this work for DDL changing statements? At least in MySql those cannot be rolled back.Lafond
L
3

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>
Lowney answered 18/5, 2012 at 14:50 Comment(0)
F
1

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

Fleisher answered 16/3, 2010 at 16:51 Comment(2)
Thanks. I do have undo deltas. But how could I run them automatically? How would the other phing task know how far to run back?Lafond
in terms of knowing how far back to go you could set a property for the starting point. As for running automatically I am not sure you can do it automatically, but what about setting a task that checks the db versioning table and based on that runs the undo. You may want to look at xinc which has notifiers for success and failure with the ability to run some code logic based on success or failure.Fleisher
R
1

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.

Rodolphe answered 28/6, 2010 at 22:3 Comment(1)
Thanks for the suggestion. I'd like to stay in the PHP ecosystem if possible though.Lafond
L
-1

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.

Loraine answered 16/3, 2010 at 11:33 Comment(3)
Thanks. Using MySQL but I am considering using this for managing some Views in an Oracle 10g as well. Any ideas how to do the backup with Phing then?Lafond
Nope. Should not be done in there. it is the admins job to make backup before configuration / software changes.Loraine
Umm, that kinda defeats the purpose of automatic deployment tools like phing, doesn't it?Lafond

© 2022 - 2024 — McMap. All rights reserved.