Skip to content

Commit

Permalink
feat: add ddev debug match-constraint (ddev#6645) [skip ci]
Browse files Browse the repository at this point in the history
Co-authored-by: Stanislav Zhuk <[email protected]>
Co-authored-by: Randy Fay <[email protected]>
  • Loading branch information
3 people authored Nov 3, 2024
1 parent 00407fd commit 749449e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/debug-capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestDebugCapabilitiesCmd(t *testing.T) {

jsonCapabilities := make(map[string]interface{})
err = json.Unmarshal([]byte(out), &jsonCapabilities)
require.NoError(t, err, "failed to unmarshal json capabilities '%v', out")
require.NoError(t, err, "failed to unmarshal json capabilities '%v'", out)
caps, ok := jsonCapabilities["raw"]
require.True(t, ok, "raw section wasn't found in jsonCapabilities: %v", out)
sArr := []string{}
Expand Down
27 changes: 27 additions & 0 deletions cmd/ddev/cmd/debug-match-constraint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"github.com/ddev/ddev/pkg/ddevapp"
"github.com/ddev/ddev/pkg/util"
"github.com/spf13/cobra"
)

// DebugMatchConstraintCmd Compares a constraint against the installed DDEV version.
var DebugMatchConstraintCmd = &cobra.Command{
Use: "match-constraint",
Short: "Check if the currently installed ddev matches the specified version constraint.",
Args: cobra.ExactArgs(1),
Example: `ddev debug match-constraint ">= 1.24.0"`,
Run: func(_ *cobra.Command, args []string) {
versionConstraint := args[0]

err := ddevapp.CheckDdevVersionConstraint(versionConstraint, "", "")
if err != nil {
util.Failed(err.Error())
}
},
}

func init() {
DebugCmd.AddCommand(DebugMatchConstraintCmd)
}
39 changes: 39 additions & 0 deletions cmd/ddev/cmd/debug-match-constraint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cmd

import (
"fmt"
"github.com/ddev/ddev/pkg/exec"
"github.com/ddev/ddev/pkg/versionconstants"
"github.com/stretchr/testify/require"
"regexp"
"testing"
)

// TestDebugMatchConstraintCmd checks to see match-constraint behaves as expected
// @see https://github.com/Masterminds/semver#checking-version-constraints
func TestDebugMatchConstraintCmd(t *testing.T) {
out, err := exec.RunHostCommand(DdevBin, "debug", "match-constraint", "-h")
require.NoError(t, err, "Match constraint should not have errored for help, out='%s'", out)
require.Contains(t, out, "Check if the currently installed ddev matches the specified version constraint")

constraint := ">= 1.twentythree"
out, err = exec.RunHostCommand(DdevBin, "debug", "match-constraint", constraint)
require.Error(t, err, "Match constraint should have errored for %s, out='%s'", constraint, out)
require.Contains(t, out, "constraint is not valid")

constraint = "!= v0.0.0-overridden-by-make"
out, err = exec.RunHostCommand(DdevBin, "debug", "match-constraint", constraint)
require.NoError(t, err, "Match constraint should not have errored for %s, out='%s'", constraint, out)

if !regexp.MustCompile(`^v[0-9]+\.`).MatchString(versionconstants.DdevVersion) {
t.Skip(fmt.Sprintf("Skipping check for semver because DDEV version doesn't start with any valid version tag, it's '%v'", versionconstants.DdevVersion))
}

constraint = ">= 1.0"
out, err = exec.RunHostCommand(DdevBin, "debug", "match-constraint", constraint)
require.NoError(t, err, "Match constraint should not have errored for %s, out='%s'", constraint, out)

constraint = "< 1.0"
out, err = exec.RunHostCommand(DdevBin, "debug", "match-constraint", constraint)
require.Error(t, err, "Match constraint should have errored for %s, out='%s'", constraint, out)
}
20 changes: 20 additions & 0 deletions docs/content/users/usage/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,26 @@ Example:
ddev debug get-volume-db-version
```

### `debug match-constraint`

Check if the currently installed ddev matches the specified [version constraint](https://github.com/Masterminds/semver#checking-version-constraints).

Example:

```shell
# This is only supported with DDEV versions above v1.24.0
if ddev debug match-constraint "< 1.25" >/dev/null 2>&1; then
# do something for ddev versions below 1.25
...
else
# do something for ddev versions 1.25+
...
fi
```

!!!tip
You can also configure a [ddev version constraint per project](../configuration/config.md#ddev_version_constraint).

### `debug migrate-database`

Migrate a MySQL or MariaDB database to a different `dbtype:dbversion`. Works only with MySQL and MariaDB, not with PostgreSQL. It will export your database, create a snapshot, destroy your current database, and import into the new database type. It only migrates the 'db' database. It will update the database version in your project's `config.yaml` file.
Expand Down

0 comments on commit 749449e

Please sign in to comment.