First pass dolt rm cmd

This commit is contained in:
Nathan Gabrielson
2025-07-01 17:18:52 -07:00
parent 4f68ae838d
commit eaaba8ea67
4 changed files with 315 additions and 0 deletions

126
go/cmd/dolt/commands/rm.go Normal file
View File

@@ -0,0 +1,126 @@
// Copyright 2025 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package commands
import (
"bytes"
"context"
"github.com/dolthub/dolt/go/cmd/dolt/cli"
"github.com/dolthub/dolt/go/cmd/dolt/errhand"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/utils/argparser"
"github.com/dolthub/go-mysql-server/sql"
"github.com/gocraft/dbr/v2"
"github.com/gocraft/dbr/v2/dialect"
)
var rmDocs = cli.CommandDocumentationContent{
ShortDesc: `Drops a table and removes it from tracking`,
LongDesc: `
In it's default mode, this command drops a table and removes it from tracking. Without '--cached', you can only call rm on committed tables.
The option '--cached' can be used to untrack tables, but leave them in the working set. You can restage them with 'dolt add'.
The dolt status command can be used to obtain a summary of which tables have changes that are staged for the next commit.'`,
Synopsis: []string{
`[{{.LessThan}}table{{.GreaterThan}}...]`,
},
}
type RmCmd struct{}
/*var _ cli.RepoNotRequiredCommand = RmCmd{}
func (cmd RmCmd) RequiresRepo() bool {
return false
}*/ //TODO DO WE NEED THIS
// Name is returns the name of the Dolt cli command. This is what is used on the command line to invoke the command
func (cmd RmCmd) Name() string {
return "rm"
}
// Description returns a description of the command
func (cmd RmCmd) Description() string {
return "Drops a table and removes it from tracking"
}
func (cmd RmCmd) Docs() *cli.CommandDocumentation {
ap := cmd.ArgParser()
return cli.NewCommandDocumentation(rmDocs, ap)
}
func (cmd RmCmd) ArgParser() *argparser.ArgParser {
return cli.CreateRmArgParser()
}
func (cmd RmCmd) Exec(ctx context.Context, commandStr string, args []string, _ *env.DoltEnv, cliCtx cli.CliContext) int {
ap := cmd.ArgParser()
apr, _, terminate, status := ParseArgsOrPrintHelp(ap, commandStr, args, rmDocs)
if terminate {
return status
}
errorBuilder := errhand.BuildDError("error: failed to create query engine")
queryEngine, sqlCtx, closeFunc, err := cliCtx.QueryEngine(ctx)
if err != nil {
return HandleVErrAndExitCode(errorBuilder.AddCause(err).Build(), nil)
}
if closeFunc != nil {
defer closeFunc()
}
interpolatedQuery, err := generateRmSql(args)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), nil)
}
_, rowIter, _, err := queryEngine.Query(sqlCtx, interpolatedQuery)
if err != nil {
return HandleVErrAndExitCode(errhand.VerboseErrorFromError(err), nil)
}
_, err = sql.RowIterToRows(sqlCtx, rowIter)
if err != nil {
cli.PrintErrln(errhand.VerboseErrorFromError(err))
return 1
}
for _, arg := range apr.Args {
cli.Printf("rm '%s'\n", arg)
}
return 0
}
func generateRmSql(args []string) (string, error) {
var buffer bytes.Buffer
var first bool
queryValues := make([]interface{}, 0, len(args))
first = true
buffer.WriteString("CALL DOLT_RM(")
for _, arg := range args {
if !first {
buffer.WriteString(", ")
}
first = false
buffer.WriteString("?")
queryValues = append(queryValues, arg)
}
buffer.WriteString(")")
return dbr.InterpolateForDialect(buffer.String(), queryValues, dialect.MySQL)
}

View File

@@ -91,6 +91,7 @@ var doltSubCommands = []cli.Command{
commands.ArchiveCmd{},
ci.Commands,
commands.DebugCmd{},
commands.RmCmd{},
}
var DoltCommand = cli.NewSubCommandHandler("dolt", "it's git for data", doltSubCommands)

View File

