diff options
author | Joop Kiefte <ikojba@gmail.com> | 2023-09-28 03:44:13 +0100 |
---|---|---|
committer | Joop Kiefte <ikojba@gmail.com> | 2023-09-28 03:44:13 +0100 |
commit | 911bc140934718dbcc1d18225ce9fd8085061a60 (patch) | |
tree | a20e73d3671638eb701a4932174494125ac4603f | |
parent | 4597822bb73287be12edca8f5fe2a6d9b6d2d585 (diff) |
Add option to create new tables
-rw-r--r-- | main.go | 80 |
1 files changed, 78 insertions, 2 deletions
@@ -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") |