buildr/modules/state/db.go
Peter 5af8ddceab
Some checks failed
continuous-integration/drone/push Build is failing
refactor: rework plugins to track their state
- introduce individual commands to manage plugins
- store plugin in state to skip loading them to memory every time
2023-06-29 20:14:52 +02:00

45 lines
889 B
Go

package state
import (
"context"
"fmt"
"code.icb4dc0.de/buildr/buildr/modules/state/ent"
"entgo.io/ent/dialect"
)
func NewDB(ctx context.Context, stateFilePath string) (*DB, error) {
client, err := ent.Open(dialect.SQLite, fmt.Sprintf("file:%s?_fk=1&_pragma=foreign_keys(1)", stateFilePath))
if err != nil {
return nil, fmt.Errorf("failed to open SQLite database: %w at %s", err, stateFilePath)
}
if err := client.Schema.Create(ctx); err != nil {
return nil, fmt.Errorf("failed to create schema: %w at %s", err, stateFilePath)
}
db := &DB{
client: client,
Plugins: NewEntPluginsRepo(client),
}
if stateStore, err := NewEntStore(ctx, client); err != nil {
return nil, err
} else {
db.State = stateStore
}
return db, nil
}
type DB struct {
client *ent.Client
State Store
Plugins Plugins
}
func (db *DB) Close() error {
return db.client.Close()
}