[Cubicweb] [postgis] Enable Postgis extension the right way

Yann Vote yann.vote at logilab.fr
Mon Jun 8 15:11:42 CEST 2015


Hello list,

As a trainee at Logilab working with geospatial data in the datacat cube 
(https://www.cubicweb.org/project/cubicweb-datacat), I'm relying on the 
postgis cube.

However, bootstrapping is fairly problematic right now: Postgis 
extension is never enabled, I have to do it manually. I would expect the 
following steps to work flawlessly:

* create a new cube,
* add a dependency on cubicweb-postgis,
* defines a schema that hold Geometry or Geography attributes,
* create an instance: tadam! I have some tables with geometry/geography 
columns in Postgres.

Currently this last step crash with an exception "execution of the sql 
schema failed, you should check your schema", and this is because 
Postgis extension has not been enabled before the tables are created.

Does this looks like a bug to everyone or just to me ?

There's a commented line in migration/precreate.py in the postgis cube, 
but to uncomment this line doesn't help. For some reason, precreate is 
executed *after* table creation (another bug ?)

So the only solution known to me right know is a monkeypatch suggested 
by RĂ©mi:

----
from cubicweb.server.sources.native import NativeSQLSource

_init_creating = NativeSQLSource.init_creating

@monkeypatch(NativeSQLSource)
def init_creating(self, cnxset=None):
     # Enable Postgis first
     if cnxset is None:
         _cnxset = self.repo._get_cnxset()
     else:
         _cnxset = cnxset
     _cnxset.cu.execute('CREATE EXTENSION IF NOT EXISTS postgis')
     _cnxset.commit()
     if cnxset is None:
         _cnxset.cnxset_freed()
         self.repo._free_cnxset(_cnxset)
     # Now do the job
     _init_creating(self, cnxset=cnxset)

----

If I put this in site_cubicweb.py in postgis cube, then everything 
works as expected.

What do you think about it ? Is there a better way to enable Postgis 
than a monkeypatch ?

Also note that, as a side effect of using 'CREATE EXTENSION', the 
minimal required version for Postgres will be 9.1. I don't think this is 
a problem nowadays.

Thank you for your comments.

Regards.

Yann



More information about the Cubicweb mailing list