Skip to content

Commit a0f5225

Browse files
committed
feat: Add better changes summary helper
1 parent eeed6a6 commit a0f5225

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

schema/table.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import (
77
"fmt"
88
"regexp"
99
"slices"
10+
"strings"
1011

1112
"github.com/apache/arrow-go/v18/arrow"
1213
"github.com/cloudquery/plugin-sdk/v4/glob"
14+
"github.com/samber/lo"
1315
"github.com/thoas/go-funk"
1416
)
1517

@@ -258,6 +260,98 @@ func (t TableColumnChange) String() string {
258260
}
259261
}
260262

263+
func getColumnChangeSummary(change TableColumnChange) string {
264+
switch change.Type {
265+
case TableColumnChangeTypeAdd:
266+
if change.Current.PrimaryKey {
267+
return fmt.Sprintf("Column %q added with type %q and primary key constraint", change.ColumnName, change.Current.Type)
268+
}
269+
if change.Current.Unique {
270+
return fmt.Sprintf("Column %q added with type %q and unique constraint", change.ColumnName, change.Current.Type)
271+
}
272+
if change.Current.NotNull {
273+
return fmt.Sprintf("Column %q added with type %q and not null constraint", change.ColumnName, change.Current.Type)
274+
}
275+
if change.Current.IncrementalKey {
276+
return fmt.Sprintf("Column %q added with type %q and as an incremental key", change.ColumnName, change.Current.Type)
277+
}
278+
return fmt.Sprintf("Column %q added with type %q", change.ColumnName, change.Current.Type)
279+
case TableColumnChangeTypeUpdate:
280+
if change.Previous.Type != change.Current.Type {
281+
return fmt.Sprintf("Type changed from %q to %q for column %q", change.Previous.Type, change.Current.Type, change.ColumnName)
282+
}
283+
if !change.Previous.PrimaryKey && change.Current.PrimaryKey {
284+
return fmt.Sprintf("Primary key constraint added to column %q", change.ColumnName)
285+
}
286+
if change.Previous.PrimaryKey && !change.Current.PrimaryKey {
287+
return fmt.Sprintf("Primary key constraint removed from column %q", change.ColumnName)
288+
}
289+
if !change.Previous.Unique && change.Current.Unique {
290+
return fmt.Sprintf("Unique constraint added to column %q", change.ColumnName)
291+
}
292+
if change.Previous.Unique && !change.Current.Unique {
293+
return fmt.Sprintf("Unique constraint removed from column %q", change.ColumnName)
294+
}
295+
if !change.Previous.NotNull && change.Current.NotNull {
296+
return fmt.Sprintf("Not null constraint added to column %q", change.ColumnName)
297+
}
298+
if change.Previous.NotNull && !change.Current.NotNull {
299+
return fmt.Sprintf("Not null constraint removed from column %q", change.ColumnName)
300+
}
301+
if !change.Previous.IncrementalKey && change.Current.IncrementalKey {
302+
return fmt.Sprintf("Column %q added as an incremental key", change.ColumnName)
303+
}
304+
if change.Previous.IncrementalKey && !change.Current.IncrementalKey {
305+
return fmt.Sprintf("Column %q removed as an incremental key", change.ColumnName)
306+
}
307+
if !change.Previous.PrimaryKeyComponent && change.Current.PrimaryKeyComponent {
308+
return fmt.Sprintf("Primary key component condition added to column %q", change.ColumnName)
309+
}
310+
if change.Previous.PrimaryKeyComponent && !change.Current.PrimaryKeyComponent {
311+
return fmt.Sprintf("Primary key component condition removed from column %q", change.ColumnName)
312+
}
313+
return fmt.Sprintf("Column %q updated. Previous: %s, Current: %s", change.ColumnName, change.Previous, change.Current)
314+
case TableColumnChangeTypeRemove:
315+
if change.Previous.PrimaryKey {
316+
return fmt.Sprintf("Primary key column %q removed", change.ColumnName)
317+
}
318+
if change.Previous.Unique {
319+
return fmt.Sprintf("Unique column %q removed", change.ColumnName)
320+
}
321+
if change.Previous.NotNull {
322+
return fmt.Sprintf("Not null column %q removed", change.ColumnName)
323+
}
324+
if change.Previous.IncrementalKey {
325+
return fmt.Sprintf("Incremental key column %q removed", change.ColumnName)
326+
}
327+
return fmt.Sprintf("Column %q with type %q removed", change.ColumnName, change.Previous.Type)
328+
case TableColumnChangeTypeRemoveUniqueConstraint:
329+
return fmt.Sprintf("Unique constraint removed from column %q", change.ColumnName)
330+
case TableColumnChangeTypeMoveToCQOnly:
331+
return fmt.Sprintf("Primary key columns removed and replaced with a single column %q with type %q", change.ColumnName, change.Current.Type)
332+
default:
333+
return fmt.Sprintf("column: %s, type: %s, current: %s, previous: %s", change.ColumnName, change.Type, change.Current, change.Previous)
334+
}
335+
}
336+
337+
func GetChangesSummary(tablesChanges map[string][]TableColumnChange) string {
338+
tables := lo.Keys(tablesChanges)
339+
slices.Sort(tables)
340+
summary := strings.Builder{}
341+
for _, table := range tables {
342+
summary.WriteString(fmt.Sprintf("%s:\n", table))
343+
changes := tablesChanges[table]
344+
changesString := lo.Map(changes, func(change TableColumnChange, _ int) string {
345+
return fmt.Sprintf(" - %s", getColumnChangeSummary(change))
346+
})
347+
slices.Sort(changesString)
348+
summary.WriteString(strings.Join(changesString, "\n"))
349+
summary.WriteString("\n")
350+
}
351+
352+
return summary.String()
353+
}
354+
261355
func (tt Tables) FilterDfsFunc(include, exclude func(*Table) bool, skipDependentTables bool) Tables {
262356
filteredTables := make(Tables, 0, len(tt))
263357
for _, t := range tt {

0 commit comments

Comments
 (0)