Skip to content

Commit 92cfa3f

Browse files
authored
Merge pull request #10281 from dolthub/zachmu/drop-schema
drop schema support
2 parents f3ed2a9 + 6885a3b commit 92cfa3f

File tree

6 files changed

+109
-6
lines changed

6 files changed

+109
-6
lines changed

go/go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ require (
6161
github.com/dolthub/dolt-mcp v0.2.2
6262
github.com/dolthub/eventsapi_schema v0.0.0-20250915094920-eadfd39051ca
6363
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2
64-
github.com/dolthub/go-mysql-server v0.20.1-0.20260106231211-877fb58d389e
64+
github.com/dolthub/go-mysql-server v0.20.1-0.20260108001605-ff0a497ea62f
6565
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63
6666
github.com/edsrzf/mmap-go v1.2.0
6767
github.com/esote/minmaxheap v1.0.0

go/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ github.com/dolthub/fslock v0.0.0-20251215194149-ef20baba2318 h1:n+vdH5G5Db+1qnDC
195195
github.com/dolthub/fslock v0.0.0-20251215194149-ef20baba2318/go.mod h1:QWql+P17oAAMLnL4HGB5tiovtDuAjdDTPbuqx7bYfa0=
196196
github.com/dolthub/go-icu-regex v0.0.0-20250916051405-78a38d478790 h1:zxMsH7RLiG+dlZ/y0LgJHTV26XoiSJcuWq+em6t6VVc=
197197
github.com/dolthub/go-icu-regex v0.0.0-20250916051405-78a38d478790/go.mod h1:F3cnm+vMRK1HaU6+rNqQrOCyR03HHhR1GWG2gnPOqaE=
198-
github.com/dolthub/go-mysql-server v0.20.1-0.20260106231211-877fb58d389e h1:Ro9BWRdOTiZ4HaVc8U8Zj+hJSauf3inWpXw5vuy+qcg=
199-
github.com/dolthub/go-mysql-server v0.20.1-0.20260106231211-877fb58d389e/go.mod h1:T+oVXfQAETbWm9R55daMqg0HE7W7Fz7bzh+pGiQgaPI=
198+
github.com/dolthub/go-mysql-server v0.20.1-0.20260108001605-ff0a497ea62f h1:pKwWXc4w96VvPKQ81S8uVBXRJxsxRInvKZIS/hpoCg4=
199+
github.com/dolthub/go-mysql-server v0.20.1-0.20260108001605-ff0a497ea62f/go.mod h1:T+oVXfQAETbWm9R55daMqg0HE7W7Fz7bzh+pGiQgaPI=
200200
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63 h1:OAsXLAPL4du6tfbBgK0xXHZkOlos63RdKYS3Sgw/dfI=
201201
github.com/dolthub/gozstd v0.0.0-20240423170813-23a2903bca63/go.mod h1:lV7lUeuDhH5thVGDCKXbatwKy2KW80L4rMT46n+Y2/Q=
202202
github.com/dolthub/ishell v0.0.0-20240701202509-2b217167d718 h1:lT7hE5k+0nkBdj/1UOSFwjWpNxf+LCApbRHgnCA17XE=

go/libraries/doltcore/doltdb/root_val.go

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ var DoltFeatureVersion FeatureVersion = 7 // last bumped when fixing bug related
5757
// RootValue is the value of the Database and is the committed value in every Dolt or Doltgres commit.
5858
type RootValue interface {
5959
Rootish
60-
6160
// CreateDatabaseSchema creates the given schema. This differs from a table's schema.
6261
CreateDatabaseSchema(ctx context.Context, dbSchema schema.DatabaseSchema) (RootValue, error)
62+
// DropDatabaseSchema drops the given schema. This differs from a table's schema.
63+
DropDatabaseSchema(ctx context.Context, dbSchema schema.DatabaseSchema) (RootValue, error)
6364
// DebugString returns a human readable string with the contents of this root. If |transitive| is true, row data from
6465
// all tables is also included. This method is very expensive for large root values, so |transitive| should only be used
6566
// when debugging tests.
@@ -1060,6 +1061,54 @@ func (root *rootValue) CreateDatabaseSchema(ctx context.Context, dbSchema schema
10601061
return root.withStorage(r), nil
10611062
}
10621063

1064+
// DropDatabaseSchema drops a database schema from the root value.
1065+
// This is currently unused in Dolt because dolt always has only a single (unnamed) schema. But it's implemented
1066+
// because technically Dolt can support multiple schemas whenever we decide to.
1067+
func (root *rootValue) DropDatabaseSchema(ctx context.Context, dbSchema schema.DatabaseSchema) (RootValue, error) {
1068+
schemas, err := root.st.GetSchemas(ctx)
1069+
if err != nil {
1070+
return nil, err
1071+
}
1072+
1073+
found := false
1074+
schemaName := dbSchema.Name
1075+
for i, s := range schemas {
1076+
if strings.EqualFold(s.Name, dbSchema.Name) {
1077+
found = true
1078+
schemaName = s.Name
1079+
// remove this element in the slice
1080+
schemas = append(schemas[:i], schemas[i+1:]...)
1081+
break
1082+
}
1083+
}
1084+
1085+
if !found {
1086+
return nil, fmt.Errorf("No schema with the name %s exists", dbSchema.Name)
1087+
}
1088+
1089+
tableMap, err := root.getTableMap(ctx, schemaName)
1090+
if err != nil {
1091+
return nil, err
1092+
}
1093+
1094+
tablesInSchema := false
1095+
tableMap.Iter(ctx, func(name string, addr hash.Hash) (bool, error) {
1096+
tablesInSchema = true
1097+
return true, nil
1098+
})
1099+
1100+
if tablesInSchema {
1101+
return nil, fmt.Errorf("Cannot drop schema %s because it still contains tables", schemaName)
1102+
}
1103+
1104+
r, err := root.st.SetSchemas(ctx, schemas)
1105+
if err != nil {
1106+
return nil, err
1107+
}
1108+
1109+
return root.withStorage(r), nil
1110+
}
1111+
10631112
// HashOf gets the hash of the root value
10641113
func (root *rootValue) HashOf() (hash.Hash, error) {
10651114
if root.rootHash.IsEmpty() {

go/libraries/doltcore/sqle/clusterdb/database.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,18 @@ func (db database) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseS
205205
panic(fmt.Sprintf("GetSchema is not implemented for database %T", db))
206206
}
207207

208+
func (db database) SupportsDatabaseSchemas() bool {
209+
return false
210+
}
211+
208212
func (db database) CreateSchema(ctx *sql.Context, schemaName string) error {
209213
panic(fmt.Sprintf("CreateSchema is not implemented for database %T", db))
210214
}
211215

216+
func (db database) DropSchema(ctx *sql.Context, schemaName string) error {
217+
panic(fmt.Sprintf("DropSchema is not implemented for database %T", db))
218+
}
219+
212220
func (db database) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
213221
panic(fmt.Sprintf("AllSchemas is not implemented for database %T", db))
214222
}

go/libraries/doltcore/sqle/database.go

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2185,6 +2185,14 @@ func (db Database) CreateTemporaryTable(ctx *sql.Context, tableName string, pkSc
21852185
return nil
21862186
}
21872187

2188+
// SupportsDatabaseSchemas implements sql.SchemaDatabase
2189+
// TODO: this interface is only necessary because we don't have a separate sql.Database implementation for doltgres,
2190+
// which has additional capabilities (like schema creation). Dolt technically can create schemas (multiple DBs in
2191+
// the same commit graph), but there's no way for users to access this functionality currently.
2192+
func (db Database) SupportsDatabaseSchemas() bool {
2193+
return resolve.UseSearchPath
2194+
}
2195+
21882196
// CreateSchema implements sql.SchemaDatabase
21892197
func (db Database) CreateSchema(ctx *sql.Context, schemaName string) error {
21902198
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
@@ -2215,6 +2223,36 @@ func (db Database) CreateSchema(ctx *sql.Context, schemaName string) error {
22152223
return db.SetRoot(ctx, root)
22162224
}
22172225

2226+
// DropSchema implements sql.SchemaDatabase
2227+
func (db Database) DropSchema(ctx *sql.Context, schemaName string) error {
2228+
if err := dsess.CheckAccessForDb(ctx, db, branch_control.Permissions_Write); err != nil {
2229+
return err
2230+
}
2231+
2232+
root, err := db.GetRoot(ctx)
2233+
if err != nil {
2234+
return err
2235+
}
2236+
2237+
_, exists, err := doltdb.ResolveDatabaseSchema(ctx, root, schemaName)
2238+
if err != nil {
2239+
return err
2240+
}
2241+
2242+
if !exists {
2243+
return sql.ErrDatabaseSchemaNotFound.New(schemaName)
2244+
}
2245+
2246+
root, err = root.DropDatabaseSchema(ctx, schema.DatabaseSchema{
2247+
Name: schemaName,
2248+
})
2249+
if err != nil {
2250+
return err
2251+
}
2252+
2253+
return db.SetRoot(ctx, root)
2254+
}
2255+
22182256
// GetSchema implements sql.SchemaDatabase
22192257
func (db Database) GetSchema(ctx *sql.Context, schemaName string) (sql.DatabaseSchema, bool, error) {
22202258
// For doltgres, the information_schema database should be a schema.
@@ -2570,13 +2608,13 @@ func (db Database) CreateTrigger(ctx *sql.Context, definition sql.TriggerDefinit
25702608
definition.Name,
25712609
definition.CreateStatement,
25722610
definition.CreatedAt,
2573-
fmt.Errorf("triggers `%s` already exists", definition.Name), //TODO: add a sql error and return that instead
2611+
fmt.Errorf("triggers `%s` already exists", definition.Name), // TODO: add a sql error and return that instead
25742612
)
25752613
}
25762614

25772615
// DropTrigger implements sql.TriggerDatabase.
25782616
func (db Database) DropTrigger(ctx *sql.Context, name string) error {
2579-
//TODO: add a sql error and use that as the param error instead
2617+
// TODO: add a sql error and use that as the param error instead
25802618
return db.dropFragFromSchemasTable(ctx, "trigger", name, sql.ErrTriggerDoesNotExist.New(name))
25812619
}
25822620

go/libraries/doltcore/sqle/user_space_database.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,18 @@ func (db *UserSpaceDatabase) GetSchema(ctx *sql.Context, schemaName string) (sql
152152
panic(fmt.Sprintf("GetSchema is not implemented for database %T", db))
153153
}
154154

155+
func (db UserSpaceDatabase) SupportsDatabaseSchemas() bool {
156+
return false
157+
}
158+
155159
func (db *UserSpaceDatabase) CreateSchema(ctx *sql.Context, schemaName string) error {
156160
panic(fmt.Sprintf("CreateSchema is not implemented for database %T", db))
157161
}
158162

163+
func (db *UserSpaceDatabase) DropSchema(ctx *sql.Context, schemaName string) error {
164+
panic(fmt.Sprintf("DropSchema is not implemented for database %T", db))
165+
}
166+
159167
func (db *UserSpaceDatabase) AllSchemas(ctx *sql.Context) ([]sql.DatabaseSchema, error) {
160168
panic(fmt.Sprintf("AllSchemas is not implemented for database %T", db))
161169
}

0 commit comments

Comments
 (0)