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

cherry-pick 1.1: sql: revert the default behavior of DROP DATABASE to CASCADE #19209

Merged
merged 1 commit into from Oct 19, 2017
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
18 changes: 14 additions & 4 deletions pkg/sql/drop.go
Expand Up @@ -71,10 +71,20 @@ func (p *planner) DropDatabase(ctx context.Context, n *parser.DropDatabase) (pla
return nil, err
}

if len(tbNames) > 0 && n.DropBehavior != parser.DropCascade {
return nil, pgerror.NewErrorf(pgerror.CodeDependentObjectsStillExistError,
"database %q is not empty and CASCADE was not specified",
parser.ErrString(parser.Name(dbDesc.Name)))
if len(tbNames) > 0 {
switch n.DropBehavior {
case parser.DropRestrict:
return nil, pgerror.NewErrorf(pgerror.CodeDependentObjectsStillExistError,
"database %q is not empty and RESTRICT was specified",
parser.ErrString(parser.Name(dbDesc.Name)))
case parser.DropDefault:
// The default is CASCADE, however be cautious if CASCADE was
// not specified explicitly.
if p.session.SafeUpdates {
return nil, pgerror.NewDangerousStatementErrorf(
"DROP DATABASE on non-empty database without explicit CASCADE")
}
}
}

td := make([]*sqlbase.TableDescriptor, len(tbNames))
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/drop_test.go
Expand Up @@ -162,7 +162,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
}

if _, err := sqlDB.Exec(`DROP DATABASE t`); !testutils.IsError(err,
if _, err := sqlDB.Exec(`DROP DATABASE t RESTRICT`); !testutils.IsError(err,
`database "t" is not empty`) {
t.Fatal(err)
}
Expand Down Expand Up @@ -286,7 +286,7 @@ INSERT INTO t.kv VALUES ('c', 'e'), ('a', 'c'), ('b', 'd');
t.Fatalf("expected %d key value pairs, but got %d", l, len(kvs))
}

if _, err := sqlDB.Exec(`DROP DATABASE t`); !testutils.IsError(err,
if _, err := sqlDB.Exec(`DROP DATABASE t RESTRICT`); !testutils.IsError(err,
`database "t" is not empty`) {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/logictest/testdata/logic_test/database
Expand Up @@ -99,7 +99,7 @@ SELECT * FROM b.a
7

statement error database "b" is not empty
DROP DATABASE b
DROP DATABASE b RESTRICT

statement ok
DROP DATABASE b CASCADE
Expand Down
16 changes: 15 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/drop_database
Expand Up @@ -16,7 +16,7 @@ test
statement ok
CREATE TABLE "foo-bar".t(x INT)

statement error database.*is not empty and CASCADE was not specified
statement error database.*is not empty and RESTRICT was specified
DROP DATABASE "foo-bar" RESTRICT

statement ok
Expand Down Expand Up @@ -225,3 +225,17 @@ test

query error database "constraint_db" does not exist
SELECT * FROM constraint_db.t1

# Check that the default option is CASCADE, but that safe_updates blocks it

statement ok
CREATE DATABASE foo; CREATE TABLE foo.bar(x INT);

statement ok
SET sql_safe_updates = TRUE;

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

statement ok
SET sql_safe_updates = FALSE; DROP DATABASE foo