Skip to content

Commit

Permalink
Merge pull request #295 from duyet/chore/ui
Browse files Browse the repository at this point in the history
feat: /database page redirect invalid database to the first one existing
  • Loading branch information
duyet authored Jul 19, 2024
2 parents 806de8f + a48bcdd commit 12f1726
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
42 changes: 30 additions & 12 deletions app/database/[database]/breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
} from '@/components/ui/dropdown-menu'
import { fetchDataWithCache } from '@/lib/clickhouse'

import { redirect } from 'next/navigation'
import { listDatabases } from '../queries'

interface Props {
Expand All @@ -30,20 +31,20 @@ interface DatabaseCount {
}

export async function DatabaseBreadcrumb({ database }: Props) {
let databases: DatabaseCount[] = []

try {
// List database names and number of tables
const { data: databases }: { data: DatabaseCount[] } =
await fetchDataWithCache()({
query: listDatabases,
})

if (!databases.length) {
return (
<ErrorAlert title="Message" message="Empty" query={listDatabases} />
)
}

return <Internal current={database} databases={databases} />
const data = (await fetchDataWithCache()({
query: listDatabases,
clickhouse_settings: {
use_query_cache: 1,
query_cache_system_table_handling: 'save',
query_cache_ttl: 300,
},
})) satisfies { data: DatabaseCount[] }

databases = data.data
} catch (e: any) {
return (
<ErrorAlert
Expand All @@ -53,6 +54,23 @@ export async function DatabaseBreadcrumb({ database }: Props) {
/>
)
}

if (!databases.length) {
return (
<ErrorAlert
title="Message"
message="No database found"
query={listDatabases}
/>
)
}

// Current database not found in database list
if (!databases.find((db) => db.name === database)) {
redirect('/database/' + databases[0].name)
}

return <Internal current={database} databases={databases} />
}

export async function DatabaseBreadcrumbSkeleton({ database }: Props) {
Expand Down
3 changes: 1 addition & 2 deletions app/database/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ export const listDatabases = `
engine
FROM system.tables
)
SELECT d.name as name,
countDistinct(t.table) as count
FROM system.databases AS d
LEFT JOIN tables_from_tables AS t USING database
JOIN tables_from_tables AS t USING database
WHERE d.engine != 'Memory'
GROUP BY d.name
`
Expand Down

1 comment on commit 12f1726

@vercel
Copy link

@vercel vercel bot commented on 12f1726 Jul 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.