Repository navigation
Adds import-db and import-files commands - #123
Conversation
beeradb
left a comment
There was a problem hiding this comment.
I did not test functionality or even compile, but did a code review pass.
| } | ||
|
|
||
| func init() { | ||
| ImportFileCmd.Flags().StringVarP(&fileSource, "src", "", "", "Provide the path to a directory or .tar.gz archive of files to import") |
There was a problem hiding this comment.
I wonder about renaming this from --files-src, as it was in the mockups. The mockup names were chosen so that if/when we add an eventual "import" which does both, we could have consistent flag names across all the commands. It does create some stuttering, though.
Ultimately it's @rickmanelius, just pointing it out for consideration.
There was a problem hiding this comment.
I opted to make it --src for both commands so that usage was consistent between the two commands, and for the flag to be shorter to type, in addition to resolving the stuttering you mentioned. Amusingly, I also kept trying to type --db-url which is apparently still muscle memory for me from drush site-install.
IMO, it would not be an issue if the flags were different for a combo cmd vs the single cmd, but that is indeed Rick's call
| homedir "github.com/mitchellh/go-homedir" | ||
| ) | ||
|
|
||
| // ValidateAsset determines if a given asset matches the required criteria for a given asset type. If the path provided is a tarball, it will extract, validate, and return the extracted asset path. |
There was a problem hiding this comment.
I'm not much of a stickler for 80 characters but this may be long enough that splitting it into multiple lines would enhance readability
|
|
||
| // ValidateAsset determines if a given asset matches the required criteria for a given asset type. If the path provided is a tarball, it will extract, validate, and return the extracted asset path. | ||
| func ValidateAsset(assetPath string, assetType string) (string, error) { | ||
| var invalidAssetError = "%s. Please provide a valid asset path." |
There was a problem hiding this comment.
%v is typically preferred over %s for printing errors from what I have seen. From https://golang.org/pkg/fmt/
%v the value in a default format
when printing structs, the plus flag (%+v) adds field names
| } | ||
|
|
||
| if len(files) > 1 { | ||
| fmt.Println("WARNING: Multiple .sql files found. Only the first .sql file will be used.") |
There was a problem hiding this comment.
I would probably prefer to just import all SQL files found. Maybe that's an edge case, though.
At the very least, printing the filename of the SQL file that is actually being imported would likely reduce confusion.
There was a problem hiding this comment.
I think it's unlikely that we could handle multi-part imports reliably, as the order in which they are imported tends to be important in that case (e.g. table structures before data goes in). I think it's best to warn and leave it to the user.
Totally valid to provide the sql file that will be used though.
|
|
||
| // if we extracted an archive, clean up the extraction point | ||
| if strings.Contains(source, os.TempDir()) { | ||
| os.RemoveAll(path.Dir(source)) |
| log.Fatal(err) | ||
| } | ||
| out, err := system.RunCommand( | ||
| "tar", |
There was a problem hiding this comment.
We already shell out all over the place, but I think we also recognize it as a portability problem we probably need to get away from.
It may be wise to look into the archive/tar and compress/gzip packages to see if we can easily handle this without shelling out. A quick google returns this https://gist.github.com/indraniel/1a91458984179ab4cf80 gist which looks to extract tar.gz files.
This may also help us get around the awkward extract-copy-delete scenario above, as we could only extract the files needed and do so directly to the correct directory.
| } | ||
|
|
||
| // findFileExt walks a given directory searching for a given extension and returns a list of matching results. | ||
| func findFileExt(dirpath string, ext string) ([]string, error) { |
There was a problem hiding this comment.
Could we be a bit more explicit with the name here?
Suggestions:
- findFilesBy[Ext|Extension]
- findFilesInDirectory
| // Define an application name. | ||
| fmt.Print(namePrompt + ": ") | ||
| c.Name = getInput(c.Name) | ||
| c.Name = GetInput(c.Name) |
There was a problem hiding this comment.
Fine w/ reusing this but we should likely move it into a utils or inputreader package if we do.
| }, | ||
| Run: func(cmd *cobra.Command, args []string) { | ||
| app := platform.PluginMap[strings.ToLower(plugin)] | ||
| app.Init() |
There was a problem hiding this comment.
This is the fault of my bad example (which I've fixed in my latest PR #116), but the return of Init, err, needs to be checked here. It will return an error if a config can't be read, which is almost certainly a problem in this case.
| @@ -295,6 +382,7 @@ func (l *LocalApp) FindPorts() error { | |||
| // as well as other sensitive data like salts. | |||
| func (l *LocalApp) Config() error { | |||
There was a problem hiding this comment.
This probably needs to be modified to only try writing if a config file doesn't exist (or maybe it does? hard to tell from the diff)
There was a problem hiding this comment.
Not a bad idea. I also had the idea of providing a --skip-config or similar flag for the db command, which would allow the user to skip it entirely and provide their own.
rfay
left a comment
There was a problem hiding this comment.
I did the opposite of @beeradb and still have plenty to do, but first round:
- Import-db worked fine, although I suspect (drupal-specific) it's dependent on the settings.php config, and that may affect actual customer success.
- I was expecting import-db to handle a gzipped, zipped, or plain-text .sql file. If we have left that expectation behind, it's probably OK, but it would likely lead to a happier customer experience. I note that the error message I was given was enough to get me on the right path.
I'll continue onto files import and then code review.
rfay
left a comment
There was a problem hiding this comment.
I experimented with both the file import and db import, and had satisfactory experiences with both.
I totally understand that this PR takes on a lot of ground and not every nit is going to be able to resolved here, but like @beeradb I had a few reactions to things that may or may not be valid.
I'll return to actually reviewing the code later; this really just covered my experiences with the actual usage and code it happened to lead me to.
Thanks for your patience @tannerjfco !
| var ImportDBCmd = &cobra.Command{ | ||
| Use: "import-db", | ||
| Short: "Import the database of an existing site to the local dev environment.", | ||
| Long: "Import the database of an existing site to the local development environment. The database can be provided as a SQL dump in a .sql or .tar.gz format. For the .tar.gz format, a SQL dump in .sql format must be present at the root of the archive.", |
There was a problem hiding this comment.
"The database dump provided will be imported into a database named 'data' on the devsite host named 'db'. Your site database settings need to point to this database." or something like that will be helpful. Remember that people may or may not understand the architecture ddev is imposing wrt WebImage, DBImage, etc.
There was a problem hiding this comment.
@tannerjfco I was suggesting changed verbiage here, maybe that wasn't clear.
There was a problem hiding this comment.
Please validate extra commands and or flags here and in files import, this was confusing to me.
$ ddev import-db src=/tmp/v0.2.1.tar.gz
Provide the path to the database you wish to import.
Import path:
| } | ||
|
|
||
| // if we have a tarball, extract and set path to the extraction point | ||
| if strings.HasSuffix(assetPath, ".tar.gz") { |
There was a problem hiding this comment.
Let's please check file type rather than file extension. .tgz should work fine - it's what is inside that counts. I guess I'd be ok with checking both .tar.gz and .tgz, but would much prefer actually checking to see if it's gzipped and then see if it has a tarball in it. I guess what I'd really like is 1) gunzip or unzip based on file type, 2) check to see if the resulting file is tarball or zipball, 3) Unzip appropriately. I understand that might be too much, but it's what I was expecting and had written in the issue.
| } | ||
|
|
||
| // findFileByExtension walks a given directory searching for a given extension and returns a list of matching results. | ||
| func findFileByExtension(dirpath string, ext string) ([]string, error) { |
There was a problem hiding this comment.
As mentioned elsewhere, extension is a very Windows way of doing things, and I would much prefer a more file-type way of doing it. So I'm uncomfortable with using extensions as a base idea at all.
| // CopyDir recursively copies a directory tree, attempting to preserve permissions. | ||
| // Source directory must exist, destination directory must *not* exist. | ||
| // Symlinks are ignored and skipped. Credit @m4ng0squ4sh https://gist.github.com/m4ng0squ4sh/92462b38df26839a3ca324697c8cba04 | ||
| func CopyDir(src string, dst string) error { |
There was a problem hiding this comment.
Do we seriously have to recreate CopyFile and CopyDir functions in golang? Yuckie. I guess I didn't know that. Would be be better to just cp -rp or rsync in that case?
| return err | ||
| } | ||
| } else { | ||
| // Skip symlinks. |
There was a problem hiding this comment.
Wouldn't it be an error to have a symlink? Is it OK to just continue?
| // destination file exists, all it's contents will be replaced by the contents | ||
| // of the source file. The file mode will be copied from the source and | ||
| // the copied data is synced/flushed to stable storage. Credit @m4ng0squ4sh https://gist.github.com/m4ng0squ4sh/92462b38df26839a3ca324697c8cba04 | ||
| func CopyFile(src string, dst string) error { |
There was a problem hiding this comment.
If we're going to introduce utility functions, we at least need to get them into a utility package, rather than have them here. I agree that they could live here until required elsewhere and be moved then. But next dev might not ever know that this was done.
|
|
||
| // CopyFile copies the contents of the file named src to the file named | ||
| // by dst. The file will be created if it does not already exist. If the | ||
| // destination file exists, all it's contents will be replaced by the contents |
beeradb
left a comment
There was a problem hiding this comment.
I attempted to do a DB import both from tar.gz and from a DB file and neither appeared to work as advertised
First, from .sql
ddev/drupal8 ‹gke:drud-test› git:(master) » ddev import-db --src=db.sql
no file to import found at /db/data.sql
A settings file already exists for your application, so ddev did not generate one.
Run 'ddev describe' to find the database credentials for this application.
Successfully imported database for drupal8
Now, from .tar.gz
ddev/drupal8 ‹gke:drud-test› git:(master) » ddev import-db
Provide the path to the database you wish to import.
Import path:
db.tar.gz
no file to import found at /db/data.sql
Generating settings.php file for database connection.
The files import appears to work as advertised, though.
|
You will need to set the database container tag in the |
beeradb
left a comment
There was a problem hiding this comment.
Approved with one (optional) comment:
ddev import-db sudhfuisdhfiudsfhsduifhsdiufhprompts me for the import path. It may be better to fail if there are arguments detected.
rfay
left a comment
There was a problem hiding this comment.
Coming back for review now, just trying some things out.
ddev import-db --src=../fredthrall.com/fredthrall.sql.gz
Unable to successfully import database for drud-d7: failed to extract provided archive: archive/tar: invalid tar header
Wouldn't a sql.gz file be an obvious thing to import? I see that the help doesn't offer that, so I'll move on and try it with .sql
|
|
||
| err = app.ImportDB(dbSource) | ||
| if err != nil { | ||
| Failed("Unable to successfully import database for %s: %s", app.GetName(), err) |
There was a problem hiding this comment.
Could we change "Unable to successfully import" to "Failed to import database" please?
|
Question for discussion: I would expect import-db to drop all tables before importing, I don't think it does right now. Is that what we want? |
rfay
left a comment
There was a problem hiding this comment.
OK now for the wall-o-text. I'm exhausted, so I know you're way beyond exhausted with all this.
Thanks for the great work and the thoughtful tests. This is a pile of work, and I think it's nearly good to go.
It worked for me in nearly everything I tried; the exception was *.sql.gz db imports.
I had a few comments that really deserve followup (checking returns on filesystem operations, deciding if *.sql.gz is supported, and if is, testing it). Then a lot of coaching-type or questioning type comments.
I'm leaving it at "requesting changes" at this point because some of those do need to be addressed.
| @@ -0,0 +1,4 @@ | |||
| // Place your settings in this file to overwrite default and user settings. | |||
| { | |||
| "go.testTimeout": "360s" | |||
There was a problem hiding this comment.
Probably should be added in another PR but I won't tell if you don't. Also... I'm assuming you want this here and that everybody should have it; otherwise it should go in .gitignore.
There was a problem hiding this comment.
The setting is specific to the project, as the default 30s timeout isn't going to be all that useful for some of these tests.
| PATH=$$PWD/bin/$(TESTOS):$$PATH CGO_ENABLED=0 DDEV_BINARY_FULLPATH=$(DDEV_BINARY_FULLPATH) DRUD_DEBUG=true go test -timeout 20m -v -installsuffix 'static' -ldflags "$(LDFLAGS)" ./pkg/... | ||
| PATH=$$PWD/bin/$(TESTOS):$$PATH CGO_ENABLED=0 DDEV_BINARY_FULLPATH=$(DDEV_BINARY_FULLPATH) go test -p 1 -timeout 20m -v -installsuffix static -ldflags "$(LDFLAGS)" ./cmd/... | ||
| PATH=$$PWD/bin/$(TESTOS):$$PATH CGO_ENABLED=0 DDEV_BINARY_FULLPATH=$(DDEV_BINARY_FULLPATH) DRUD_DEBUG=true go test -timeout 20m -v -installsuffix static -ldflags "$(LDFLAGS)" ./pkg/... | ||
|
|
There was a problem hiding this comment.
This change will conflict with #140, which moves these two into separate targets; There is no damage in removing the single quotes from 'static', but also I don't believe there can be any benefit (or change).
|
|
||
| ``` | ||
| ➜ ddev import-db | ||
| Provide the path to the database you wish to import. |
There was a problem hiding this comment.
Isn't this doc out of date? Shouldn't it have --src= ?
There was a problem hiding this comment.
The cmd will provide a prompt if no flags are provided.
| Successfully imported database for drupal8 | ||
| ``` | ||
|
|
||
| The `import-db` command allows you to specify the location of a SQL dump to be imported as the active database for your site. The database may be provided as a `.sql` file, `.sql.gz` or `.tar.gz` archive. The provided dump will be imported into the database named `data` in the database container for your site. A database connection file will be generated for your site if one does not exist (`settings.php` for Drupal, `wp-config.php` for WordPress). If you have already created a connection file, you will need to ensure your connection credentials match the ones provided in `ddev describe`. |
There was a problem hiding this comment.
I don't think sql.gz works right now.
$ ddev import-db --src=../fredthrall.com/fredthrall.sql.gz Unable to successfully import database for drud-d7: failed to extract provided archive: archive/tar: invalid tar header Unable to successfully import database for drud-d7: failed to extract provided archive: archive/tar: invalid tar header
| Successfully imported files for drupal8 | ||
| ``` | ||
|
|
||
| The `import-files` command allows you to specify the location of uploaded file assets to import for your site. For Drupal, this is the public files directory, located at `sites/default/files` by default. For WordPress, this is the uploads directory, located at `wp-content/uploads` by default. The files may be provided as a directory or archive containing the contents of the uploads folder. The contents of the directory or archive provided will be copied to the default location of the upload directory for your site. |
There was a problem hiding this comment.
$ ddev import-files --src=/tmp/fredthrall_files.tgz
Unable to successfully import files for drud-d7: remove /Users/rfay/workspace/drud-d7/docroot/sites/default/files: permission denied
This is the classic problem of drupal setting the sites/default to 0555. - We may want to explicitly chmod u+w sites/default ?
There was a problem hiding this comment.
Document needs to say what the contents of the "files" tarball looks like. AFAICT it is supposed to be the files and directories within the files directory, not including files/ - This may want to be in the help for the command as well.
| os.Setenv("DRUD_NONINTERACTIVE", "true") | ||
|
|
||
| err = system.DownloadFile(site.archivePath(), site.DownloadURL) | ||
| err = system.DownloadFile(site.archivePath(), site.SourceURL) |
| // DownloadURL is the URL of the tarball to be used for building the site. | ||
| DownloadURL string | ||
| Dir string | ||
| // SourceURL is the URL of the source code tarball to be used for building the site. |
There was a problem hiding this comment.
I was confused at first about the name change DownloadURL -> SourceURL, but the comment helps - "source code tarball"
| return err | ||
| } | ||
|
|
||
| tf := tar.NewReader(gf) |
|
|
||
| err = Untargz(testArchivePath, exDir) | ||
| assert.NoError(err) | ||
| assert.True(system.FileExists(path.Join(exDir, "2017", "04", "pexels-photo-265186-100x100.jpeg"))) |
There was a problem hiding this comment.
Pretty stinking fragile :( Might there be a way to infer general structure, rather than the exact contents? Basically this tightly-couples the test here and the tarball there, making it so either one can break the test, and neither one is actually relevant to the underlying reason for the test (with is to make sure we can untar a tarball).
|
|
||
| file, err := os.Stat(dest) | ||
| assert.NoError(err) | ||
| assert.Equal(int(file.Mode()), 0644) |
There was a problem hiding this comment.
How about an assertion to see if they're actually equal, maybe exec a cksum command, or another golang technique?
|
Testing #123 Prep for ddev
Prepping Site Configuration
Importing Assets
I'm stopping for now because I need to change gears for our meetings, but any help on fixing the file archive will help get this to an approved state. |
|
Follow up to my last message. I was actually able to solve this, but I'm guessing there are two parts needed to address. The issue was that the SITENAME.tar.zip downloaded from Pantheon unzips with a files-live parent folder wherease the files.tar.gz that Tanner referenced here https://github.com/drud/drupal-kickstart/releases/tag/v0.1.0 unzips without a folder that encloses the files normally expected in sites/default/files. So my work-around was to do the following. I manually ran I'm not sure how I would necessarily solve. We could run a quick check to see if unzipping the file tar.gz results in a single folder and then assume that we should move the files from within that singular directory instead of copying the enclosing folder over. The second issue, which I'm not sure how to address, is why the zip from Pantheon refused to work even with the parent folder. For example, I originally unzipped and rezipped with the files-live parent folder as the enclosing folder within the zip. I was still able to successfully run I would recommend spinning up a free demo site on Patheon, pulling a zip, and using that to address the enclosing folder issue. Hopefully that fixes the other issue as to why it wasn't unzipping in the first place, but I wanted to surface this just in case. |
rickmanelius
left a comment
There was a problem hiding this comment.
While the scope of this ticket didn't include compatibility with zips from Pantheon, the enclosing folder issue will be a gotcha for those that aren't careful on how they zip their files directory. Additionally, since we are targeting Pantheon integration, I would be curious as to why the zip wasn't working as it is (it could also be user error on my part).
|
With the latest updates, sql.gz import should work as expected. Additionally, tar files will work whether or not they are gzipped. Once we have green board on the tests, this should be ready to come in. |
|
I tested .sql, .sql.gz for database and .tar, .tar.gz, .tgz for files. They all seemed to work fine. Approved. There will be followups, we know. Huge accomplishment, thanks for hanging in there. |
I read through Rick's review and believe all his issues are solved at this point (or punted to future issues). And we should get this merged. Hope that's OK Rick.
The Problem:
See #33 for full details. Our existing import functionality is heavily reliant on site archives in a very specific structure and naming convention. We want import functionality that does not require rigid archive formats, and we want to be able to separately import the database and files as desired.
The Fix:
This PR introduces 2 new commands,
ddev import-dbandddev import-files. The commands have some similar functionality. They both accept a path provided by user prompt or by using a--srcflag. They both accepttar.gzarchives, and if provided, will be extracted so the assets within can be imported.The import-db command ultimately expects a single
.sqlfile be provided. This means either a file path should be provided directly, or an archive containing a sql dump at the root of the archive should be provided.The import-files command ultimately expects a directory path be provided. The provided directory's contents will be copied to the default uploaded files directory for the given application. In Drupal's case, this is
sites/default/files. In WordPress' case, this iswp-content/uploads. If an archive is provided, the contents of the archive are copied as-is to the default location.This also implements a new wp-config.php template that will work with a standard installation. Full details of this can be seen at the closed PR for this task #112
This PR does not remove the previous import command and its related functionality. For the purpose of easier reviews, the removal will be completed in a separate PR.
The Test:
Note: You will need to set the database container tag in the
.ddev/config.yamlof the site you are testing to theimport-updatetag.You should be able to test this with a pre-existing site, provided the site uses standard location for uploaded files. You can also use the following test sites that I have compiled test assets for:
https://github.com/drud/drupal8/releases/tag/v0.2.2
https://github.com/drud/wordpress/releases/tag/v0.1.0
https://github.com/drud/drupal-kickstart/releases/tag/v0.1.0
Usage examples:
examples are for import-files, but also valid for import-db, provided you are providing a sql dump.
Prompt:
Flag:
or
Automation Overview:
I have added test coverage for ImportDB and ImportFiles at the local plugin level. I have also added test coverage for the new appimport and files packages.
Related Issue Link(s):
#33 OP
#107 / #112 update wp-config template
drud/mysql-docker-local#11 - mysql import updates
Release/Deployment notes:
Does this affect anything else, or are there ramifications for other code? Does anything have to be done on deployment?
This PR does not remove the previous import command and its related functionality. For the purpose of easier reviews, the removal will be completed in a separate PR.
drud/mysql-docker-local#11 - mysql import updates is required for this change. A new tag will need to be pushed for this container.