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

sql: reduce UX surprise with DROP DATABASE and ALTER DATABASE RENAME #24246

Merged
merged 1 commit into from
Mar 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions pkg/sql/drop_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func (p *planner) DropDatabase(ctx context.Context, n *tree.DropDatabase) (planN
return nil, errEmptyDatabaseName
}

if string(n.Name) == p.SessionData().Database && p.SessionData().SafeUpdates {
return nil, pgerror.NewDangerousStatementErrorf("DROP DATABASE on current database")
}

// Check that the database exists.
var dbDesc *DatabaseDescriptor
var err error
Expand Down
3 changes: 3 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/drop_database
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ CREATE DATABASE foo; CREATE TABLE foo.bar(x INT);
statement ok
SET sql_safe_updates = TRUE;

statement error DROP DATABASE on current database
DROP DATABASE test

statement error DROP DATABASE on non-empty database without explicit CASCADE
DROP DATABASE foo

Expand Down
7 changes: 7 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/rename_database
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ SELECT * FROM kv
7 8

statement ok
SET sql_safe_updates = TRUE;

statement error RENAME DATABASE on current database
ALTER DATABASE test RENAME TO u

statement ok
SET sql_safe_updates = FALSE;
ALTER DATABASE test RENAME TO u

statement error pgcode 42P01 relation "kv" does not exist
SELECT * FROM kv

Expand Down
5 changes: 5 additions & 0 deletions pkg/sql/rename_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"

"github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgerror"
"github.com/cockroachdb/cockroach/pkg/sql/privilege"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
Expand All @@ -33,6 +34,10 @@ func (p *planner) RenameDatabase(ctx context.Context, n *tree.RenameDatabase) (p
return nil, errEmptyDatabaseName
}

if string(n.Name) == p.SessionData().Database && p.SessionData().SafeUpdates {
return nil, pgerror.NewDangerousStatementErrorf("RENAME DATABASE on current database")
}

if err := p.RequireSuperUser(ctx, "ALTER DATABASE ... RENAME"); err != nil {
return nil, err
}
Expand Down