Skip to content

Commit

Permalink
Remove GORM from production code (#1372)
Browse files Browse the repository at this point in the history
This removes GORM from our production code entirely. It's now only used
in tests.

Therefore `db` was renamed to `gormDB` to make it clear that it's no
longer to be used.
`bMain` is now `db`.
  • Loading branch information
n8maninger committed Jul 15, 2024
2 parents cdad429 + 0c50b3b commit d61f70a
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 321 deletions.
6 changes: 3 additions & 3 deletions stores/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Accounts returns all accounts from the db.
func (s *SQLStore) Accounts(ctx context.Context) (accounts []api.Account, err error) {
err = s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
accounts, err = tx.Accounts(ctx)
return err
})
Expand All @@ -21,15 +21,15 @@ func (s *SQLStore) Accounts(ctx context.Context) (accounts []api.Account, err er
// sync all accounts after an unclean shutdown and the bus will know not to
// apply drift.
func (s *SQLStore) SetUncleanShutdown(ctx context.Context) error {
return s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.SetUncleanShutdown(ctx)
})
}

// SaveAccounts saves the given accounts in the db, overwriting any existing
// ones.
func (s *SQLStore) SaveAccounts(ctx context.Context, accounts []api.Account) error {
return s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.SaveAccounts(ctx, accounts)
})
}
6 changes: 3 additions & 3 deletions stores/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
)

func (s *SQLStore) Autopilots(ctx context.Context) (aps []api.Autopilot, _ error) {
err := s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
err := s.db.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
aps, err = tx.Autopilots(ctx)
return
})
return aps, err
}

func (s *SQLStore) Autopilot(ctx context.Context, id string) (ap api.Autopilot, _ error) {
err := s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
err := s.db.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
ap, err = tx.Autopilot(ctx, id)
return
})
Expand All @@ -32,7 +32,7 @@ func (s *SQLStore) UpdateAutopilot(ctx context.Context, ap api.Autopilot) error
if err := ap.Config.Validate(); err != nil {
return err
}
return s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.UpdateAutopilot(ctx, ap)
})
}
23 changes: 8 additions & 15 deletions stores/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,18 @@ var (
)

// ChainIndex returns the last stored chain index.
func (ss *SQLStore) ChainIndex(ctx context.Context) (types.ChainIndex, error) {
var ci dbConsensusInfo
if err := ss.db.
WithContext(ctx).
Where(&dbConsensusInfo{Model: Model{ID: consensusInfoID}}).
FirstOrCreate(&ci).
Error; err != nil {
return types.ChainIndex{}, err
}
return types.ChainIndex{
Height: ci.Height,
ID: types.BlockID(ci.BlockID),
}, nil
func (s *SQLStore) ChainIndex(ctx context.Context) (ci types.ChainIndex, err error) {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
ci, err = tx.Tip(ctx)
return err
})
return
}

// ProcessChainUpdate returns a callback function that process a chain update
// inside a transaction.
func (s *SQLStore) ProcessChainUpdate(ctx context.Context, applyFn chain.ApplyChainUpdateFn) error {
return s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.ProcessChainUpdate(ctx, applyFn)
})
}
Expand All @@ -46,7 +39,7 @@ func (s *SQLStore) UpdateChainState(reverted []chain.RevertUpdate, applied []cha

// ResetChainState deletes all chain data in the database.
func (s *SQLStore) ResetChainState(ctx context.Context) error {
return s.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.ResetChainState(ctx)
})
}
63 changes: 24 additions & 39 deletions stores/hostdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,6 @@ import (
"gorm.io/gorm/clause"
)

const (
// consensusInfoID defines the primary key of the entry in the consensusInfo
// table.
consensusInfoID = 1
)

var (
ErrNegativeMaxDowntime = errors.New("max downtime can not be negative")
)
Expand Down Expand Up @@ -120,12 +114,6 @@ type (
Hosts []dbHost `gorm:"many2many:host_blocklist_entry_hosts;constraint:OnDelete:CASCADE"`
}

dbConsensusInfo struct {
Model
Height uint64
BlockID hash256
}

// dbAnnouncement is a table used for storing all announcements. It
// doesn't have any relations to dbHost which means it won't
// automatically prune when a host is deleted.
Expand All @@ -151,9 +139,6 @@ type (
// TableName implements the gorm.Tabler interface.
func (dbAnnouncement) TableName() string { return "host_announcements" }

// TableName implements the gorm.Tabler interface.
func (dbConsensusInfo) TableName() string { return "consensus_infos" }

// TableName implements the gorm.Tabler interface.
func (dbHost) TableName() string { return "hosts" }

Expand Down Expand Up @@ -271,8 +256,8 @@ func (e *dbBlocklistEntry) blocks(h dbHost) bool {
}

// Host returns information about a host.
func (ss *SQLStore) Host(ctx context.Context, hostKey types.PublicKey) (api.Host, error) {
hosts, err := ss.SearchHosts(ctx, "", api.HostFilterModeAll, api.UsabilityFilterModeAll, "", []types.PublicKey{hostKey}, 0, 1)
func (s *SQLStore) Host(ctx context.Context, hostKey types.PublicKey) (api.Host, error) {
hosts, err := s.SearchHosts(ctx, "", api.HostFilterModeAll, api.UsabilityFilterModeAll, "", []types.PublicKey{hostKey}, 0, 1)
if err != nil {
return api.Host{}, err
} else if len(hosts) == 0 {
Expand All @@ -282,15 +267,15 @@ func (ss *SQLStore) Host(ctx context.Context, hostKey types.PublicKey) (api.Host
}
}

func (ss *SQLStore) UpdateHostCheck(ctx context.Context, autopilotID string, hk types.PublicKey, hc api.HostCheck) (err error) {
return ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) UpdateHostCheck(ctx context.Context, autopilotID string, hk types.PublicKey, hc api.HostCheck) (err error) {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.UpdateHostCheck(ctx, autopilotID, hk, hc)
})
}

// HostsForScanning returns the address of hosts for scanning.
func (ss *SQLStore) HostsForScanning(ctx context.Context, maxLastScan time.Time, offset, limit int) (hosts []api.HostAddress, err error) {
err = ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) HostsForScanning(ctx context.Context, maxLastScan time.Time, offset, limit int) (hosts []api.HostAddress, err error) {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
hosts, err = tx.HostsForScanning(ctx, maxLastScan, offset, limit)
return err
})
Expand All @@ -306,77 +291,77 @@ func (s *SQLStore) ResetLostSectors(ctx context.Context, hk types.PublicKey) err
})
}

