|
1 | 1 | package diff |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "strconv" |
5 | 6 | "strings" |
6 | 7 |
|
@@ -40,6 +41,9 @@ func (e *DiffEngine) Compare() types.DiffList { |
40 | 41 | // Materialized View comparison |
41 | 42 | differences = append(differences, e.compareMaterializedViews()...) |
42 | 43 |
|
| 44 | + // Function comparison |
| 45 | + differences = append(differences, e.compareFunctions()...) |
| 46 | + |
43 | 47 | return differences |
44 | 48 | } |
45 | 49 |
|
@@ -725,3 +729,55 @@ func (e *DiffEngine) compareMaterializedViews() types.DiffList { |
725 | 729 |
|
726 | 730 | return differences |
727 | 731 | } |
| 732 | + |
| 733 | +func (e *DiffEngine) compareFunctions() types.DiffList { |
| 734 | + var differences types.DiffList |
| 735 | + |
| 736 | + sourceFuncs := make(map[string]*types.Function) |
| 737 | + targetFuncs := make(map[string]*types.Function) |
| 738 | + |
| 739 | + for i := range e.source.Functions { |
| 740 | + f := &e.source.Functions[i] |
| 741 | + key := fmt.Sprintf("%s(%s)", f.Name, f.Arguments) |
| 742 | + sourceFuncs[key] = f |
| 743 | + } |
| 744 | + for i := range e.target.Functions { |
| 745 | + f := &e.target.Functions[i] |
| 746 | + key := fmt.Sprintf("%s(%s)", f.Name, f.Arguments) |
| 747 | + targetFuncs[key] = f |
| 748 | + } |
| 749 | + |
| 750 | + // New and altered functions |
| 751 | + for key, sourceFunc := range sourceFuncs { |
| 752 | + if targetFunc, exists := targetFuncs[key]; !exists { |
| 753 | + differences = append(differences, types.Diff{ |
| 754 | + Type: types.DiffAdd, |
| 755 | + Object: types.ObjectFunction, |
| 756 | + Name: key, // key includes func_name(args) |
| 757 | + NewValue: sourceFunc.Definition, // complete CREATE OR REPLACE snippet |
| 758 | + }) |
| 759 | + } else if sourceFunc.Definition != targetFunc.Definition { |
| 760 | + // Definition changed |
| 761 | + differences = append(differences, types.Diff{ |
| 762 | + Type: types.DiffAlter, |
| 763 | + Object: types.ObjectFunction, |
| 764 | + Name: key, |
| 765 | + OldValue: targetFunc.Definition, |
| 766 | + NewValue: sourceFunc.Definition, |
| 767 | + }) |
| 768 | + } |
| 769 | + } |
| 770 | + |
| 771 | + // Dropped functions |
| 772 | + for key := range targetFuncs { |
| 773 | + if _, exists := sourceFuncs[key]; !exists { |
| 774 | + differences = append(differences, types.Diff{ |
| 775 | + Type: types.DiffDrop, |
| 776 | + Object: types.ObjectFunction, |
| 777 | + Name: key, |
| 778 | + }) |
| 779 | + } |
| 780 | + } |
| 781 | + |
| 782 | + return differences |
| 783 | +} |
0 commit comments