mirror of
https://github.com/hanzoai/base.git
synced 2026-10-21 03:41:50 +00:00
The admin had no storage surface at all: nothing in ui-react named an engine, a file or a size, so the one question a growing Base raises — how much is in here and where is it — could only be answered by finding the process's data directory. `/v1/database` answers it, and Settings -> Data renders it. core.DescribeDatabase measures the engine, the files while the app holds them, and a count per collection through CountRecords, so both engines answer the same questions and the answer has one shape. A collection the engine refuses to count reads as unknown rather than as empty, and is logged, so one broken view leaves the rest of the report standing rather than taking the page down. core.ReclaimDatabase is the one thing the page can DO, and it does something real: it rewrites both databases without the pages deleted records left behind, folds the log back into them, refreshes the statistics, and returns the size either side so the page reports what was freed rather than claiming something was. Driven in a browser it took a database from 9.0 MB to 1.1 MB with the record counts unchanged. The rewrite goes BEFORE the fold. A VACUUM lands through the log like any other write, so folding first leaves the whole rewrite sitting in the log and the app holding more than it started with — 2,068,480 bytes to 2,665,912 that way, and to 593,920 this way. Both orders shrink something, which is why TestDatabaseReclaim measures each database separately: an earlier version was satisfied by 8 KB off the auxiliary log and went on passing with the rewrite removed entirely. There is no switch, because whether an instance belongs on a server is the host's question and Base stopped having an opinion about it. The page states which of the two it is on and what the other would mean, and it does not name a backend Base cannot open — a disabled control for one would be the datastore tier constant again, in a place a customer can see. core.LocalDatabase is now the single place that asks whether the app holds the database; backup asked it inline and now asks it there. tests/engine gains the cross-engine cases, which CI already runs against PostgreSQL 18 — the LLM.md line saying it does not was two releases stale. The third copy of a byte formatter went into ui-react/src/lib/format.ts and the two existing pages now read from it.
37 lines
991 B
Go
37 lines
991 B
Go
package apis
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/hanzoai/base/core"
|
|
"github.com/hanzoai/base/tools/router"
|
|
)
|
|
|
|
// bindDatabaseApi registers the database api endpoints.
|
|
func bindDatabaseApi(app core.App, rg *router.RouterGroup[*core.RequestEvent]) {
|
|
subGroup := rg.Group("/database").Bind(RequireSuperuserAuth())
|
|
subGroup.GET("", databaseRead)
|
|
subGroup.POST("/reclaim", databaseReclaim)
|
|
}
|
|
|
|
func databaseRead(e *core.RequestEvent) error {
|
|
db, err := core.DescribeDatabase(e.App)
|
|
if err != nil {
|
|
return firstApiError(err, e.InternalServerError("Failed to read the database.", err))
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, db)
|
|
}
|
|
|
|
func databaseReclaim(e *core.RequestEvent) error {
|
|
before, after, err := core.ReclaimDatabase(e.App)
|
|
if err != nil {
|
|
return firstApiError(err, e.InternalServerError("Failed to reclaim the unused space. Raw error:\n"+err.Error(), nil))
|
|
}
|
|
|
|
return e.JSON(http.StatusOK, struct {
|
|
Before int64 `json:"before"`
|
|
After int64 `json:"after"`
|
|
}{before, after})
|
|
}
|