Repository: opendatacube/datacube-core

Issue:
"datacube system init" fails when postgres user includes hyphen
### Expected behaviour
Running `datacube system init` should init the postgres database, even if the postgres username includes a hyphen.

### Actual behaviour
The init fails, causing issues with the sqlalchemy/psycopg2 driver. It looks like it's passing the username string with `"` marks included?

Here is a trace:

```
Initialising database...
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.SyntaxError: zero-length delimited identifier at or near """"
LINE 1: set role ""ssdl-admin""
                 ^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/opt/conda/bin/datacube", line 10, in <module>
    sys.exit(cli())
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/ui/click.py", line 197, in new_func
    return f(parsed_config, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/ui/click.py", line 229, in with_index
    return f(index, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/scripts/system.py", line 48, in database_init
    with_permissions=init_users)
  File "/opt/conda/lib/python3.6/site-packages/datacube/index/index.py", line 60, in init_db
    is_new = self._db.init(with_permissions=with_permissions)
  File "/opt/conda/lib/python3.6/site-packages/datacube/drivers/postgres/_connections.py", line 180, in init
    is_new = _core.ensure_db(self._engine, with_permissions=with_permissions)
  File "/opt/conda/lib/python3.6/site-packages/datacube/drivers/postgres/_core.py", line 88, in ensure_db
    c.execute('set role "{}"'.format(quoted_user))
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1012, in execute
    return self._execute_text(object_, multiparams, params)
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1187, in _execute_text
    parameters,
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1324, in _execute_context
    e, statement, parameters, cursor, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1518, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) zero-length delimited identifier at or near """"
LINE 1: set role ""ssdl-admin""
                 ^
[SQL: set role ""ssdl-admin""]
(Background on this error at: http://sqlalche.me/e/f405)
```


### Steps to reproduce the behaviour

Run `datacube system init` with a postgres user including a hyphen, such as `ssdl-admin`.

### Environment information

* Which ``datacube --version`` are you using?
Open Data Cube core, version 1.8.0

* What datacube deployment/enviornment are you running against?
Self-hosted
"datacube system init" fails when postgres user includes hyphen
### Expected behaviour
Running `datacube system init` should init the postgres database, even if the postgres username includes a hyphen.

### Actual behaviour
The init fails, causing issues with the sqlalchemy/psycopg2 driver. It looks like it's passing the username string with `"` marks included?

Here is a trace:

```
Initialising database...
Traceback (most recent call last):
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.SyntaxError: zero-length delimited identifier at or near """"
LINE 1: set role ""ssdl-admin""
                 ^
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/opt/conda/bin/datacube", line 10, in <module>
    sys.exit(cli())
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/opt/conda/lib/python3.6/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/ui/click.py", line 197, in new_func
    return f(parsed_config, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/ui/click.py", line 229, in with_index
    return f(index, *args, **kwargs)
  File "/opt/conda/lib/python3.6/site-packages/datacube/scripts/system.py", line 48, in database_init
    with_permissions=init_users)
  File "/opt/conda/lib/python3.6/site-packages/datacube/index/index.py", line 60, in init_db
    is_new = self._db.init(with_permissions=with_permissions)
  File "/opt/conda/lib/python3.6/site-packages/datacube/drivers/postgres/_connections.py", line 180, in init
    is_new = _core.ensure_db(self._engine, with_permissions=with_permissions)
  File "/opt/conda/lib/python3.6/site-packages/datacube/drivers/postgres/_core.py", line 88, in ensure_db
    c.execute('set role "{}"'.format(quoted_user))
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1012, in execute
    return self._execute_text(object_, multiparams, params)
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1187, in _execute_text
    parameters,
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1324, in _execute_context
    e, statement, parameters, cursor, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1518, in _handle_dbapi_exception
    sqlalchemy_exception, with_traceback=exc_info[2], from_=e
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1284, in _execute_context
    cursor, statement, parameters, context
  File "/opt/conda/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) zero-length delimited identifier at or near """"
LINE 1: set role ""ssdl-admin""
                 ^
[SQL: set role ""ssdl-admin""]
(Background on this error at: http://sqlalche.me/e/f405)
```


### Steps to reproduce the behaviour

Run `datacube system init` with a postgres user including a hyphen, such as `ssdl-admin`.

### Environment information

* Which ``datacube --version`` are you using?
Open Data Cube core, version 1.8.0

* What datacube deployment/enviornment are you running against?
Self-hosted


Discussion:

Resolving patch:
diff --git a/datacube/drivers/postgres/_core.py b/datacube/drivers/postgres/_core.py
--- a/datacube/drivers/postgres/_core.py
+++ b/datacube/drivers/postgres/_core.py
@@ -85,7 +85,7 @@ def ensure_db(engine, with_permissions=True):
             raise
         finally:
             if with_permissions:
-                c.execute('set role "{}"'.format(quoted_user))
+                c.execute('set role {}'.format(quoted_user))
 
     if with_permissions:
         _LOG.info('Adding role grants.')


Test patch: