-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump.go
185 lines (156 loc) · 3.9 KB
/
dump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package cmd
import (
"database/sql"
"encoding/json"
"errors"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
_ "github.com/mattn/go-sqlite3" // SQLite driver usage
"github.com/spf13/cobra"
)
// https://stackoverflow.com/a/10485970/2610955
func contains(tables []string, target string) bool {
for _, table := range tables {
if table == target {
return true
}
}
return false
}
var dumpCmd = &cobra.Command{
Use: "dump [tables]",
Short: "Dump sqlite database metadata and table",
Long: `Dump sqlite database metadata and table`,
RunE: func(cmd *cobra.Command, args []string) error {
path, _ := cmd.Flags().GetString("path")
filePath, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
err := errors.New("Path doesn't exist")
return err
}
return err
}
if filePath.IsDir() {
err := errors.New("Path should be a file")
return err
}
output, _ := cmd.Flags().GetString("output")
_, err = os.Stat(output)
if err != nil {
if os.IsNotExist(err) {
err := os.MkdirAll(output, os.ModePerm)
if err != nil {
return err
}
}
}
outputPath, err := os.Stat(output)
if err != nil {
if os.IsNotExist(err) {
return err
}
}
if !outputPath.IsDir() {
err := errors.New("Output should be a directory")
return err
}
db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
defer db.Close()
allTables := []string{}
queryTables := []string{}
all, _ := cmd.Flags().GetBool("all")
if !all && len(args) == 0 {
msg := "You must pass --all or specify some tables"
return errors.New(msg)
}
allTablesQuery := "SELECT name FROM sqlite_master WHERE type='table'"
rows, _ := db.Query(allTablesQuery)
for rows.Next() {
var table string
rows.Scan(&table)
allTables = append(allTables, table)
}
if all {
queryTables = allTables
} else {
for _, arg := range args {
if contains(allTables, arg) {
queryTables = append(queryTables, arg)
}
}
}
for _, table := range queryTables {
rows, err := db.Query("select * from " + table)
if err != nil {
return err
}
if rows == nil {
err := errors.New("No rows found for table " + table)
return err
}
// https://stackoverflow.com/questions/17845619/how-to-call-the-scan-variadic-function-using-reflection/17885636#17885636
columns, _ := rows.Columns()
count := len(columns)
values := make([]interface{}, count)
valuePtrs := make([]interface{}, count)
lines := []string{}
schemaResult := db.QueryRow("SELECT sql FROM sqlite_schema WHERE name = ?", table)
var schema string
schemaResult.Scan(&schema)
metadata := map[string]interface{}{
"name": table,
"columns": columns,
"schema": schema,
}
metadataJSON, _ := json.MarshalIndent(metadata, "", " ")
for rows.Next() {
for i := range columns {
valuePtrs[i] = &values[i]
}
rows.Scan(valuePtrs...)
var entries = []interface{}{}
for i := range columns {
val := values[i]
b, ok := val.([]byte)
var v interface{}
if ok {
v = string(b)
} else {
v = val
}
entries = append(entries, v)
}
data, _ := json.Marshal(entries)
jsonString := string(data)
lines = append(lines, jsonString)
}
metadataPath := filepath.Join(output, table+".metadata.json")
tablePath := filepath.Join(output, table+".ndjson")
err = ioutil.WriteFile(metadataPath,
[]byte(metadataJSON), 0644)
if err != nil {
return err
}
err = ioutil.WriteFile(tablePath,
[]byte(strings.Join(lines, "\n")), 0644)
if err != nil {
return err
}
}
return nil
},
}
func init() {
dumpCmd.Flags().StringP("path", "p", "", "Path to sqlite database")
dumpCmd.Flags().StringP("output", "o", "", "Output directory")
dumpCmd.Flags().BoolP("all", "", false, "Dump all tables")
dumpCmd.MarkFlagRequired("path")
dumpCmd.MarkFlagRequired("output")
}