Skip to content

Commit d01e3bc

Browse files
committed
Add migrations to vacuum internal database
1 parent ccbc0fa commit d01e3bc

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

cli/migrations/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ These scripts are <u>**not real migrations**</u>. There is no up/down rules. The
44

55
These scripts were needed to migrate the Database between different states.
66

7+
- [Vacuum database](command-vacuum.go)
8+
```shell
9+
# rebuilds the database to defragment it
10+
go run cli/migrations/*.go vacuum internal.db
11+
```
12+
713
- [Add UAST nullable columns](command-UAST-add-columns.go)
814
```shell
915
# prepares the current "internal.db". Adds "uast_a" and "uast_b" nulable cols

cli/migrations/command-vacuum.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/src-d/code-annotation/server/dbutil"
8+
)
9+
10+
type vacuumCmd struct {
11+
commandDesc
12+
Args struct {
13+
InternalDBPath string `description:"filepath to the internal SQLite database"`
14+
} `positional-args:"yes" required:"yes"`
15+
}
16+
17+
var vacuumOpts = vacuumCmd{
18+
commandDesc: commandDesc{
19+
name: "vacuum",
20+
shortDesc: "Rebuilds the database to defragment it",
21+
longDesc: "Rebuilds the sqlite://InternalDBPath database to eliminate free pages, compact table data...",
22+
},
23+
}
24+
25+
// queries
26+
const (
27+
vacuumQuery = "VACUUM"
28+
)
29+
30+
func (c *vacuumCmd) Execute(args []string) error {
31+
internalDb, err := dbutil.Open(fmt.Sprintf(sqliteDSN, c.Args.InternalDBPath), false)
32+
if err != nil {
33+
log.Fatal(err)
34+
}
35+
36+
defer internalDb.Close()
37+
38+
log.Println("Running VACUUM process ...")
39+
40+
_, err = internalDb.Exec(vacuumQuery)
41+
if err != nil {
42+
log.Fatal(err)
43+
}
44+
45+
log.Println("... VACUUM process finished")
46+
return nil
47+
}

cli/migrations/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func main() {
1818
addCommand(parser, &uastColsOpts)
1919
addCommand(parser, &uastImportOpts)
2020
addCommand(parser, &diffRmColOpts)
21+
addCommand(parser, &vacuumOpts)
2122
parse(parser, description)
2223
}
2324

0 commit comments

Comments
 (0)