Skip to content

Commit

Permalink
Add all_tables endpoint to allow airpal / superset perm sync.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdan Kyryliuk committed Nov 17, 2016
1 parent ed3d44d commit ec6a120
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions superset/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,31 @@ def activity_per_day(self):
ccount for dt, ccount in qry if dt}
return Response(json.dumps(payload), mimetype="application/json")

@api
@has_access_api
@expose("/all_tables/<db_id>")
def all_tables(self, db_id):
"""Endpoint that returns all tables and views from the database"""
database = (
db.session
.query(models.Database)
.filter_by(id=db_id)
.one()
)
all_tables = []
all_views = []
schemas = database.all_schema_names()
for schema in schemas:
all_tables.extend(database.all_table_names(schema=schema))
all_views.extend(database.all_view_names(schema=schema))
if not schemas:
all_tables.extend(database.all_table_names())
all_views.extend(database.all_view_names())

return Response(
json.dumps({"tables": all_tables, "views": all_views}),
mimetype="application/json")

@api
@has_access_api
@expose("/tables/<db_id>/<schema>")
Expand Down
11 changes: 10 additions & 1 deletion tests/core_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,19 @@ def test_table_metadata(self):

def test_fetch_datasource_metadata(self):
self.login(username='admin')
url = '/superset/fetch_datasource_metadata?datasource_type=table&datasource_id=1';
url = '/superset/fetch_datasource_metadata?datasource_type=table&' \
'datasource_id=1'
resp = json.loads(self.get_resp(url))
self.assertEqual(len(resp['field_options']), 19)

def test_fetch_all_tables(self):
self.login(username='admin')
database = self.get_main_database(db.session)
url = '/superset/all_tables/{}'.format(database.id)
resp = json.loads(self.get_resp(url))
self.assertIn('tables', resp)
self.assertIn('views', resp)


if __name__ == '__main__':
unittest.main()
Expand Down

0 comments on commit ec6a120

Please sign in to comment.