MCPcopy Create free account
hub / github.com/dolthub/doltgresql / DropDatabaseSchema

Method DropDatabaseSchema

core/rootvalue.go:85–138  ·  view source on GitHub ↗

DropDatabaseSchema implements the interface doltdb.RootValue.

(ctx context.Context, dbSchema schema.DatabaseSchema)

Source from the content-addressed store, hash-verified

83
84// DropDatabaseSchema implements the interface doltdb.RootValue.
85func (root *RootValue) DropDatabaseSchema(ctx context.Context, dbSchema schema.DatabaseSchema) (doltdb.RootValue, error) {
86 schemas, err := root.st.GetSchemas(ctx)
87 if err != nil {
88 return nil, err
89 }
90
91 found := false
92 schemaName := dbSchema.Name
93 for i, s := range schemas {
94 if strings.EqualFold(s.Name, dbSchema.Name) {
95 found = true
96 schemaName = s.Name
97 // remove this element in the slice
98 schemas = append(schemas[:i], schemas[i+1:]...)
99 break
100 }
101 }
102
103 if !found {
104 return nil, fmt.Errorf("No schema with the name %s exists", dbSchema.Name)
105 }
106
107 // Check for dangling objects in the schema and reject the drop if there are any
108 danglingObjects := false
109 root.IterRootObjects(ctx, func(name doltdb.TableName, table doltdb.RootObject) (stop bool, err error) {
110 if strings.EqualFold(name.Schema, dbSchema.Name) {
111 danglingObjects = true
112 }
113 return false, nil
114 })
115
116 // check the tables specifically in addition to root objects. This is just an extra paranoid step to avoid
117 // removing a schema that still has tables.
118 tableMap, err := root.getTableMap(ctx, schemaName)
119 if err != nil {
120 return nil, err
121 }
122
123 tableMap.Iter(ctx, func(name string, addr hash.Hash) (bool, error) {
124 danglingObjects = true
125 return true, nil
126 })
127
128 if danglingObjects {
129 return nil, fmt.Errorf("cannot drop schema %s because other objects depend on it", dbSchema.Name)
130 }
131
132 r, err := root.st.SetSchemas(ctx, schemas)
133 if err != nil {
134 return nil, err
135 }
136
137 return root.withStorage(r), nil
138}
139
140// validateSchemaForCreate returns an error if a schema with the name given cannot be created
141func validateSchemaForCreate(existingSchemas []schema.DatabaseSchema, dbSchema schema.DatabaseSchema) error {

Callers

nothing calls this directly

Calls 7

IterRootObjectsMethod · 0.95
getTableMapMethod · 0.95
withStorageMethod · 0.95
GetSchemasMethod · 0.80
SetSchemasMethod · 0.80
ErrorfMethod · 0.65
IterMethod · 0.45

Tested by

no test coverage detected