storage/sqlite/sqlite-storage-cli/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ storage/sqlite/sqlite-storage.go | 4 ++-- diff --git a/storage/sqlite/sqlite-storage-cli/main.go b/storage/sqlite/sqlite-storage-cli/main.go new file mode 100644 index 0000000000000000000000000000000000000000..5f315743d70a8e51b3fb87649464d92c0f3952e6 --- /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 cb2ab1b973533764a6f79a85b8f9d1915ef15e8a..8ec4d5a6d42e6f3e48790bd5f317f5a12ff5e550 100644 --- a/storage/sqlite/sqlite-storage.go +++ b/storage/sqlite/sqlite-storage.go @@ -49,7 +49,7 @@ } 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 @@ if !opts.DontInitSchema { if opts.PageSize == 0 { opts.PageSize = 1 << 14 } - err = initSchema(conn, opts.PageSize, true) + err = InitSchema(conn, opts.PageSize, true) if err != nil { return }