@@ -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.
611638func (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
0 commit comments