···23import (
4 "context"
5- "sync"
67 "gorm.io/gorm"
8 "gorm.io/gorm/clause"
···1011type DB struct {
12 cli *gorm.DB
13- mu sync.Mutex
14}
1516func NewDB(cli *gorm.DB) *DB {
17 return &DB{
18 cli: cli,
19- mu: sync.Mutex{},
20 }
21}
2223func (db *DB) Create(ctx context.Context, value any, clauses []clause.Expression) *gorm.DB {
24- db.mu.Lock()
25- defer db.mu.Unlock()
26 return db.cli.WithContext(ctx).Clauses(clauses...).Create(value)
27}
2829func (db *DB) Save(ctx context.Context, value any, clauses []clause.Expression) *gorm.DB {
30- db.mu.Lock()
31- defer db.mu.Unlock()
32 return db.cli.WithContext(ctx).Clauses(clauses...).Save(value)
33}
3435func (db *DB) Exec(ctx context.Context, sql string, clauses []clause.Expression, values ...any) *gorm.DB {
36- db.mu.Lock()
37- defer db.mu.Unlock()
38 return db.cli.WithContext(ctx).Clauses(clauses...).Exec(sql, values...)
39}
40···47}
4849func (db *DB) Delete(ctx context.Context, value any, clauses []clause.Expression) *gorm.DB {
50- db.mu.Lock()
51- defer db.mu.Unlock()
52 return db.cli.WithContext(ctx).Clauses(clauses...).Delete(value)
53}
54···56 return db.cli.WithContext(ctx).First(dest, conds...)
57}
5859-// TODO: this isn't actually good. we can commit even if the db is locked here. this is probably okay for the time being, but need to figure
60-// out a better solution. right now we only do this whenever we're importing a repo though so i'm mostly not worried, but it's still bad.
61-// e.g. when we do apply writes we should also be using a transcation but we don't right now
62-func (db *DB) BeginDangerously(ctx context.Context) *gorm.DB {
63 return db.cli.WithContext(ctx).Begin()
64}
6566-func (db *DB) Lock() {
67- db.mu.Lock()
68-}
69-70-func (db *DB) Unlock() {
71- db.mu.Unlock()
72}