From 1828de20b06ef47cff05ca52b1b0b9641abc39aa Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Tue, 17 May 2016 14:36:15 +0300 Subject: [PATCH 1/3] Fix: DynamoDB having issues when setting host --- redash/query_runner/dynamodb_sql.py | 32 ++++++++++++++++------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/redash/query_runner/dynamodb_sql.py b/redash/query_runner/dynamodb_sql.py index 037eda0b8b..2bf03341a7 100644 --- a/redash/query_runner/dynamodb_sql.py +++ b/redash/query_runner/dynamodb_sql.py @@ -39,32 +39,28 @@ def configuration_schema(cls): "properties": { "region": { "type": "string", - "default": "us-west-1" + "default": "us-east-1" }, "host": { "type": "string", - "default": "127.0.0.1" + "default": "Use for non standard endpoints." }, "port": { "type": "number", - "default": 8000 + "default": 80 }, "access_key": { "type": "string", - "default": "anything" - }, "secret_key": { "type": "string", - "default": "anything" - }, "is_secure": { "type": "boolean", "default": False, } }, - "required": ["host"], + "required": ["access_key", "secret_key"], "secret": ["secret_key"] } @@ -83,11 +79,20 @@ def name(cls): def __init__(self, configuration): super(DynamoDBSQL, self).__init__(configuration) + def _connect(self): + engine = FragmentEngine() + config = self.configuration.to_dict() + + if not config.get('region'): + config['region'] = 'us-east-1' + + return engine, engine.connect(**config) + + def _get_tables(self, schema): try: - engine = FragmentEngine() - engine.connect(**self.configuration.to_dict()) + engine, _ = self._connect() for table in engine.describe_all(): schema[table.name] = {'name': table.name, 'columns': table.attrs.keys()} @@ -97,11 +102,9 @@ def _get_tables(self, schema): raise sys.exc_info()[1], None, sys.exc_info()[2] def run_query(self, query): - connection = None try: - engine = FragmentEngine() - connection = engine.connect(**self.configuration.to_dict()) + engine, connection = self._connect() res_dict = engine.execute(query if str(query).endswith(';') else str(query)+';') @@ -122,7 +125,8 @@ def run_query(self, query): json_data = json.dumps(data, cls=JSONEncoder) error = None except KeyboardInterrupt: - connection.cancel() + if connection: + connection.cancel() error = "Query cancelled by user." json_data = None except Exception as e: From 367ea859e42c5794b33074049abd9bbf814315de Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Wed, 18 May 2016 13:30:10 +0300 Subject: [PATCH 2/3] If host param is empty, change it to None --- redash/query_runner/dynamodb_sql.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/dynamodb_sql.py b/redash/query_runner/dynamodb_sql.py index 2bf03341a7..8b4d0f0ba5 100644 --- a/redash/query_runner/dynamodb_sql.py +++ b/redash/query_runner/dynamodb_sql.py @@ -86,8 +86,10 @@ def _connect(self): if not config.get('region'): config['region'] = 'us-east-1' - return engine, engine.connect(**config) + if config.get('host') == '': + config['host'] = None + return engine, engine.connect(**config) def _get_tables(self, schema): From 31aee1b6b90e8b4abb9c6635008849e24097ef20 Mon Sep 17 00:00:00 2001 From: Arik Fraimovich Date: Wed, 18 May 2016 13:30:20 +0300 Subject: [PATCH 3/3] Better exception handling --- redash/query_runner/dynamodb_sql.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/redash/query_runner/dynamodb_sql.py b/redash/query_runner/dynamodb_sql.py index 8b4d0f0ba5..2c1ec78a26 100644 --- a/redash/query_runner/dynamodb_sql.py +++ b/redash/query_runner/dynamodb_sql.py @@ -126,13 +126,15 @@ def run_query(self, query): data = {'columns': columns, 'rows': rows} json_data = json.dumps(data, cls=JSONEncoder) error = None + except (SyntaxError, RuntimeError) as e: + error = e.message + json_data = None except KeyboardInterrupt: if connection: connection.cancel() error = "Query cancelled by user." json_data = None except Exception as e: - logging.exception(e) raise sys.exc_info()[1], None, sys.exc_info()[2] return json_data, error