josephramsay / lds_replicate

Replication scripts for LDS (LINZ Data Service).
http://data.linz.govt.nz/
2 stars 1 forks source link

Alter VersionChecker.getPostGISVersion to not use psql process #10

Closed palmerj closed 11 years ago

palmerj commented 11 years ago

In many cases windows users don't have the psql process available. In this case the version checking will fail. However the GDAL/OGR library has PG LIB support as part of the postgresql driver.

A better way might just be to use GDAL to get the version. i.e

from osgeo import gdal
from osgeo import ogr

driver = ogr.GetDriverByName("PostgreSQL")

pg_ds = None
postgis_version = None
version = None

try:
    # gdal.PushErrorHandler( 'CPLQuietErrorHandler' )
    pg_ds = ogr.Open( 'PG:dbname=mydb' )
    # gdal.PopErrorHandler()
except:
    pass
    # do some exception logging

ver_lyr = pg_ds.ExecuteSQL('SELECT version()')
feat = ver_lyr.GetNextFeature()
version_str = feat.GetFieldAsString('version')

version = float(version_str[11:14])

ver_lyr = pg_ds.ExecuteSQL('SELECT postgis_version()')
pg_has_postgis = ver_lyr is not None
if pg_has_postgis:
    feat = ver_lyr.GetNextFeature()
    version_str = feat.GetFieldAsString('postgis_version')
    postgis_version = float(version_str[0:3])
pg_ds.ReleaseResultSet(ver_lyr)

print "PostgreSQL Version: " + str(version)
if pg_has_postgis:
    print "PostGIS Version: " + str(postgis_version)
else:
    print "PostGIS Is not available"
josephramsay commented 11 years ago

Thanks. I thought I had this covered but was just for GDAL. Delayed version checking till DS initialisation and pushed checking into subclasses. e.g.

    def versionCheck(self):
        '''SpatiaLite/SQLite version checker'''
        from VersionChecker import VersionChecker,UnsupportedVersionException

        slv_cmd = 'file '+str(self._commonURI(None))

        slv_ver = VersionChecker.getVersionFromShell(slv_cmd,'SQLite (\d+.\w+) database')

        if VersionChecker.compareVersions(VersionChecker.SpatiaLite_MIN, slv_ver if slv_ver is not None else VersionChecker.SpatiaLite_MIN):
            raise UnsupportedVersionException('SpatiaLite version '+str(slv_ver)+' does not meet required minumum '+str(VersionChecker.SpatiaLite_MIN))

        return True
palmerj commented 11 years ago

Great - Looks good.

Any reason to only support PostgreSQL 9.0+? I would have thought 8.4+ would be easy to support. Which specific 9.0 features are you using?

BTW. 8.4 is still a fully supported version for the PostgreSQL community. PostGIS 2.0 also works with 8.4+. PostGIS 2.1 will however require 9.0.

josephramsay commented 11 years ago

Thanks.

No good reason, was just setting the minimum back to a last major release before a confirmed working version. Its 8.4 now

palmerj commented 11 years ago

Cool.