Repository: GeotrekCE/Geotrek-admin

Issue:
SEARCH_PATH for Geotrek DB user
Since Geotrek 0.28, tables and functions have be moved to different schemas, which is a very good point (https://github.com/makinacorpus/Geotrek/releases/tag/v0.28.0).

Schemas are not mentionned in triggers which is OK too, as Django is doing it in his connexions so it is not a problem for GEOTREK applications.

It gets a problem when you try to edit or insert a data from an external tool (QGIS, Talend...). 
You have to change the db_user search_path so that he can find tables and functions not only in public schemas.

It could be interesting to do it during GEOTREK installation for the Geotrek DB user mentionned in settings : 

ALTER USER $geotrek_db_user SET 
search_path=public,django,geotrek,gestion,rando,zonage,foncier,tourisme; 

Of course if you are using another user to edit datas in external tools, you will have to do it manually the first time. 



Discussion:
Search path can be set at database level too:
ALTER DATABASE $geotrek_db SET
search_path=public,django,geotrek,gestion,rando,zonage,foncier,tourisme; 

Yes of course, this is what I done. Because I got the informations.
But other users will not know nor understand why it dont work anymore when working in QGIS or Talend such as here : https://groups.google.com/forum/#!topic/geotrek-fr/rzqTyC0fElw

So it would be good to include the SQL in installation/migration script for Geotrek_DB_user as I guess most of people are editing the DB from external tools with the same user. 

Of course those who are using another db_user will have to change their search_path manually at DB level.

Yes, instead of specifying the search_path at the connection level, we could `ALTER USER` during the sync process ([here for example](https://github.com/makinacorpus/Geotrek/blob/v0.28.2/geotrek/common/utils/postgresql.py#L102-L141))

OK and will work for those who are installing Geotrek from scratch ? Not only for those who are updating it ? 

Yep :)

71bb0868 seems to fail when geotrek user is not owner of the geotrekdb database, which is the case with ./install.sh
It works with travis since the db is set up before running ./install.sh and postgresql user is superuser (@leplatrem, do you remember why ? If it's just to be able to create the test database, replacing SUPERUSER by CREATEDB should be enough).
Actually I don't think we can set path at database or user level with just geotrek user rights. Though, we may set it in the install.sh script.

As far as I can tell, the Travis setup about database is made to take profit of the existing database provided in the container... During some time, we could not rely on install.sh to install postgis since the gdal version was incompatible with the one installed by default (see GDAL packaging hell)

Regarding the search_path, don't you think a user can set his own variables using `ALTER USER` ? or like password for instance !

Some ALTER USER commands should require special rights but maybe just setting search_path works. I'll try.


Resolving patch:
diff --git a/geotrek/common/utils/postgresql.py b/geotrek/common/utils/postgresql.py
--- a/geotrek/common/utils/postgresql.py
+++ b/geotrek/common/utils/postgresql.py
@@ -145,6 +145,7 @@ def move_models_to_schemas(app_label):
     # set database level (for all users, and for this database only).
     if app_label == 'common':
         dbname = settings.DATABASES['default']['NAME']
+        dbuser = settings.DATABASES['default']['USER']
         search_path = 'public,%s' % ','.join(set(settings.DATABASE_SCHEMAS.values()))
-        sql = "ALTER DATABASE %s SET search_path=%s;" % (dbname, search_path)
+        sql = "ALTER ROLE %s IN DATABASE %s SET search_path=%s;" % (dbuser, dbname, search_path)
         cursor.execute(sql)


Test patch: