parent
476d4059bf
commit
a015408486
@ -16,13 +16,6 @@ const (
|
||||
integrityCheckTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
type PragmaForeignKeyCheckResult struct {
|
||||
Table string `gorm:"column:table"`
|
||||
RowID int `gorm:"column:rowid"`
|
||||
Parent string `gorm:"column:parent"`
|
||||
FKID int `gorm:"column:fkid"`
|
||||
}
|
||||
|
||||
// PeriodicIntegrityCheck periodically checks the database integrity.
|
||||
// This function only returns when the context is done.
|
||||
func (db *DB) PeriodicIntegrityCheck(
|
||||
@ -127,13 +120,11 @@ func (db *DB) pragmaIntegrityCheck(ctx context.Context) (ok bool) {
|
||||
//
|
||||
// See https: //www.sqlite.org/pragma.html#pragma_foreign_key_check
|
||||
func (db *DB) pragmaForeignKeyCheck(ctx context.Context) (ok bool) {
|
||||
var issues []PragmaForeignKeyCheckResult
|
||||
queries := db.queries()
|
||||
|
||||
tx := db.gormDB.WithContext(ctx).
|
||||
Raw("PRAGMA foreign_key_check").
|
||||
Scan(&issues)
|
||||
if tx.Error != nil {
|
||||
log.Error().Err(tx.Error).Msg("database: error checking foreign keys")
|
||||
issues, err := queries.PragmaForeignKeyCheck(ctx)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("database: error checking foreign keys")
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -63,3 +63,40 @@ func (q *Queries) PragmaForeignKeysGet(ctx context.Context) (bool, error) {
|
||||
err := row.Scan(&fkEnabled)
|
||||
return fkEnabled, err
|
||||
}
|
||||
|
||||
const pragmaForeignKeyCheck = `PRAGMA foreign_key_check`
|
||||
|
||||
type PragmaForeignKeyCheckResult struct {
|
||||
Table string
|
||||
RowID int
|
||||
Parent string
|
||||
FKID int
|
||||
}
|
||||
|
||||
func (q *Queries) PragmaForeignKeyCheck(ctx context.Context) ([]PragmaForeignKeyCheckResult, error) {
|
||||
rows, err := q.db.QueryContext(ctx, pragmaForeignKeyCheck)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []PragmaForeignKeyCheckResult
|
||||
for rows.Next() {
|
||||
var i PragmaForeignKeyCheckResult
|
||||
if err := rows.Scan(
|
||||
&i.Table,
|
||||
&i.RowID,
|
||||
&i.Parent,
|
||||
&i.FKID,
|
||||
); 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