Skip to content

Commit 6aadc6e

Browse files
authoredSep 9, 2016
Simplifying the flow to add a table (#1068)
When specifying a table reference that can not be found, the system used to still create the object, which would result in confusion and bad error messages down the line. Now it will fail and not create the object. I also removed fields that are not necessary to worry about when initially creating the table.
1 parent 8eb4cbf commit 6aadc6e

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed
 

‎caravel/models.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -889,16 +889,17 @@ def query( # sqla
889889
return QueryResult(
890890
df=df, duration=datetime.now() - qry_start_dttm, query=sql)
891891

892+
def get_sqla_table_object(self):
893+
return self.database.get_table(self.table_name, schema=self.schema)
894+
892895
def fetch_metadata(self):
893896
"""Fetches the metadata for the table and merges it in"""
894897
try:
895-
table = self.database.get_table(self.table_name, schema=self.schema)
896-
except Exception as e:
897-
flasher(str(e))
898-
flasher(
898+
table = self.get_sqla_table_object()
899+
except Exception:
900+
raise Exception(
899901
"Table doesn't seem to exist in the specified database, "
900-
"couldn't fetch column information", "danger")
901-
return
902+
"couldn't fetch column information")
902903

903904
TC = TableColumn # noqa shortcut to class
904905
M = SqlMetric # noqa

‎caravel/views.py

+22-13
Original file line numberDiff line numberDiff line change
@@ -526,24 +526,24 @@ class TableModelView(CaravelModelView, DeleteMixin): # noqa
526526
'changed_by_', 'changed_on_']
527527
order_columns = [
528528
'table_link', 'database', 'is_featured', 'changed_on_']
529-
add_columns = [
530-
'table_name', 'database', 'schema',
531-
'default_endpoint', 'offset', 'cache_timeout']
529+
add_columns = ['table_name', 'database', 'schema']
532530
edit_columns = [
533531
'table_name', 'sql', 'is_featured', 'database', 'schema',
534532
'description', 'owner',
535533
'main_dttm_col', 'default_endpoint', 'offset', 'cache_timeout']
536534
related_views = [TableColumnInlineView, SqlMetricInlineView]
537535
base_order = ('changed_on', 'desc')
538536
description_columns = {
539-
'offset': "Timezone offset (in hours) for this datasource",
540-
'schema': (
537+
'offset': _("Timezone offset (in hours) for this datasource"),
538+
'table_name': _(
539+
"Name of the table that exists in the source database"),
540+
'schema': _(
541541
"Schema, as used only in some databases like Postgres, Redshift "
542542
"and DB2"),
543543
'description': Markup(
544544
"Supports <a href='https://daringfireball.net/projects/markdown/'>"
545545
"markdown</a>"),
546-
'sql': (
546+
'sql': _(
547547
"This fields acts a Caravel view, meaning that Caravel will "
548548
"run a query against this string as a subquery."
549549
),
@@ -561,17 +561,26 @@ class TableModelView(CaravelModelView, DeleteMixin): # noqa
561561
'cache_timeout': _("Cache Timeout"),
562562
}
563563

564-
def post_add(self, table):
565-
table_name = table.table_name
564+
def pre_add(self, table):
565+
# Fail before adding if the table can't be found
566566
try:
567-
table.fetch_metadata()
567+
table.get_sqla_table_object()
568568
except Exception as e:
569569
logging.exception(e)
570-
flash(
571-
"Table [{}] doesn't seem to exist, "
572-
"couldn't fetch metadata".format(table_name),
573-
"danger")
570+
raise Exception(
571+
"Table [{}] could not be found, "
572+
"please double check your "
573+
"database connection, schema, and "
574+
"table name".format(table.table_name))
575+
576+
def post_add(self, table):
577+
table.fetch_metadata()
574578
utils.merge_perm(sm, 'datasource_access', table.perm)
579+
flash(_(
580+
"The table was created. As part of this two phase configuration "
581+
"process, you should now click the edit button by "
582+
"the new table to configure it."),
583+
"info")
575584

576585
def post_update(self, table):
577586
self.post_add(table)

0 commit comments

Comments
 (0)
Please sign in to comment.