]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add sqlite-storage-cli
authorMatt Joiner <anacrolix@gmail.com>
Tue, 19 Jan 2021 05:55:14 +0000 (16:55 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Mon, 25 Jan 2021 04:54:37 +0000 (15:54 +1100)
storage/sqlite/sqlite-storage-cli/main.go [new file with mode: 0644]
storage/sqlite/sqlite-storage.go

diff --git a/storage/sqlite/sqlite-storage-cli/main.go b/storage/sqlite/sqlite-storage-cli/main.go
new file mode 100644 (file)
index 0000000..5f31574
--- /dev/null
@@ -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")
+       }
+}
index cb2ab1b973533764a6f79a85b8f9d1915ef15e8a..8ec4d5a6d42e6f3e48790bd5f317f5a12ff5e550 100644 (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
                }