mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 04:34:19 +01:00
* add sqlite db * add .vscode to gitignore * add vtxo repo * add sqlite repos implementations * add sqlite in db/service * update go.mod * fix sqlite * move sqlite tests to service_test.go + fixes * integration tests using sqlite + properly close statements * implement GetRoundsIds * add "tx" table to store forfeits, connectors and congestion trees * add db max conn = 1 * upsert VTXO + fix onboarding * remove json tags * Fixes * Fix * fix lint * fix config.go * Fix rm config & open db only once * Update makefile --------- Co-authored-by: altafan <18440657+altafan@users.noreply.github.com>
46 lines
776 B
Go
46 lines
776 B
Go
package sqlitedb
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
_ "modernc.org/sqlite"
|
|
)
|
|
|
|
const (
|
|
driverName = "sqlite"
|
|
)
|
|
|
|
func OpenDb(dbPath string) (*sql.DB, error) {
|
|
dir := filepath.Dir(dbPath)
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
err = os.MkdirAll(dir, 0755)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to create directory: %v", err)
|
|
}
|
|
}
|
|
|
|
db, err := sql.Open(driverName, dbPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to open db: %w", err)
|
|
}
|
|
|
|
db.SetMaxOpenConns(1) // prevent concurrent writes
|
|
|
|
return db, nil
|
|
}
|
|
|
|
func extendArray[T any](arr []T, position int) []T {
|
|
if arr == nil {
|
|
return make([]T, position+1)
|
|
}
|
|
|
|
if len(arr) <= position {
|
|
return append(arr, make([]T, position-len(arr)+1)...)
|
|
}
|
|
|
|
return arr
|
|
}
|