From 911bc140934718dbcc1d18225ce9fd8085061a60 Mon Sep 17 00:00:00 2001 From: Joop Kiefte Date: Thu, 28 Sep 2023 03:44:13 +0100 Subject: Add option to create new tables --- main.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 6ba9791..492a14e 100644 --- a/main.go +++ b/main.go @@ -21,6 +21,8 @@ func main() { noninteractive = true choice = os.Args[1] switch choice { + case "create": // Create table + table = os.Args[2] case "edit": // Import table table = os.Args[2] case "run": // Run query @@ -53,6 +55,11 @@ Running debby with any other arguments will run it as a dolt command.` // Execute choice switch choice { + case "create": // Create table + err = CreateTable(table) + if err != nil { + fmt.Println(err) + } case "run": // Table name will be empty. Run query err = RunSQL(query, "") @@ -81,7 +88,7 @@ Running debby with any other arguments will run it as a dolt command.` fmt.Println() fmt.Println("Tables: ", strings.Join(tables, ", ")) fmt.Println() - fmt.Println("[run] a query, [edit] a table, run any dolt command, or [exit]") + fmt.Println("[run] a query, [create] or [edit] a table, run any dolt command, or [exit]") fmt.Print("> ") // Read user input @@ -89,7 +96,7 @@ Running debby with any other arguments will run it as a dolt command.` choices := strings.Split(choicestr, " ") choice = choices[0] switch choice { - case "run", "edit", "exit", "quit": + case "create", "run", "edit", "exit", "quit": query = strings.Join(choices[1:], " ") if len(choices) > 1 { table = choices[1] @@ -125,6 +132,75 @@ func SaveToTable(table, file string) error { return cmd.Run() } +func CreateTable(table string) error { + // Create a temporary CSV file + file, err := os.CreateTemp("", "debby-*.csv") + if err != nil { + return err + } + defer os.Remove(file.Name()) + + // Create a temporary SQL file for the schema of the table + schemafile, err := os.CreateTemp("", "debby-*.sql") + if err != nil { + return err + } + defer os.Remove(schemafile.Name()) + + // Create dummy contents in the CSV file + _, err = file.WriteString("a\n1\n") + if err != nil { + return err + } + + // Open the CSV file in visidata + err = OpenVisidata(file.Name()) + if err != nil { + return err + } + // Run dolt schema import -c --pks id table CSVfile --dry-run > schemafile + cmd := exec.Command("dolt", "schema", "import", "-c", "--pks", "id", table, file.Name(), "--dry-run") + cmd.Stdout = schemafile + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err = cmd.Run() + if err != nil { + return err + } + + // Open the SQL file in the default editor, fall back to vim if not set + editor := os.Getenv("EDITOR") + if editor == "" { + editor = "vim" + } + cmd = exec.Command(editor, schemafile.Name()) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err = cmd.Run() + if err != nil { + return err + } + + // Run the SQL file to create the table + cmd = exec.Command("dolt", "sql", "-f", schemafile.Name()) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + err = cmd.Run() + if err != nil { + return err + } + + // Do the normal replacing import + err = SaveToTable(table, file.Name()) + if err != nil { + return err + } + + return nil +} + func ReadTableNames() []string { // Run dolt ls, ignore the first line and strip spaces for each following line cmd := exec.Command("dolt", "ls") -- cgit v1.2.3-70-g09d2