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:
parent
7b592950e4
commit
af6f7103c4
@ -16,10 +16,6 @@ const (
|
||||
integrityCheckTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type PragmaIntegrityCheckResult struct {
|
||||
Description string `gorm:"column:integrity_check"`
|
||||
}
|
||||
|
||||
type PragmaForeignKeyCheckResult struct {
|
||||
Table string `gorm:"column:table"`
|
||||
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
|
||||
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).
|
||||
Raw("PRAGMA integrity_check").
|
||||
Scan(&issues)
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("database: error checking integrity")
|
||||
issues, err := queries.PragmaIntegrityCheck(ctx)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("database: error checking integrity")
|
||||
return false
|
||||
}
|
||||
|
||||
|
40
internal/manager/persistence/sqlc/integrity.go
Normal file
40
internal/manager/persistence/sqlc/integrity.go
Normal 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
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user