On 20/06/13 01:37 -0700, Albert Cervera i Areny wrote:
> El dimecres 19 de juny de 2013 14:09:44 UTC+2, Cédric Krier va escriure:
>
> > Hi,
> >
> > In the same idea as the changeset [1], I would like to propose this
> > change for the write method:
> >
> > @classmethod
> > def write(cls, records, values):
> > …
> >
> > =>
> >
> > @classmethod
> > def write(cls, records, values, *args):
> > assert not len(args) % 2
> > all_records = []
> > actions = iter((records, values) + args)
> > for records, values in zip(actions, actions):
> > …
> > all_records += records
> >
> > cls._validate(all_records)
> >
>
> I've been thinking in something in this line since the create with lists
> implementation. The main issue I see is yet another important API change,
> which we wanted to minimize.
Except it is a backward compatible changes but of course code that
extend write method will need to be updated.
> I know it has some implementation issues but I also had another idea in
> mind which maybe somebody has some ideas to improve and that it may be less
> aggressive in terms of API changes. The idea is to implement deferred
> validation in the same way a database implements deferred constraints.
> Something like this:
>
> with Transaction().set_deferred():
> for record in records:
> record.field = random.random()
> record.save()
>
> The checks would be executed on all modified records when exiting the with
> statement.
Ok but you will still have one UPDATE query per iteration.
Such cases are not very common and most of the time we get some calls the
ModelStorage.write for each cases. So I like the idea to still be able
to work easily with classmethod on a bunch of records.
Moreover you can not use your context manager on RPC calls.
Also what will happen in case of nested deferred context?
And finally, if after the save of one "invalid" record, you could have
some piece of code that will be triggered and make decision based on
"invalid" data which could result to a weird exception.
> Probably the main issue is that in case of a failure of the validation, it
> may be harder to find where and when the value was set incorrectly, but
> that's why that should be used in loops and places where the developer
> knows beforehand that it can take advantage of deferring the validation.
But such assumption doesn't work well in the modular context of Tryton.
You can not be sure of what will run inside your loop.
> One advantage is that it can defer validation in a loop of "create" or
> "write" (for example, an import process which checks for the existance of a
> record and creates it or updates it accordingly).
Yes but it is just at best an improvement of factor 2 no more. And for
large bunch of records, it will just be none.