@@ -58,6 +58,24 @@ var DoltRmTests = []queries.ScriptTest{
},
},
},
{
Name: "dolt Rm multiple tables",
SetUpScript: []string{
"CREATE TABLE test1 (i int)",
"CREATE TABLE test2 (i int)",
"CALL DOLT_COMMIT('-A', '-m', 'created tables')",
},
Assertions: []queries.ScriptTestAssertion{
{
Query: "call dolt_rm('test1', 'test2');",
Expected: []sql.Row{{0}},
},
{
Query: "show tables;",
Expected: []sql.Row{},
},
},
},
{
Name: "dolt Rm staged table",
SetUpScript: []string{

View File

@@ -0,0 +1,170 @@
#!/usr/bin/env bats
load $BATS_TEST_DIRNAME/helper/common.bash
setup() {
setup_common
}
teardown() {
assert_feature_version
teardown_common
}
@test "rm: dolt rm without tables" {
run dolt rm
[ "$status" -eq 1 ]
[[ "$output" =~ "Nothing specified, nothing removed. Which tables should I remove?" ]] || false
}
@test "rm: simple dolt rm" {
dolt sql -q "CREATE TABLE test (i int)"
dolt commit -A -m "created table"
run dolt rm test
[ "$status" -eq 0 ]
[[ "$output" =~ "rm 'test'" ]] || false
run dolt ls
[ "$status" -eq 0 ]
[[ "$output" =~ "No tables in working set" ]] || false
}
@test "rm: dolt rm multiple tables" {
dolt sql -q "CREATE TABLE test1 (i int)"
dolt sql -q "CREATE TABLE test2 (i int)"
dolt commit -A -m "created tables"
run dolt rm test1 test2
[ "$status" -eq 0 ]
[[ "$output" =~ "rm 'test1'" ]] || false
[[ "$output" =~ "rm 'test2'" ]] || false
run dolt ls
[ "$status" -eq 0 ]
[[ "$output" =~ "No tables in working set" ]] || false
}
@test "rm: dolt rm staged table" {
dolt sql -q "CREATE TABLE test (i int)"
dolt add test
run dolt rm test
[ "$status" -eq 1 ]
[[ "$output" =~ "error: the table(s) test have changes saved in the index. Use --cached or commit." ]] || false
}
@test "rm: dolt rm unstaged table" {
dolt sql -q "CREATE TABLE test (i int)"
run dolt rm test
[ "$status" -eq 1 ]
[[ "$output" =~ "error: the table(s) test have unstaged changes." ]] || false
}
@test "rm: dolt rm staged table with unstaged changes" {
dolt sql -q "CREATE TABLE test (i int)"
dolt add test
dolt sql -q "INSERT INTO test VALUES (1)"
run dolt rm test
[ "$status" -eq 1 ]
[[ "$output" =~ "error: the table(s) test have unstaged changes." ]] || false
}
@test "rm: dolt rm committed table with unstaged changes" {
dolt sql -q "CREATE TABLE test (i int)"
dolt commit -A -m "created table"
dolt sql -q "INSERT INTO test VALUES (1)"
run dolt rm test
[ "$status" -eq 1 ]
[[ "$output" =~ "error: the table(s) test have unstaged changes." ]] || false
}
@test "rm: dolt rm with cached option" {
dolt sql -q "CREATE TABLE test (i int)"
dolt commit -A -m "created table"
run dolt rm --cached test
[ "$status" -eq 0 ]
[[ "$output" =~ "rm 'test'" ]] || false
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "deleted:" ]] || false
[[ "$output" =~ "test" ]] || false
[[ "$output" =~ "new table:" ]] || false
}
@test "rm: dolt rm staged table with cached option" {
dolt sql -q "CREATE TABLE test (i int)"
dolt add test
run dolt rm --cached test
[ "$status" -eq 0 ]
[[ "$output" =~ "rm 'test'" ]] || false
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "Untracked tables:" ]] || false
[[ "$output" =~ "new table:" ]] || false
[[ "$output" =~ "test" ]] || false
}
@test "rm: dolt rm staged and unstaged with cached option" {
dolt sql -q "CREATE TABLE committed (i int)"
dolt commit -A -m "created table"
dolt sql -q "CREATE TABLE staged (i int)"
dolt add staged
run dolt rm --cached committed staged
[ "$status" -eq 0 ]
[[ "$output" =~ "rm 'committed'" ]] || false
[[ "$output" =~ "rm 'staged'" ]] || false
run dolt status
[ "$status" -eq 0 ]
[[ "$output" =~ "deleted:" ]] || false
[[ "$output" =~ "committed" ]] || false
[[ "$output" =~ "new table:" ]] || false
[[ "$output" =~ "staged" ]] || false
}
@test "rm: dolt rm errors on foreign key constrained table" {
dolt sql -q "CREATE TABLE parent (pk int primary key, p1 int)"
dolt sql -q "CREATE TABLE child (pk int primary key, c1 int, FOREIGN KEY (c1) REFERENCES parent (pk))"
dolt commit -A -m "created tables"
run dolt rm parent
[ "$status" -eq 1 ]
[[ "$output" =~ "unable to remove \`parent\` since it is referenced from table \`child\`" ]] || false
}
@test "rm: dolt rm errors on staged foreign key constrained table with cached option" {
dolt sql -q "CREATE TABLE parent (pk int primary key, p1 int)"
dolt commit -A -m "created tables"
dolt sql -q "CREATE TABLE child (pk int primary key, c1 int, FOREIGN KEY (c1) REFERENCES parent (pk))"
dolt add child
run dolt rm --cached parent
[ "$status" -eq 1 ]
[[ "$output" =~ "unable to remove \`parent\` since it is referenced from table \`child\`" ]] || false
}
@test "rm: dolt rm errors on unstaged foreign key constrained table with cached option" {
dolt sql -q "CREATE TABLE parent (pk int primary key, p1 int)"
dolt commit -A -m "created tables"
dolt sql -q "CREATE TABLE child (pk int primary key, c1 int, FOREIGN KEY (c1) REFERENCES parent (pk))"
run dolt rm --cached parent
[ "$status" -eq 1 ]
[[ "$output" =~ "unable to remove \`parent\` since it is referenced from table \`child\`" ]] || false
}
@test "rm: dolt rm nonexistent table" {
run dolt rm nonexistent
echo "$output"
[ "$status" -eq 1 ]
[[ "$output" =~ "error: the table(s) nonexistent do not exist" ]] || false
}