superset-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b...@apache.org
Subject [incubator-superset] branch master updated: check db extra and metadata params preemptively (#6004)
Date Tue, 09 Oct 2018 03:26:30 GMT
This is an automated email from the ASF dual-hosted git repository.

beto pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-superset.git


The following commit(s) were added to refs/heads/master by this push:
     new f50ed17  check db extra and metadata params preemptively (#6004)
f50ed17 is described below

commit f50ed17655fa3bf91911a6a959a26144ae6ed7b4
Author: Junda Yang <youngyjd@gmail.com>
AuthorDate: Mon Oct 8 20:26:26 2018 -0700

    check db extra and metadata params preemptively (#6004)
    
    * check db extra and metadata params preemptively
    
    * flake8
    
    * make use of inspect
---
 superset/connectors/sqla/models.py |  3 ++-
 superset/views/core.py             | 19 ++++++++++++++++++-
 2 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/superset/connectors/sqla/models.py b/superset/connectors/sqla/models.py
index 78b5873..75251f6 100644
--- a/superset/connectors/sqla/models.py
+++ b/superset/connectors/sqla/models.py
@@ -809,7 +809,8 @@ class SqlaTable(Model, BaseDatasource):
         """Fetches the metadata for the table and merges it in"""
         try:
             table = self.get_sqla_table_object()
-        except Exception:
+        except Exception as e:
+            logging.exception(e)
             raise Exception(_(
                 "Table [{}] doesn't seem to exist in the specified database, "
                 "couldn't fetch column information").format(self.table_name))
diff --git a/superset/views/core.py b/superset/views/core.py
index aaf46d9..707140c 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -6,6 +6,7 @@ from __future__ import print_function
 from __future__ import unicode_literals
 
 from datetime import datetime, timedelta
+import inspect
 import logging
 import os
 import re
@@ -26,7 +27,7 @@ import simplejson as json
 from six import text_type
 from six.moves.urllib import parse
 import sqlalchemy as sqla
-from sqlalchemy import and_, create_engine, or_, update
+from sqlalchemy import and_, create_engine, MetaData, or_, update
 from sqlalchemy.engine.url import make_url
 from sqlalchemy.exc import IntegrityError
 from unidecode import unidecode
@@ -259,6 +260,7 @@ class DatabaseView(SupersetModelView, DeleteMixin, YamlExportMixin): 
# noqa
     }
 
     def pre_add(self, db):
+        self.check_extra(db)
         db.set_sqlalchemy_uri(db.sqlalchemy_uri)
         security_manager.merge_perm('database_access', db.perm)
         # adding a new database we always want to force refresh schema list
@@ -279,6 +281,21 @@ class DatabaseView(SupersetModelView, DeleteMixin, YamlExportMixin):
 # noqa
     def _delete(self, pk):
         DeleteMixin._delete(self, pk)
 
+    def check_extra(self, db):
+        # this will check whether json.loads(extra) can succeed
+        try:
+            extra = db.get_extra()
+        except Exception as e:
+            raise Exception('Extra field cannot be decoded by JSON. {}'.format(str(e)))
+
+        # this will check whether 'metadata_params' is configured correctly
+        metadata_signature = inspect.signature(MetaData)
+        for key in extra.get('metadata_params', {}):
+            if key not in metadata_signature.parameters:
+                raise Exception('The metadata_params in Extra field '
+                                'is not configured correctly. The key '
+                                '{} is invalid.'.format(key))
+
 
 appbuilder.add_link(
     'Import Dashboards',


Mime
View raw message