Skip to content

Commit 4f187cf

Browse files
committed
Initial commit, mostly working, requires some testing
0 parents  commit 4f187cf

File tree

9 files changed

+212
-0
lines changed

9 files changed

+212
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea/*
2+
test/data/*

commands/list_tables.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"github.com/persata/mysqldumptablepurger/io"
6+
"github.com/persata/mysqldumptablepurger/parser"
7+
)
8+
9+
func ListTables(path string) {
10+
s, f := io.GetScanner(path)
11+
defer f.Close()
12+
13+
fmt.Println(fmt.Sprintf("Scanning for tables in %s...\n", path))
14+
15+
for s.Scan() {
16+
match := parser.TableStructureRegex.FindStringSubmatch(s.Text())
17+
18+
if len(match) > 0 {
19+
fmt.Println(match[1])
20+
}
21+
}
22+
23+
if s.Err() != nil {
24+
fmt.Println(fmt.Sprintf("An error occurred scanning the file: %s", s.Err()))
25+
}
26+
27+
fmt.Println("\nScan complete!")
28+
}

commands/remove_tables.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package commands
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"bufio"
7+
"strings"
8+
"github.com/persata/mysqldumptablepurger/io"
9+
"github.com/persata/mysqldumptablepurger/parser"
10+
"regexp"
11+
)
12+
13+
func RemoveTables(inputPath string, outputPath string, tables []string) {
14+
fmt.Println("The following tables will be removed: \n")
15+
16+
for _, table := range tables {
17+
fmt.Println(fmt.Sprintf("- %s", table))
18+
}
19+
20+
if _, err := os.Stat(outputPath); err == nil {
21+
fmt.Println(fmt.Sprintf("\nThe specified output file '%s' already exists, and will be overwritten!", outputPath))
22+
}
23+
24+
fmt.Println("\nContinue? Y/n")
25+
26+
reader := bufio.NewReader(os.Stdin)
27+
c, _ := reader.ReadString('\n')
28+
29+
c = strings.TrimSpace(c)
30+
31+
if c != "Y" {
32+
fmt.Println("Exiting, no action performed")
33+
} else {
34+
fmt.Println("Processing...\n")
35+
}
36+
37+
s, fr := io.GetScanner(inputPath)
38+
defer fr.Close()
39+
40+
w, fw := io.GetWriter(outputPath)
41+
defer fw.Close()
42+
43+
//skipping := false
44+
45+
replaceRegex := regexp.MustCompile(fmt.Sprintf(parser.TableStructureRegexReplace, strings.Join(tables, "|")))
46+
47+
skipping := false
48+
49+
matchCount := 0
50+
51+
for s.Scan() {
52+
currentLine := s.Text()
53+
54+
match := replaceRegex.FindStringSubmatch(currentLine)
55+
56+
if len(match) > 0 {
57+
matchCount += 1
58+
skipping = true
59+
fmt.Println(fmt.Sprintf("Found section for `%s`, skipping data", match[1]))
60+
61+
w.WriteString(fmt.Sprintf("-- Section for `%s` removed by mysqldumptablepurger", match[1]))
62+
w.WriteString("\n")
63+
}
64+
65+
if !skipping {
66+
w.WriteString(currentLine)
67+
w.WriteString("\n")
68+
}
69+
70+
if skipping {
71+
if strings.HasPrefix(currentLine, parser.EndSkippingPrefix) {
72+
skipping = false
73+
}
74+
}
75+
}
76+
77+
w.Flush()
78+
79+
fmt.Println(fmt.Sprintf("\nExiting, %d tables removed", matchCount))
80+
}

io/get_scanner.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
func GetScanner(path string) (*bufio.Scanner, *os.File) {
9+
f := OpenReadFile(path)
10+
11+
s := bufio.NewScanner(f)
12+
13+
s.Buffer(make([]byte, 0), 1024*1024)
14+
15+
return s, f
16+
}

io/get_writer.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
func GetWriter(path string) (*bufio.Writer, *os.File) {
9+
f := OpenWriteFile(path)
10+
11+
w := bufio.NewWriter(f)
12+
13+
return w, f
14+
}

io/open_read_file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io
2+
3+
import "os"
4+
5+
func OpenReadFile(path string) (*os.File) {
6+
f, err := os.Open(path)
7+
8+
if err != nil {
9+
panic(err)
10+
}
11+
12+
return f
13+
}

io/open_write_file.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io
2+
3+
import "os"
4+
5+
func OpenWriteFile(path string) (*os.File) {
6+
f, err := os.Create(path)
7+
8+
if err != nil {
9+
panic(err)
10+
}
11+
12+
return f
13+
}

mysqltabledumppurger.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"os"
5+
"gopkg.in/alecthomas/kingpin.v2"
6+
"github.com/persata/mysqldumptablepurger/commands"
7+
)
8+
9+
var (
10+
// App Declaration
11+
app = kingpin.New("mysqldumptablepurger", "A command line app to remove table data from MySQL dumps.")
12+
13+
// List Command & Args
14+
list = app.Command("list", "List all tables in the given MySQL dump.")
15+
listDumpFile = list.Arg("file", "MySQL dump file").Required().String()
16+
17+
// Remove Command & Args
18+
remove = app.Command("remove", "Remove the list of provided tables from the given MySQL dump")
19+
removeDumpFile = remove.Arg("inputFile", "MySQL dump file").Required().String()
20+
removeNewFile = remove.Arg("outputFile", "MySQL dump file").Required().String()
21+
removeTables = remove.Arg("tables", "Space delimited list of tables to remove").Required().Strings()
22+
)
23+
24+
func main() {
25+
26+
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
27+
28+
// List all tables in the given MySQL dump
29+
case list.FullCommand():
30+
commands.ListTables(*listDumpFile)
31+
32+
// List all tables in the given MySQL dump
33+
case remove.FullCommand():
34+
commands.RemoveTables(*removeDumpFile, *removeNewFile, *removeTables)
35+
36+
}
37+
}

parser/parser.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package parser
2+
3+
import "regexp"
4+
5+
var TableStructureRegex = regexp.MustCompile("-- Table structure for table `(.*)`")
6+
7+
var TableStructureRegexReplace = "^-- Dumping data for table `(%s)`"
8+
9+
var EndSkippingPrefix = "-- Table structure for table"

0 commit comments

Comments
 (0)