ImportDB takes a source sql dump and imports it to an active site's database container.
(dumpFile string, extractPath string, progress bool, noDrop bool, targetDB string)
| 878 | |
| 879 | // ImportDB takes a source sql dump and imports it to an active site's database container. |
| 880 | func (app *DdevApp) ImportDB(dumpFile string, extractPath string, progress bool, noDrop bool, targetDB string) error { |
| 881 | _ = app.DockerEnv() |
| 882 | dockerutil.CheckAvailableSpace() |
| 883 | |
| 884 | if targetDB == "" { |
| 885 | targetDB = "db" |
| 886 | } |
| 887 | var extPathPrompt bool |
| 888 | dbPath, err := os.MkdirTemp(filepath.Dir(app.ConfigPath), ".importdb") |
| 889 | if err != nil { |
| 890 | return err |
| 891 | } |
| 892 | err = util.Chmod(dbPath, 0777) |
| 893 | if err != nil { |
| 894 | return err |
| 895 | } |
| 896 | |
| 897 | defer func() { |
| 898 | _ = os.RemoveAll(dbPath) |
| 899 | }() |
| 900 | |
| 901 | err = app.ProcessHooks("pre-import-db") |
| 902 | if err != nil { |
| 903 | return err |
| 904 | } |
| 905 | |
| 906 | // If they don't provide an import path and we're not on a tty (piped in stuff) |
| 907 | // then prompt for path to db |
| 908 | if dumpFile == "" && isatty.IsTerminal(os.Stdin.Fd()) { |
| 909 | // ensure we prompt for extraction path if an archive is provided, while still allowing |
| 910 | // non-interactive use of --file flag without providing a --extract-path flag. |
| 911 | if extractPath == "" { |
| 912 | extPathPrompt = true |
| 913 | } |
| 914 | output.UserOut.Println("Provide the path to the database you want to import.") |
| 915 | fmt.Print("Path to file: ") |
| 916 | |
| 917 | dumpFile = util.GetQuotedInput("") |
| 918 | } |
| 919 | |
| 920 | if dumpFile != "" { |
| 921 | importPath, isArchive, err := appimport.ValidateAsset(dumpFile, "db") |
| 922 | if err != nil { |
| 923 | if isArchive && extPathPrompt { |
| 924 | output.UserOut.Println("You provided an archive. Do you want to extract from a specific path in your archive? You may leave this blank if you wish to use the full archive contents") |
| 925 | fmt.Print("Archive extraction path: ") |
| 926 | |
| 927 | extractPath = util.GetQuotedInput("") |
| 928 | } else { |
| 929 | return fmt.Errorf("unable to validate import asset %s: %s", dumpFile, err) |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | switch { |
| 934 | case strings.HasSuffix(importPath, "sql.gz") || strings.HasSuffix(importPath, "mysql.gz"): |
| 935 | err = archive.Ungzip(importPath, dbPath) |
| 936 | if err != nil { |
| 937 | return fmt.Errorf("failed to extract provided file: %v", err) |