MCPcopy Create free account
hub / github.com/ddev/ddev / ImportDB

Method ImportDB

pkg/ddevapp/ddevapp.go:880–1112  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

878
879// ImportDB takes a source sql dump and imports it to an active site's database container.
880func (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)

Callers 9

TestDdevImportDBFunction · 0.95
TestDdevAllDatabasesFunction · 0.95
TestDdevExportDBFunction · 0.95
TestDdevFullSiteSetupFunction · 0.95
importDatabaseBackupMethod · 0.80
TestDdevRestoreSnapshotFunction · 0.80
TestDebugMigrateDatabaseFunction · 0.80
importDBRunFunction · 0.80

Calls 15

DockerEnvMethod · 0.95
ProcessHooksMethod · 0.95
MutagenSyncFlushMethod · 0.95
GetDBClientCommandMethod · 0.95
ExecMethod · 0.95
CreateSettingsFileMethod · 0.95
PostImportDBActionMethod · 0.95
CheckAvailableSpaceFunction · 0.92
ChmodFunction · 0.92
GetQuotedInputFunction · 0.92
ValidateAssetFunction · 0.92
UngzipFunction · 0.92

Tested by 6

TestDdevImportDBFunction · 0.76
TestDdevAllDatabasesFunction · 0.76
TestDdevExportDBFunction · 0.76
TestDdevFullSiteSetupFunction · 0.76
TestDdevRestoreSnapshotFunction · 0.64
TestDebugMigrateDatabaseFunction · 0.64