Skip to content

Commit e553fd3

Browse files
committed
working drop schema
1 parent ad6648d commit e553fd3

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

core/rootvalue.go

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,11 @@ func (root *RootValue) DropDatabaseSchema(ctx context.Context, dbSchema schema.D
9696
}
9797

9898
found := false
99+
schemaName := dbSchema.Name
99100
for i, s := range schemas {
100101
if strings.EqualFold(s.Name, dbSchema.Name) {
101102
found = true
103+
schemaName = s.Name
102104
// remove this element in the slice
103105
schemas = append(schemas[:i], schemas[i+1:]...)
104106
break
@@ -109,6 +111,31 @@ func (root *RootValue) DropDatabaseSchema(ctx context.Context, dbSchema schema.D
109111
return nil, fmt.Errorf("No schema with the name %s exists", dbSchema.Name)
110112
}
111113

114+
// Check for dangling objects in the schema and reject the drop if there are any
115+
danglingObjects := false
116+
root.IterRootObjects(ctx, func(name doltdb.TableName, table doltdb.RootObject) (stop bool, err error) {
117+
if strings.EqualFold(name.Schema, dbSchema.Name) {
118+
danglingObjects = true
119+
}
120+
return false, nil
121+
})
122+
123+
// check the tables specifically in addition to root objects. This is just an extra paranoid step to avoid
124+
// removing a schema that still has tables.
125+
tableMap, err := root.getTableMap(ctx, schemaName)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
tableMap.Iter(ctx, func(name string, addr hash.Hash) (bool, error) {
131+
danglingObjects = true
132+
return true, nil
133+
})
134+
135+
if danglingObjects {
136+
return nil, fmt.Errorf("cannot drop schema %s because other objects depend on it", dbSchema.Name)
137+
}
138+
112139
r, err := root.st.SetSchemas(ctx, schemas)
113140
if err != nil {
114141
return nil, err
@@ -609,10 +636,10 @@ func (root *RootValue) PutTable(ctx context.Context, tName doltdb.TableName, tab
609636

610637
// RemoveTables implements the interface doltdb.RootValue.
611638
func (root *RootValue) RemoveTables(
612-
ctx context.Context,
613-
skipFKHandling bool,
614-
allowDroppingFKReferenced bool,
615-
originalTables ...doltdb.TableName,
639+
ctx context.Context,
640+
skipFKHandling bool,
641+
allowDroppingFKReferenced bool,
642+
originalTables ...doltdb.TableName,
616643
) (doltdb.RootValue, error) {
617644
if len(originalTables) == 0 {
618645
return root, nil

server/ast/drop_schema.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ func nodeDropSchema(ctx *Context, node *tree.DropSchema) (vitess.Statement, erro
2828
}
2929

3030
if len(node.Names) > 1 {
31-
return NotYetSupportedError("DROP SCHEMA with multiple schema names")
31+
return NotYetSupportedError("DROP SCHEMA with multiple schema names is not yet supported.")
32+
}
33+
34+
if node.DropBehavior == tree.DropCascade {
35+
return NotYetSupportedError("DROP SCHEMA with CASCADE behavior is not yet supported.")
3236
}
3337

3438
schemaName := node.Names[0]

server/ast/no_op.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ func NotYetSupportedError(errorMsg string) (vitess.Statement, error) {
5353
return NewNoOp(errorMsg), nil
5454
}
5555

56-
return nil, errors.New(errorMsg)
56+
return nil, errors.Errorf(errorMsg + " Please file an issue at https://github.com/dolthub/doltgresql/issues")
5757
}

testing/go/schemas_test.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ var SchemaTests = []ScriptTest{
874874
Focus: true,
875875
SetUpScript: []string{
876876
"CREATE SCHEMA dropme",
877-
"CREATE schema hasTables",
877+
`CREATE schema "hasTables"`,
878878
"CREATE TABLE hasTables.t1 (pk BIGINT PRIMARY KEY, v1 BIGINT);",
879879
},
880880
Assertions: []ScriptTestAssertion{
@@ -893,9 +893,30 @@ var SchemaTests = []ScriptTest{
893893
Query: "DROP SCHEMA dropme;",
894894
Expected: []sql.Row{},
895895
},
896+
{
897+
Query: "Show schemas",
898+
Expected: []sql.Row{
899+
{"dolt"},
900+
{"hasTables"},
901+
{"pg_catalog"},
902+
{"public"},
903+
{"information_schema"},
904+
},
905+
},
896906
{
897907
Query: "DROP SCHEMA dropme;",
898-
ExpectedErr: "schema dropme does not exist",
908+
ExpectedErr: "database schema not found",
909+
},
910+
{
911+
Query: "drop schema if exists dropme;",
912+
},
913+
{
914+
Query: "DROP SCHEMA hasTables;",
915+
ExpectedErr: "cannot drop schema hastables because other objects depend on it",
916+
},
917+
{
918+
Skip: true, // not implemented yet
919+
Query: "drop schema hasTables cascade;",
899920
},
900921
},
901922
},

0 commit comments

Comments
 (0)