[Cubicweb] commit and rollback on rqlcontroller

Aurélien Campéas aurelien.campeas at logilab.fr
Fri Jun 13 12:52:57 CEST 2014

On 13/06/2014 12:18, Jinpeng Li wrote:
> Dear all,
> Yesterday I worked with Yann for the indexation on database and we have
> some feedback on rqlcontroller. When we insert an entity
> with rqlcontroller, the mandatory relations of this entity should be
> inserted as well, otherwise I will get error. It means each post already
> commit, or rollback when it fails. Maybe cubicweb team needs to make it
> stateless since it is RESTful Web services.

Sending queries through rqlcontroller is asking the database to be
atomically updated (per request).

> For example:
> ```
> base_url = 'http://localhost:8080'
> r = requests.Session()
> a = r.get(base_url + "?__login=admin&__password=admin")
> print a.status_code
> url = base_url + "/rqlio/1.0"
> args = [('INSERT Study ST: ST identifier "%s"' % study_id, {}),
>               ('INSERT Subject SU: SU identifier "%s"' % subject_id, {}),
>               ('SET SU related_studies ST WHERE ST eid %(t)s, SU eid %(u)s',
>               {'t': '__r0', 'u':'__r1'}),]
> a = requests.post(
>                 url,
>                 data=json.dumps(args),
>                 headers={'Content-Type': 'application/json'},
>                 verify=False,
>                 timeout=3)
> ```
> However, this architecture makes it difficult to migrate from zmq/pyro
> to the rqlcontroller. While an entity will be inserted, and its
> complicated mandatory relations should be inserted in the meantime. We
> cannot first insert entities and then insert their relations. I need to
> put all entities and their relations in args which is difficult to read
> with '__r0' and '__r1'. In addition, we don't have rollback operation.
> Suggestion:
> It is better to make rqlcontroller like a similar architecture to
> the zmq/pyro method based on http session ID? 
> 1: Easy migration from zmq/pyro to the rqlcontroller. 
> 2: The rollback operation can easily restore database to a consistent
> state after erroneous operations are performed.

I suspect you would like some kind of "session" + orm management
client-side to help write the code, especially coming from the


 from cwclientlib import session # I don't remember the details

 with session(login='babar', password='celeste').remote_cnx() as cnx:
     s = cnx.create_entity('Study', identifier=...)
     su = cnx.create_entity('Subject', identifier=...)
     cnx.commit() # => Raise (Remote)ValidationError or succeed

That would really be just syntactic sugar for your code above.
I will leave implementing this as an exercise for the reader ;-)

Providing an .execute on the cnx object would be the hard part ...


More information about the Cubicweb mailing list