Skip to content

Commit 3014ad6

Browse files
committed
Drop schema support
1 parent ddcf88a commit 3014ad6

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

core/rootvalue.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package core
1717
import (
1818
"bytes"
1919
"context"
20+
"fmt"
2021
"slices"
2122
"sort"
2223
"strconv"
@@ -87,6 +88,35 @@ func (root *RootValue) CreateDatabaseSchema(ctx context.Context, dbSchema schema
8788
return root.withStorage(r), nil
8889
}
8990

91+
// DropDatabaseSchema implements the interface doltdb.RootValue.
92+
func (root *RootValue) DropDatabaseSchema(ctx context.Context, dbSchema schema.DatabaseSchema) (doltdb.RootValue, error) {
93+
schemas, err := root.st.GetSchemas(ctx)
94+
if err != nil {
95+
return nil, err
96+
}
97+
98+
found := false
99+
for i, s := range schemas {
100+
if strings.EqualFold(s.Name, dbSchema.Name) {
101+
found = true
102+
// remove this element in the slice
103+
schemas = append(schemas[:i], schemas[i+1:]...)
104+
break
105+
}
106+
}
107+
108+
if !found {
109+
return nil, fmt.Errorf("No schema with the name %s exists", dbSchema.Name)
110+
}
111+
112+
r, err := root.st.SetSchemas(ctx, schemas)
113+
if err != nil {
114+
return nil, err
115+
}
116+
117+
return root.withStorage(r), nil
118+
}
119+
90120
// validateSchemaForCreate returns an error if a schema with the name given cannot be created
91121
func validateSchemaForCreate(existingSchemas []schema.DatabaseSchema, dbSchema schema.DatabaseSchema) error {
92122
if dbSchema.Name == "" {
@@ -579,10 +609,10 @@ func (root *RootValue) PutTable(ctx context.Context, tName doltdb.TableName, tab
579609

580610
// RemoveTables implements the interface doltdb.RootValue.
581611
func (root *RootValue) RemoveTables(
582-
ctx context.Context,
583-
skipFKHandling bool,
584-
allowDroppingFKReferenced bool,
585-
originalTables ...doltdb.TableName,
612+
ctx context.Context,
613+
skipFKHandling bool,
614+
allowDroppingFKReferenced bool,
615+
originalTables ...doltdb.TableName,
586616
) (doltdb.RootValue, error) {
587617
if len(originalTables) == 0 {
588618
return root, nil

server/ast/drop_schema.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ func nodeDropSchema(ctx *Context, node *tree.DropSchema) (vitess.Statement, erro
2727
return nil, nil
2828
}
2929

30-
return NotYetSupportedError("DROP SCHEMA is not yet supported")
30+
if len(node.Names) > 1 {
31+
return NotYetSupportedError("DROP SCHEMA with multiple schema names")
32+
}
33+
34+
schemaName := node.Names[0]
35+
36+
return &vitess.DBDDL{
37+
Action: vitess.DropStr,
38+
SchemaOrDatabase: "schema",
39+
DBName: schemaName,
40+
CharsetCollate: nil,
41+
IfExists: node.IfExists,
42+
}, nil
3143
}

0 commit comments

Comments
 (0)