Intercept Schema Update

Mar 14, 2015 at 5:17 AM

Does VITA maintain database update version?, example:

If i have 2 computers using the same application with the same database, if one is updated and the other not, if i execute the older version it will delete de columns added in the newer version?

Mar 14, 2015 at 7:45 AM
Kinda yes and no.
Yes - there's a module that adds DbInfo table that holds version of Db. It loads this into Database.DbInfo property automatically and before anything happens. After db is updated, the version is set from EntityApp.Version, and dbInfo record in database is updated.
No - currently VITA does not use this information much, except when recording update scripts in DbModelChangeModule tables. So if you start older version of software against newer version of database - the software version will win and roll db to older state.
And it happened for me too, when shared system was rolled back to older state by old-version client. I am planning to put some protection against this, just did not decided yet exactly how.
But here's one consideration. Automatic db update from software is recommended only for dev and test environments. For updating production servers, recommended way is to generate update script using vdbtool, and apply it manually - if we are talking about centralized production environment, managed by admins. Will this work better for you?
Mar 16, 2015 at 6:59 PM
Can i intercept the dbupdate processe and read that information before VITA does the Update?

Mar 16, 2015 at 7:26 PM
yes, but it is not straightforward.
Get IDataAccessService instance (app.GetService<IDAtaAccessService>());
Hook to event Registered (fired when new data source is registered);
in the handler, get to Database instance (args.DataSource.Database) and hook to ModelUpdating event.
In the handler, you get list of scripts that are about to be applied.
Mar 16, 2015 at 9:48 PM
By the way, I added check if Db version is higher than current app version - if yes, the model update is canceled and error is thrown, to prevent older client versions rolling back db to older version.
Mar 16, 2015 at 11:50 PM
Works fine :)

How can i intercept this error so i can inform the user?

Mar 17, 2015 at 5:48 PM
Well.. for now, not so simple. You can try to check/compare versions yourself, when data source is registered and throw a custom exception... Or catch the specific message tokens inside the message/log of exception that is thrown. I am thinking about refactoring this piece (initialization/model update events and errors), will think about making things like this easier.