func (ss *SQLStore) SearchHosts(ctx context.Context, autopilotID, filterMode, usabilityMode, addressContains string, keyIn []types.PublicKey, offset, limit int) ([]api.Host, error) {
func (s *SQLStore) SearchHosts(ctx context.Context, autopilotID, filterMode, usabilityMode, addressContains string, keyIn []types.PublicKey, offset, limit int) ([]api.Host, error) {
var hosts []api.Host
err := ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
err := s.db.Transaction(ctx, func(tx sql.DatabaseTx) (err error) {
hosts, err = tx.SearchHosts(ctx, autopilotID, filterMode, usabilityMode, addressContains, keyIn, offset, limit)
return
})
return hosts, err
}

// Hosts returns non-blocked hosts at given offset and limit.
func (ss *SQLStore) Hosts(ctx context.Context, offset, limit int) ([]api.Host, error) {
return ss.SearchHosts(ctx, "", api.HostFilterModeAllowed, api.UsabilityFilterModeAll, "", nil, offset, limit)
func (s *SQLStore) Hosts(ctx context.Context, offset, limit int) ([]api.Host, error) {
return s.SearchHosts(ctx, "", api.HostFilterModeAllowed, api.UsabilityFilterModeAll, "", nil, offset, limit)
}

func (ss *SQLStore) RemoveOfflineHosts(ctx context.Context, minRecentFailures uint64, maxDowntime time.Duration) (removed uint64, err error) {
func (s *SQLStore) RemoveOfflineHosts(ctx context.Context, minRecentFailures uint64, maxDowntime time.Duration) (removed uint64, err error) {
// sanity check 'maxDowntime'
if maxDowntime < 0 {
return 0, ErrNegativeMaxDowntime
}
err = ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
n, err := tx.RemoveOfflineHosts(ctx, minRecentFailures, maxDowntime)
removed = uint64(n)
return err
})
return
}

func (ss *SQLStore) UpdateHostAllowlistEntries(ctx context.Context, add, remove []types.PublicKey, clear bool) (err error) {
func (s *SQLStore) UpdateHostAllowlistEntries(ctx context.Context, add, remove []types.PublicKey, clear bool) (err error) {
// nothing to do
if len(add)+len(remove) == 0 && !clear {
return nil
}
return ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.UpdateHostAllowlistEntries(ctx, add, remove, clear)
})
}

func (ss *SQLStore) UpdateHostBlocklistEntries(ctx context.Context, add, remove []string, clear bool) (err error) {
func (s *SQLStore) UpdateHostBlocklistEntries(ctx context.Context, add, remove []string, clear bool) (err error) {
// nothing to do
if len(add)+len(remove) == 0 && !clear {
return nil
}
return ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.UpdateHostBlocklistEntries(ctx, add, remove, clear)
})
}

func (ss *SQLStore) HostAllowlist(ctx context.Context) (allowlist []types.PublicKey, err error) {
err = ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) HostAllowlist(ctx context.Context) (allowlist []types.PublicKey, err error) {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
allowlist, err = tx.HostAllowlist(ctx)
return err
})
return
}

func (ss *SQLStore) HostBlocklist(ctx context.Context) (blocklist []string, err error) {
err = ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) HostBlocklist(ctx context.Context) (blocklist []string, err error) {
err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
blocklist, err = tx.HostBlocklist(ctx)
return err
})
return
}

func (ss *SQLStore) RecordHostScans(ctx context.Context, scans []api.HostScan) error {
return ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) RecordHostScans(ctx context.Context, scans []api.HostScan) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.RecordHostScans(ctx, scans)
})
}

func (ss *SQLStore) RecordPriceTables(ctx context.Context, priceTableUpdate []api.HostPriceTableUpdate) error {
return ss.bMain.Transaction(ctx, func(tx sql.DatabaseTx) error {
func (s *SQLStore) RecordPriceTables(ctx context.Context, priceTableUpdate []api.HostPriceTableUpdate) error {
return s.db.Transaction(ctx, func(tx sql.DatabaseTx) error {
return tx.RecordPriceTables(ctx, priceTableUpdate)
})
}
Expand Down
Loading

0 comments on commit d61f70a

Please sign in to comment.