diff --git a/superset-frontend/src/SqlLab/components/ResultSet.jsx b/superset-frontend/src/SqlLab/components/ResultSet.jsx index 71f3f4625a..5dd34691bf 100644 --- a/superset-frontend/src/SqlLab/components/ResultSet.jsx +++ b/superset-frontend/src/SqlLab/components/ResultSet.jsx @@ -194,6 +194,10 @@ export default class ResultSet extends React.PureComponent { this.props.search ? this.props.height - SEARCH_HEIGHT : this.props.height, ); let sql; + let exploreDBId = query.dbId; + if (this.props.database && this.props.database.explore_database_id) { + exploreDBId = this.props.database.explore_database_id; + } if (this.props.showSql) { sql = ; @@ -245,7 +249,7 @@ export default class ResultSet extends React.PureComponent { diff --git a/superset/models/core.py b/superset/models/core.py index 25922ac791..595cbc7f90 100755 --- a/superset/models/core.py +++ b/superset/models/core.py @@ -195,6 +195,10 @@ class Database( return bool(extra.get("allows_virtual_table_explore", True)) + @property + def explore_database_id(self) -> int: + return self.get_extra().get("explore_database_id", self.id) + @property def data(self) -> Dict[str, Any]: return { @@ -205,6 +209,7 @@ class Database( "allows_subquery": self.allows_subquery, "allows_cost_estimate": self.allows_cost_estimate, "allows_virtual_table_explore": self.allows_virtual_table_explore, + "explore_database_id": self.explore_database_id, } @property diff --git a/superset/views/database/api.py b/superset/views/database/api.py index 212dc8b95a..2caed02e1e 100644 --- a/superset/views/database/api.py +++ b/superset/views/database/api.py @@ -137,6 +137,7 @@ class DatabaseRestApi(DatabaseMixin, BaseSupersetModelRestApi): "allows_subquery", "allows_cost_estimate", "allows_virtual_table_explore", + "explore_database_id", "backend", "function_names", ] diff --git a/superset/views/database/views.py b/superset/views/database/views.py index ae4b3bc116..88652705c8 100644 --- a/superset/views/database/views.py +++ b/superset/views/database/views.py @@ -187,7 +187,7 @@ class CsvToDatabaseView(SimpleFormView): # E.g. if hive was used to upload a csv, presto will be a better option # to explore the table. expore_database = database - explore_database_id = database.get_extra().get("explore_database_id", None) + explore_database_id = database.explore_database_id if explore_database_id: expore_database = ( db.session.query(models.Database) diff --git a/tests/core_tests.py b/tests/core_tests.py index bb26a85b20..fde253e47b 100644 --- a/tests/core_tests.py +++ b/tests/core_tests.py @@ -1307,6 +1307,20 @@ class CoreTests(SupersetTestCase): database.extra = json.dumps(extra) self.assertEqual(database.allows_virtual_table_explore, True) + def test_explore_database_id(self): + database = utils.get_example_database() + explore_database = utils.get_example_database() + + # test that explore_database_id is the regular database + # id if none is set in the extra + self.assertEqual(database.explore_database_id, database.id) + + # test that explore_database_id is correct if the extra is set + extra = database.get_extra() + extra["explore_database_id"] = explore_database.id + database.extra = json.dumps(extra) + self.assertEqual(database.explore_database_id, explore_database.id) + if __name__ == "__main__": unittest.main() diff --git a/tests/database_api_tests.py b/tests/database_api_tests.py index 1b9e1caa1d..d0e9ecf541 100644 --- a/tests/database_api_tests.py +++ b/tests/database_api_tests.py @@ -52,6 +52,7 @@ class DatabaseApiTests(SupersetTestCase): "allows_virtual_table_explore", "backend", "database_name", + "explore_database_id", "expose_in_sqllab", "force_ctas_schema", "function_names",