Skip to content

Commit

Permalink
Make stdin work with import-db, add test coverage for that to TestDde…
Browse files Browse the repository at this point in the history
…vImportDB
  • Loading branch information
rfay committed Jan 23, 2020
1 parent effc94f commit d46d5b2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
12 changes: 2 additions & 10 deletions pkg/ddevapp/ddevapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,18 +373,10 @@ func (app *DdevApp) ImportDB(imPath string, extPath string, progress bool, noDro
if !noDrop {
preImportSQL = fmt.Sprintf("DROP DATABASE IF EXISTS %s; ", targetDB) + preImportSQL
}
inContainerCommand := fmt.Sprintf(`echo "%s" | mysql -uroot -proot`, preImportSQL)
_, _, err = app.Exec(&ExecOpts{
Service: "db",
Cmd: inContainerCommand,
})
if err != nil {
return err
}

inContainerCommand = fmt.Sprintf(`pv %s/*.*sql | mysql %s`, insideContainerImportPath, targetDB)
inContainerCommand := fmt.Sprintf(`mysql -uroot -proot -e "%s" && pv %s/*.*sql | mysql %s`, preImportSQL, insideContainerImportPath, targetDB)
if imPath == "" && extPath == "" {
inContainerCommand = "pv | mysql " + targetDB
inContainerCommand = fmt.Sprintf(`mysql -uroot -proot -e "%s" && mysql %s`, preImportSQL, targetDB)
}
_, _, err = app.Exec(&ExecOpts{
Service: "db",
Expand Down
19 changes: 19 additions & 0 deletions pkg/ddevapp/ddevapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,25 @@ func TestDdevImportDB(t *testing.T) {
app.Hooks = nil

for _, db := range []string{"db", "extradb"} {

// Import from stdin, make sure that works
inputFile := filepath.Join(testDir, "testdata", t.Name(), "stdintable.sql")
f, err := os.Open(inputFile)
require.NoError(t, err)
// nolint: errcheck
defer f.Close()
savedStdin := os.Stdin
os.Stdin = f
err = app.ImportDB("", "", false, false, db)
os.Stdin = savedStdin
assert.NoError(err)
out, _, err := app.Exec(&ddevapp.ExecOpts{
Service: "db",
Cmd: fmt.Sprintf(`echo "SHOW DATABASES LIKE '%s'; SELECT COUNT(*) FROM stdintable;" | mysql -N %s`, db, db),
})
assert.NoError(err)
assert.Equal(out, fmt.Sprintf("%s\n2\n", db))

// Import 2-user users.sql into users table
path := filepath.Join(testDir, "testdata", t.Name(), "users.sql")
err = app.ImportDB(path, "", false, false, db)
Expand Down
55 changes: 55 additions & 0 deletions pkg/ddevapp/testdata/TestDdevImportDB/stdintable.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- MySQL dump 10.13 Distrib 5.5.54, for debian-linux-gnu (x86_64)
--
-- Host: db Database: data
-- ------------------------------------------------------
-- Server version 5.7.17-log

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;

--
-- Table structure for table `stdintable`
--

DROP TABLE IF EXISTS `stdintable`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `stdintable` (
`uid` int(10) unsigned NOT NULL,
`uuid` varchar(128) CHARACTER SET ascii NOT NULL,
`langcode` varchar(12) CHARACTER SET ascii NOT NULL,
PRIMARY KEY (`uid`),
UNIQUE KEY `user_field__uuid__value` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='The base table for user entities.';
/*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `stdintable`
--

LOCK TABLES `stdintable` WRITE;
/*!40000 ALTER TABLE `stdintable` DISABLE KEYS */;
set autocommit=0;
INSERT INTO `stdintable` VALUES (0,'13751eca-19cf-41c2-90d4-9363f3a07c45','en'),(1,'186efa0a-8aa3-4eeb-90ce-6302fb9c4e07','en');
/*!40000 ALTER TABLE `stdintable` ENABLE KEYS */;
UNLOCK TABLES;
commit;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-05-31 14:03:34

0 comments on commit d46d5b2

Please sign in to comment.