Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use --file for import-db and rework import-db and export-db, fixes #4593 #5013

Merged
merged 3 commits into from
Jun 26, 2023

Conversation

gilbertsoft
Copy link
Member

@gilbertsoft gilbertsoft commented Jun 23, 2023

The Issue

How This PR Solves The Issue

This PR streamlines the flags and descriptions of the import-db and export-db commands. It introduces a new package which makes it possible to write cleaner command definitions regarding indentation. Commands are created with a factory function.

Old flags are still here and marked as deprecated to avoid BC issues.

Thanks to @miromichalicka for the initial work done in #4647.

Manual Testing Instructions

The impact is minimal and everything should be covered by automated testing.

  • import-db command works including deprecated flags
  • export-db command works including deprecated flags

Automated Testing Overview

Related Issue Link(s)

Closes:

Replaces:

Release/Deployment Notes

@github-actions
Copy link

github-actions bot commented Jun 23, 2023

@gilbertsoft gilbertsoft changed the title Streamline flags of import- and export-db commands, fixes 4593 Streamline flags of import- and export-db commands, fixes #4593 Jun 23, 2023
@gilbertsoft gilbertsoft marked this pull request as ready for review June 23, 2023 18:00
@gilbertsoft gilbertsoft requested review from a team as code owners June 23, 2023 18:00
Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't completely grok this yet, but please update the description here to

  • explain the new internal/text package (and why you call it "text", which is not very descriptive)
  • explain what the heredoc package is and why it's used

Although this is apparently simple, it is definitely not easy to review.

Since this will apparently be a pattern for future updates, could you please remove all the var usages with pointers, and instead use flags, as is done in newer commands? start.go uses this technique, for example

skip, err := cmd.Flags().GetBool("skip-confirmation")
if err != nil {
util.Failed(err.Error())
}
// Look for version change and opt-in to instrumentation if it has changed.
err = checkDdevVersionAndOptInInstrumentation(skip)
if err != nil {
util.Failed(err.Error())
}
selectFlag, err := cmd.Flags().GetBool("select")

@gilbertsoft gilbertsoft force-pushed the task/streamline-file-flags branch 2 times, most recently from ec12f71 to e6bce72 Compare June 23, 2023 18:56
@gilbertsoft
Copy link
Member Author

I don't completely grok this yet, but please update the description here to

* [ ]  explain the new internal/text package (and why you call it "text", which is not very descriptive)

Highly inspired by GH cli. It's about text helpper functions so the name looks fine to me.

* [ ]  explain what the heredoc package is and why it's used

It's explained in the PR, it's about the heredoc syntax which allows to indent texts so we do not longer have texts starting on the first col but can be indented properly like the code.

Although this is apparently simple, it is definitely not easy to review.

Yeah, the main thing which has changed is the indentation but the diff view is not very helpful here. The biggest change is the access to the flags like requested.

Since this will apparently be a pattern for future updates, could you please remove all the var usages with pointers, and instead use flags, as is done in newer commands? start.go uses this technique, for example

Done.

docs/content/users/usage/commands.md Outdated Show resolved Hide resolved
docs/content/users/usage/commands.md Outdated Show resolved Hide resolved
docs/content/users/usage/commands.md Outdated Show resolved Hide resolved
docs/content/users/usage/commands.md Outdated Show resolved Hide resolved
@rfay
Copy link
Member

rfay commented Jun 23, 2023

Just read the heredoc readme, I like that feature! Not sure why they named it "heredoc", which means something completely different in bash.

@gilbertsoft
Copy link
Member Author

Just read the heredoc readme, I like that feature! Not sure why they named it "heredoc", which means something completely different in bash.

heredoc is a name in the PHP world, not sure if somewhere else too. See https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a great improvement and good example of command setup, thanks! I manually tested and everything seems to work fine.

  • Please don't change import-db's --target-db. There was no request for this, and there's no reason to break people's existing scripts and such.
  • I can cope with the way you've implemented --file but the idea was to support it as an alternate rather than as a replacement for --src. It's OK though.
  • Please credit @miromichalicka in the commit when this goes in.
  • Would you mind changing the text package to something like "indent" instead? "text" is already the name of dozens of packages.
  • Interested why the package doesn't just go into pkg; that would be the natural place for it. Unless you have a strong argument I'd prefer for it to be there.

cmd/ddev/cmd/import-db.go Show resolved Hide resolved
@rfay rfay changed the title Streamline flags of import- and export-db commands, fixes #4593 Streamline flags of import- and export-db commands, use --file for import-db, fixes #4593 Jun 23, 2023
@rfay rfay changed the title Streamline flags of import- and export-db commands, use --file for import-db, fixes #4593 Use --file for import-db and rework import-db and export-db, fixes #4593 Jun 23, 2023
@gilbertsoft
Copy link
Member Author

This seems like a great improvement and good example of command setup, thanks! I manually tested and everything seems to work fine.

* Please don't change import-db's --target-db. There was no request for this, and there's no reason to break people's existing scripts and such.

Nothing will break, --target-db still exists. And I really like to have this streamlined with other tools e.g. mysqldump, that's why I've changed it in the first place. We should always try to use standards from other tools so it easier for people to use.

* I can cope with the way you've implemented `--file` but the idea was to support it as an alternate rather than as a replacement for `--src`. It's OK though.

