Add sqlite-storage-cli

This commit is contained in:
Matt Joiner 2021-01-19 16:55:14 +11:00
parent 5920bcae89
commit 3e34763678
2 changed files with 44 additions and 2 deletions

View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"log"
"os"
"crawshaw.io/sqlite"
"github.com/alexflint/go-arg"
sqliteStorage "github.com/anacrolix/torrent/storage/sqlite"
)
type InitCommand struct {
Path string `arg:"positional"`
}
func main() {
err := mainErr()
if err != nil {
log.Printf("error in main: %v", err)
os.Exit(1)
}
}
func mainErr() error {
var args struct {
Init *InitCommand `arg:"subcommand"`
}
p := arg.MustParse(&args)
switch {
case args.Init != nil:
conn, err := sqlite.OpenConn(args.Init.Path, 0)
if err != nil {
return fmt.Errorf("opening sqlite conn: %w", err)
}
defer conn.Close()
return sqliteStorage.InitSchema(conn, 1<<14, true)
default:
p.Fail("expected subcommand")
panic("unreachable")
}
}

View File

@ -49,7 +49,7 @@ func initConn(conn conn, wal bool) error {
return nil
}
func initSchema(conn conn, pageSize int, triggers bool) error {
func InitSchema(conn conn, pageSize int, triggers bool) error {
if pageSize != 0 {
err := sqlitex.ExecScript(conn, fmt.Sprintf("pragma page_size=%d", pageSize))
if err != nil {
@ -249,7 +249,7 @@ func NewPool(opts NewPoolOpts) (_ ConnPool, _ ProviderOpts, err error) {
if opts.PageSize == 0 {
opts.PageSize = 1 << 14
}
err = initSchema(conn, opts.PageSize, true)
err = InitSchema(conn, opts.PageSize, true)
if err != nil {
return
}