Skip to content

Commit 0bb641a

Browse files
authored
Fix db browser (#254)
* Fix the updating of the schema browser * Make the lists in the schema browser always start at the top * Fix db browser in the metadata management for new items * Fix table discovery and minor refactor of metadata management
1 parent a1588af commit 0bb641a

File tree

3 files changed

+60
-36
lines changed

3 files changed

+60
-36
lines changed

daiquiri/metadata/assets/js/components/Management.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ const Management = () => {
3838
const handleModal = (type) => {
3939
setValues({
4040
type,
41-
schema: isEmpty(schemas) ? null : schemas[0].id,
42-
table: (isEmpty(schemas) || isEmpty(schemas[0].tables)) ? null : schemas[0].tables[0].id,
41+
schema: isEmpty(schemas) ? null : (activeItem?.type == 'schema' ? activeItem.id : schemas[0].id),
42+
table: (isEmpty(schemas) || isEmpty(schemas[0].tables)) ? null : (activeItem?.type == 'table' ? activeItem.id : schemas[0].tables[0].id),
4343
query_string: '',
4444
discover: true
4545
})

daiquiri/metadata/assets/js/components/Schemas.js

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useState } from 'react'
1+
import React, { useEffect, useState, useRef } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { isEmpty, isNil } from 'lodash'
@@ -12,6 +12,9 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
1212
const [visibleTables, setVisibleTables] = useState([])
1313
const [visibleColumns, setVisibleColumns] = useState([])
1414

15+
const refListTables = useRef(null)
16+
const refListColumns = useRef(null)
17+
1518
const initBrowser = () => {
1619
if (!isEmpty(schemas)) {
1720
const schema = schemas[0]
@@ -44,40 +47,57 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
4447
}
4548

4649
setOpenTable(table)
50+
if (refListTables.current) {
51+
refListTables.current.scrollTop = 0
52+
}
4753
setVisibleColumns((isNil(table) || isNil(table.columns)) ? [] : table.columns)
48-
49-
} else if (activeItem.type == 'table') {
50-
// search for the schema and table
51-
const [schema, table] = schemas.reduce((result, schema) => {
52-
const table = (schema.tables || []).find(t => isEqual(t, activeItem))
53-
return isNil(table) ? result : [schema, table]
54-
}, [])
55-
56-
if (schema) {
57-
setOpenSchema(schema)
58-
setVisibleTables(schema.tables)
54+
if (refListColumns.current) {
55+
refListColumns.current.scrollTop = 0
5956
}
6057

61-
if (table) {
62-
setOpenTable(table)
63-
setVisibleColumns(table.columns)
58+
} else if (activeItem.type == 'table') {
59+
if (!isNil(activeItem.schema)) {
60+
// this is a newly created table, search for the schema and table
61+
const [schema, table] = schemas.reduce((result, schema) => {
62+
return schema.id == activeItem.schema ? (
63+
[schema, (schema.tables || []).find(t => isEqual(t, activeItem))]
64+
) : result
65+
}, [] )
66+
67+
if (schema) {
68+
setOpenSchema(schema)
69+
setVisibleTables(schema.tables)
70+
}
71+
72+
if (table) {
73+
setOpenTable(table)
74+
setVisibleColumns(table.columns)
75+
}
76+
} else {
77+
setOpenTable(activeItem)
78+
setVisibleColumns(activeItem.columns)
79+
if (refListColumns.current) {
80+
refListColumns.current.scrollTop = 0
81+
}
6482
}
6583

6684
} else if (activeItem.type == 'column') {
67-
// search for the schema and the table for the column
68-
const [schema, table] = schemas.reduce((result, schema) => {
69-
const table = (schema.tables || []).find(t => (t.columns && t.columns.find(c => isEqual(c, activeItem))))
70-
return isNil(table) ? result : [schema, table]
71-
}, [])
72-
73-
if (schema) {
74-
setOpenSchema(schema)
75-
setVisibleTables(schema.tables)
76-
}
77-
78-
if (table) {
79-
setOpenTable(table)
80-
setVisibleColumns(table.columns)
85+
if (!isNil(activeItem.table)) {
86+
// this is a newly created column, search for the schema and table
87+
const [schema, table] = schemas.reduce((result, schema) => {
88+
const table = (schema.tables || []).find(t => (t.id == activeItem.table))
89+
return isNil(table) ? result : [schema, table]
90+
}, [])
91+
92+
if (schema) {
93+
setOpenSchema(schema)
94+
setVisibleTables(schema.tables)
95+
}
96+
97+
if (table) {
98+
setOpenTable(table)
99+
setVisibleColumns(table.columns)
100+
}
81101
}
82102
}
83103
}
@@ -117,7 +137,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
117137
>
118138
<div>{schema.name}</div>
119139
{
120-
openSchema && (openSchema.id == schema.id) && (
140+
openSchema && (isEqual(openSchema, schema)) && (
121141
<div className="ms-auto"><i className="bi bi-chevron-right"></i></div>
122142
)
123143
}
@@ -132,7 +152,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
132152
<div className="dq-browser-title">
133153
{gettext('Tables')}
134154
</div>
135-
<ul className="dq-browser-list">
155+
<ul className="dq-browser-list" ref={refListTables}>
136156
{
137157
visibleTables.map((table, index) => (
138158
<li key={index}>
@@ -144,7 +164,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
144164
>
145165
<div>{table.name}</div>
146166
{
147-
openTable && (openTable.id == table.id) && (
167+
openTable && (isEqual(openTable, table)) && (
148168
<div className="ms-auto"><i className="bi bi-chevron-right"></i></div>
149169
)
150170
}
@@ -159,7 +179,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
159179
<div className="dq-browser-title">
160180
{gettext('Columns')}
161181
</div>
162-
<ul className="dq-browser-list">
182+
<ul className="dq-browser-list" ref={refListColumns}>
163183
{
164184
visibleColumns.map((column, index) => (
165185
<li key={index}>

daiquiri/metadata/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ def admin_url(self):
244244

245245
def discover(self, adapter):
246246
metadata = adapter.fetch_table(self.schema.name, self.name)
247-
self.type = metadata['type']
247+
try:
248+
self.type = metadata['type']
249+
except KeyError:
250+
# if the table does not exist in the database then do nothing
251+
return
248252
self.nrows = adapter.fetch_nrows(self.schema.name, self.name)
249253
self.size = adapter.fetch_size(self.schema.name, self.name)
250254

0 commit comments

Comments
 (0)