Same as above, src still exists. But we can not use source for import because there it's the target or destination but file we can use in all places.

* Please credit @miromichalicka in the commit when this goes in.

* Would you mind changing the text package to something like "indent" instead? "text" is already the name of dozens of packages.

* Interested why the package doesn't just go into pkg; that would be the natural place for it. Unless you have a strong argument I'd prefer for it to be there.

Changed it to heredoc, best name I've found and also moved to pkg now.

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, --target-db does not work here, which is why the tests are failing. Looks like it's just a bug that makes it not work though. Please manually test.

$ ddev import-db --src=.tarballs/db.sql.gz --target-db=junk
Flag --src has been deprecated, please use --file instead
Flag --target-db has been deprecated, please use --database instead
Error: failed to import database 'db' for d9: Unable to validate import asset junk: invalid asset: file not found
Usage:
  ddev import-db [project] [flags]

Examples:
  $ ddev import-db
  $ ddev import-db --file=.tarballs/junk.sql
  $ ddev import-db --file=.tarballs/junk.sql.gz
  $ ddev import-db --database=other_db --file=.tarballs/db.sql.gz
  $ ddev import-db --file=.tarballs/db.sql.bz2
  $ ddev import-db --file=.tarballs/db.sql.xz
  $ ddev import-db < db.sql
  $ ddev import-db some-project < db.sql
  $ gzip -dc db.sql.gz | ddev import-db


Flags:
  -d, --database string       Target database to import into (default "db")
      --extract-path string   Path to extract within the archive
  -f, --file string           Path to a SQL dump file, plain text or compressed (tar/tar.gz/tgz/zip)
  -h, --help                  help for import-db
      --no-drop               Do not drop the database before importing
      --no-progress           Do not output progress

Global Flags:
  -j, --json-output   If true, user-oriented output will be in JSON format.

@gilbertsoft
Copy link
Member Author

No, --target-db does not work here, which is why the tests are failing. Looks like it's just a bug that makes it not work though. Please manually test.

$ ddev import-db --src=.tarballs/db.sql.gz --target-db=junk
Flag --src has been deprecated, please use --file instead
Flag --target-db has been deprecated, please use --database instead
Error: failed to import database 'db' for d9: Unable to validate import asset junk: invalid asset: file not found
Usage:
  ddev import-db [project] [flags]

Examples:
  $ ddev import-db
  $ ddev import-db --file=.tarballs/junk.sql
  $ ddev import-db --file=.tarballs/junk.sql.gz
  $ ddev import-db --database=other_db --file=.tarballs/db.sql.gz
  $ ddev import-db --file=.tarballs/db.sql.bz2
  $ ddev import-db --file=.tarballs/db.sql.xz
  $ ddev import-db < db.sql
  $ ddev import-db some-project < db.sql
  $ gzip -dc db.sql.gz | ddev import-db


Flags:
  -d, --database string       Target database to import into (default "db")
      --extract-path string   Path to extract within the archive
  -f, --file string           Path to a SQL dump file, plain text or compressed (tar/tar.gz/tgz/zip)
  -h, --help                  help for import-db
      --no-drop               Do not drop the database before importing
      --no-progress           Do not output progress

Global Flags:
  -j, --json-output   If true, user-oriented output will be in JSON format.

Looks like a bug while reading the flags. Will have a look....

@gilbertsoft
Copy link
Member Author

gilbertsoft commented Jun 25, 2023

Found the problem, copy&paste issue. Same in the db-export :(

@rfay
Copy link
Member

rfay commented Jun 26, 2023

Did you intentionally re-add the -z flag? It's still there...

$ ddev --version
ddev version v1.22.0-alpha3-10-g0069dfdba
$ ddev help export-db
Dump a database to a file or to stdout.

Usage:
  ddev export-db [project] [flags]

Examples:
  $ ddev export-db --file=/tmp/db.sql.gz
  $ ddev export-db -f /tmp/db.sql.gz
  $ ddev export-db --gzip=false --file /tmp/db.sql
  $ ddev export-db > /tmp/db.sql.gz
  $ ddev export-db --gzip=false > /tmp/db.sql
  $ ddev export-db --database=additional_db --file=.tarballs/additional_db.sql.gz
  $ ddev export-db my-project --gzip=false --file=/tmp/my_project.sql


Flags:
      --bzip2             Use bzip2 compression
  -d, --database string   Target database to export from (default "db")
  -f, --file string       Path to a SQL dump file to export to
  -z, --gzip              Use gzip compression (default true)
  -h, --help              help for export-db
      --xz                Use xz compression

Global Flags:
  -j, --json-output   If true, user-oriented output will be in JSON format.
rfay@rfay-tag1-m1:~/workspace/d9$ ddev export-db -z --file .tarballs/junk2.sql.gz
Wrote database dump from project 'd9' database 'db' to file .tarballs/junk2.sql.gz in gzip format.

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, --target-db still doesn't work:

Edit: I messed up the command, it's working

Copy link
Member

@rfay rfay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it's ready to go, thanks! Manually tested as well.

@rfay rfay merged commit 2d481a4 into ddev:master Jun 26, 2023
17 checks passed
@rfay rfay deleted the task/streamline-file-flags branch June 26, 2023 18:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants