From 3e34763678a3147bcc624e6a48e006b1430c91b1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 19 Jan 2021 16:55:14 +1100 Subject: [PATCH] Add sqlite-storage-cli --- storage/sqlite/sqlite-storage-cli/main.go | 42 +++++++++++++++++++++++ storage/sqlite/sqlite-storage.go | 4 +-- 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 storage/sqlite/sqlite-storage-cli/main.go diff --git a/storage/sqlite/sqlite-storage-cli/main.go b/storage/sqlite/sqlite-storage-cli/main.go new file mode 100644 index 00000000..5f315743 --- /dev/null +++ b/storage/sqlite/sqlite-storage-cli/main.go @@ -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") + } +} diff --git a/storage/sqlite/sqlite-storage.go b/storage/sqlite/sqlite-storage.go index cb2ab1b9..8ec4d5a6 100644 --- a/storage/sqlite/sqlite-storage.go +++ b/storage/sqlite/sqlite-storage.go @@ -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 } -- 2.48.1