Peter
5af8ddceab
Some checks failed
continuous-integration/drone/push Build is failing
- introduce individual commands to manage plugins - store plugin in state to skip loading them to memory every time
45 lines
889 B
Go
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()
|
|
}
|