Skip to content

Commit

Permalink
Fix db browser (#254)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kimakan authored Jan 27, 2025
1 parent a1588af commit 0bb641a
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 36 deletions.
4 changes: 2 additions & 2 deletions daiquiri/metadata/assets/js/components/Management.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ const Management = () => {
const handleModal = (type) => {
setValues({
type,
schema: isEmpty(schemas) ? null : schemas[0].id,
table: (isEmpty(schemas) || isEmpty(schemas[0].tables)) ? null : schemas[0].tables[0].id,
schema: isEmpty(schemas) ? null : (activeItem?.type == 'schema' ? activeItem.id : schemas[0].id),
table: (isEmpty(schemas) || isEmpty(schemas[0].tables)) ? null : (activeItem?.type == 'table' ? activeItem.id : schemas[0].tables[0].id),
query_string: '',
discover: true
})
Expand Down
86 changes: 53 additions & 33 deletions daiquiri/metadata/assets/js/components/Schemas.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useState, useRef } from 'react'
import PropTypes from 'prop-types'
import classNames from 'classnames'
import { isEmpty, isNil } from 'lodash'
Expand All @@ -12,6 +12,9 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
const [visibleTables, setVisibleTables] = useState([])
const [visibleColumns, setVisibleColumns] = useState([])

const refListTables = useRef(null)
const refListColumns = useRef(null)

const initBrowser = () => {
if (!isEmpty(schemas)) {
const schema = schemas[0]
Expand Down Expand Up @@ -44,40 +47,57 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
}

setOpenTable(table)
if (refListTables.current) {
refListTables.current.scrollTop = 0
}
setVisibleColumns((isNil(table) || isNil(table.columns)) ? [] : table.columns)

} else if (activeItem.type == 'table') {
// search for the schema and table
const [schema, table] = schemas.reduce((result, schema) => {
const table = (schema.tables || []).find(t => isEqual(t, activeItem))
return isNil(table) ? result : [schema, table]
}, [])

if (schema) {
setOpenSchema(schema)
setVisibleTables(schema.tables)
if (refListColumns.current) {
refListColumns.current.scrollTop = 0
}

if (table) {
setOpenTable(table)
setVisibleColumns(table.columns)
} else if (activeItem.type == 'table') {
if (!isNil(activeItem.schema)) {
// this is a newly created table, search for the schema and table
const [schema, table] = schemas.reduce((result, schema) => {
return schema.id == activeItem.schema ? (
[schema, (schema.tables || []).find(t => isEqual(t, activeItem))]
) : result
}, [] )

if (schema) {
setOpenSchema(schema)
setVisibleTables(schema.tables)
}

if (table) {
setOpenTable(table)
setVisibleColumns(table.columns)
}
} else {
setOpenTable(activeItem)
setVisibleColumns(activeItem.columns)
if (refListColumns.current) {
refListColumns.current.scrollTop = 0
}
}

} else if (activeItem.type == 'column') {
// search for the schema and the table for the column
const [schema, table] = schemas.reduce((result, schema) => {
const table = (schema.tables || []).find(t => (t.columns && t.columns.find(c => isEqual(c, activeItem))))
return isNil(table) ? result : [schema, table]
}, [])

if (schema) {
setOpenSchema(schema)
setVisibleTables(schema.tables)
}

if (table) {
setOpenTable(table)
setVisibleColumns(table.columns)
if (!isNil(activeItem.table)) {
// this is a newly created column, search for the schema and table
const [schema, table] = schemas.reduce((result, schema) => {
const table = (schema.tables || []).find(t => (t.id == activeItem.table))
return isNil(table) ? result : [schema, table]
}, [])

if (schema) {
setOpenSchema(schema)
setVisibleTables(schema.tables)
}

if (table) {
setOpenTable(table)
setVisibleColumns(table.columns)
}
}
}
}
Expand Down Expand Up @@ -117,7 +137,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
>
<div>{schema.name}</div>
{
openSchema && (openSchema.id == schema.id) && (
openSchema && (isEqual(openSchema, schema)) && (
<div className="ms-auto"><i className="bi bi-chevron-right"></i></div>
)
}
Expand All @@ -132,7 +152,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
<div className="dq-browser-title">
{gettext('Tables')}
</div>
<ul className="dq-browser-list">
<ul className="dq-browser-list" ref={refListTables}>
{
visibleTables.map((table, index) => (
<li key={index}>
Expand All @@ -144,7 +164,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
>
<div>{table.name}</div>
{
openTable && (openTable.id == table.id) && (
openTable && (isEqual(openTable, table)) && (
<div className="ms-auto"><i className="bi bi-chevron-right"></i></div>
)
}
Expand All @@ -159,7 +179,7 @@ const Schemas = ({ schemas, activeItem, setActiveItem, getTooltip, onDoubleClick
<div className="dq-browser-title">
{gettext('Columns')}
</div>
<ul className="dq-browser-list">
<ul className="dq-browser-list" ref={refListColumns}>
{
visibleColumns.map((column, index) => (
<li key={index}>
Expand Down
6 changes: 5 additions & 1 deletion daiquiri/metadata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,11 @@ def admin_url(self):

def discover(self, adapter):
metadata = adapter.fetch_table(self.schema.name, self.name)
self.type = metadata['type']
try:
self.type = metadata['type']
except KeyError:
# if the table does not exist in the database then do nothing
return
self.nrows = adapter.fetch_nrows(self.schema.name, self.name)
self.size = adapter.fetch_size(self.schema.name, self.name)

Expand Down

0 comments on commit 0bb641a

Please sign in to comment.