|
| 1 | +// Copyright 2024 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package typecollection |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + |
| 21 | + "github.com/dolthub/doltgresql/server/types" |
| 22 | +) |
| 23 | + |
| 24 | +// Merge handles merging sequences on our root and their root. |
| 25 | +func Merge(ctx context.Context, ourCollection, theirCollection, ancCollection *TypeCollection) (*TypeCollection, error) { |
| 26 | + mergedCollection := ourCollection.Clone() |
| 27 | + err := theirCollection.IterateTypes(func(schema string, theirType *types.Type) error { |
| 28 | + // If we don't have the type, then we simply add it |
| 29 | + mergedType, exists := mergedCollection.GetType(schema, theirType.Name) |
| 30 | + if !exists { |
| 31 | + newSeq := *theirType |
| 32 | + return mergedCollection.CreateType(schema, &newSeq) |
| 33 | + } |
| 34 | + |
| 35 | + // Different types with the same name cannot be merged. (e.g.: 'domain' type and 'base' type with the same name) |
| 36 | + if mergedType.TypType != theirType.TypType { |
| 37 | + return fmt.Errorf(`cannot merge type "%s" because type types do not match: '%s' and '%s'"`, theirType.Name, mergedType.TypType, theirType.TypType) |
| 38 | + } |
| 39 | + |
| 40 | + switch theirType.TypType { |
| 41 | + case types.TypeType_Domain: |
| 42 | + if mergedType.BaseTypeOID != theirType.BaseTypeOID { |
| 43 | + // TODO: we can extend on this in the future (e.g.: maybe uses preferred type?) |
| 44 | + return fmt.Errorf(`base types of domain type "%s" do not match`, theirType.Name) |
| 45 | + } |
| 46 | + if mergedType.Default == "" { |
| 47 | + mergedType.Default = theirType.Default |
| 48 | + } else if theirType.Default != "" && mergedType.Default != theirType.Default { |
| 49 | + return fmt.Errorf(`default values of domain type "%s" do not match`, theirType.Name) |
| 50 | + } |
| 51 | + // if either of types defined as NOT NULL, take NOT NULL |
| 52 | + if mergedType.NotNull || theirType.NotNull { |
| 53 | + mergedType.NotNull = true |
| 54 | + } |
| 55 | + if len(theirType.Checks) > 0 { |
| 56 | + // TODO: check for duplicate check constraints |
| 57 | + mergedType.Checks = append(mergedType.Checks, theirType.Checks...) |
| 58 | + } |
| 59 | + default: |
| 60 | + // TODO: support merge for other types. (base, range, etc.) |
| 61 | + } |
| 62 | + return nil |
| 63 | + }) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + return mergedCollection, nil |
| 68 | +} |
0 commit comments