Manager: convert DB integrity check to sqlc

Convert the database integrity check from GORM to sqlc.

No functional changes.

Ref: #104305
This commit is contained in:
Sybren A. Stüvel 2024-09-18 09:36:44 +02:00
parent 7b592950e4
commit af6f7103c4
2 changed files with 48 additions and 10 deletions

View File

@ -16,10 +16,6 @@ const (
integrityCheckTimeout = 10 * time.Second integrityCheckTimeout = 10 * time.Second
) )
type PragmaIntegrityCheckResult struct {
Description string `gorm:"column:integrity_check"`
}
type PragmaForeignKeyCheckResult struct { type PragmaForeignKeyCheckResult struct {
Table string `gorm:"column:table"` Table string `gorm:"column:table"`
RowID int `gorm:"column:rowid"` RowID int `gorm:"column:rowid"`
@ -93,13 +89,15 @@ func (db *DB) performIntegrityCheck(ctx context.Context) (ok bool) {
// //
// See https: //www.sqlite.org/pragma.html#pragma_integrity_check // See https: //www.sqlite.org/pragma.html#pragma_integrity_check
func (db *DB) pragmaIntegrityCheck(ctx context.Context) (ok bool) { func (db *DB) pragmaIntegrityCheck(ctx context.Context) (ok bool) {
var issues []PragmaIntegrityCheckResult queries, err := db.queries()
if err != nil {
log.Error().Err(err).Msg("database: could not obtain queries object")
return false
}
tx := db.gormDB.WithContext(ctx). issues, err := queries.PragmaIntegrityCheck(ctx)
Raw("PRAGMA integrity_check"). if err != nil {
Scan(&issues) log.Error().Err(err).Msg("database: error checking integrity")
if tx.Error != nil {
log.Error().Err(tx.Error).Msg("database: error checking integrity")
return false return false
} }

View File

@ -0,0 +1,40 @@
// Code MANUALLY written to extend the SQLC interface with some extra functions.
//
// This is to work around https://github.com/sqlc-dev/sqlc/issues/3237
package sqlc
import (
"context"
)
const pragmaIntegrityCheck = `PRAGMA integrity_check`
type PragmaIntegrityCheckResult struct {
Description string
}
func (q *Queries) PragmaIntegrityCheck(ctx context.Context) ([]PragmaIntegrityCheckResult, error) {
rows, err := q.db.QueryContext(ctx, pragmaIntegrityCheck)
if err != nil {
return nil, err
}
defer rows.Close()
var items []PragmaIntegrityCheckResult
for rows.Next() {
var i PragmaIntegrityCheckResult
if err := rows.Scan(
&i.Description,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}