Skip to content

Commit

Permalink
change detection improvements and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ZelvaMan committed Jun 22, 2024
1 parent 353e90a commit 015dcf3
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 23 deletions.
9 changes: 9 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ var commonFlags = []helpers.FlagArgument{
helpers.NewStringFlag(keyConfig, "s", "", "Connection string used to connect to database"),
helpers.NewStringFlag(keyConnectionString, "c", "", "Path to configuration file"),
}

func printDatabaseChanges(databaseChanges string) {
if len(databaseChanges) == 0 {
helpers.LogBold("No database changes detected")
return
}

helpers.LogBold("Database changes:\n" + databaseChanges)
}
13 changes: 9 additions & 4 deletions cmd/databaseChanges.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,22 @@ func doDatabaseChanges() error {
return nil
}

var routines []dbGen.DbRoutine

log.Printf("Getting routines...")
routines, err = dbGen.GetRoutines(config)
routines, err := dbGen.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
log.Printf("Got %d routines", len(routines))

err = dbGen.PreprocessRoutines(&routines, config)
if err != nil {
return fmt.Errorf("error preprocessing routines: %s", err)
}

log.Printf("Routines preprocessed")

databaseChanges := buildInfo.GetRoutinesChanges(routines)
log.Printf("Database changes:\n" + databaseChanges)
printDatabaseChanges(databaseChanges)

return nil
}
15 changes: 11 additions & 4 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,22 @@ func doGenerate() error {
timer.AddEntry("saving debug file")
}

// mark function as overloads
log.Printf("Preprocessing...")
err = dbGen.PreprocessRoutines(&routines, config)
if err != nil {
return fmt.Errorf("error preprocessing: %s", err)
}

if infoExist {
// TODO we should only show changes of routines after filtering
changesMsg := buildInfo.GetRoutinesChanges(routines)
log.Printf("Database changes:\n" + changesMsg)
// TODO maybe only handle changes after filtering
changes := buildInfo.GetRoutinesChanges(routines)
printDatabaseChanges(changes)
} else {
log.Printf("No previous build information found")
}

log.Printf("Preprocessing...")
log.Printf("Processing...")
processedFunctions, err := dbGen.Process(routines, config)
if err != nil {
return fmt.Errorf("error preprocessing: %s", err)
Expand Down
38 changes: 25 additions & 13 deletions private/dbGen/generationInformation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ type GenerationInformation struct {
}

type databaseChanges struct {
deletedRoutines []DbRoutine
createdRoutines []DbRoutine
changedRoutines []routineChanges
deletedRoutines []DbRoutine
createdRoutines []DbRoutine
maybeChangedRoutines []routinePain
}

type routineChanges struct {
type routinePain struct {
oldRoutine DbRoutine
newRoutine DbRoutine
}
Expand Down Expand Up @@ -108,11 +108,11 @@ func (info *GenerationInformation) GetRoutinesChanges(newRoutines []DbRoutine) s
for _, newRoutine := range newRoutines {
oldRoutine, exists := findRoutine(oldRoutines, newRoutine)
if !exists {
changes.createdRoutines = append(changes.deletedRoutines, newRoutine)
changes.createdRoutines = append(changes.createdRoutines, newRoutine)
continue
}

changes.changedRoutines = append(changes.changedRoutines, routineChanges{
changes.maybeChangedRoutines = append(changes.maybeChangedRoutines, routinePain{
oldRoutine: *oldRoutine,
newRoutine: newRoutine,
})
Expand All @@ -127,28 +127,35 @@ func (databaseChanges *databaseChanges) String() string {
if len(databaseChanges.deletedRoutines) > 0 {
out.WriteString("Deleted routines:\n")
for _, routine := range databaseChanges.deletedRoutines {
out.WriteString(fmt.Sprintf(" - %s.%s\n", routine.RoutineSchema, routine.RoutineName))
out.WriteString(fmt.Sprintf(" - %s.%s\n", routine.RoutineSchema, routine.RoutineNameWithParams))
}
}

if len(databaseChanges.createdRoutines) > 0 {
out.WriteString("Created routines:\n")
for _, routine := range databaseChanges.createdRoutines {
out.WriteString(fmt.Sprintf(" - %s.%s\n", routine.RoutineSchema, routine.RoutineName))
out.WriteString(fmt.Sprintf(" - %s.%s\n", routine.RoutineSchema, routine.RoutineNameWithParams))
}
}

if len(databaseChanges.changedRoutines) > 0 {
out.WriteString("Changed routines:\n")
for _, changes := range databaseChanges.changedRoutines {
out.WriteString(changes.String())
log.Printf("number of changed routines: %d\n", len(databaseChanges.maybeChangedRoutines))
if len(databaseChanges.maybeChangedRoutines) > 0 {
changesDetected := false
for _, routinesInfo := range databaseChanges.maybeChangedRoutines {
changes := routinesInfo.String()
if !changesDetected && len(changes) > 0 {
out.WriteString("Changed routines:\n")
changesDetected = true
}
out.WriteString(changes)

}
}

return out.String()
}

func (change *routineChanges) String() string {
func (change *routinePain) String() string {
var changes strings.Builder

oldR := change.oldRoutine
Expand Down Expand Up @@ -232,6 +239,11 @@ func getParameterChanges(oldParams []DbParameter, newParams []DbParameter) strin

func findRoutine(routines []DbRoutine, routine DbRoutine) (*DbRoutine, bool) {
index := slices.IndexFunc(routines, func(oldRoutine DbRoutine) bool {
// for function with overloads we can't detect parameter changes
if routine.HasOverload {
return routine.RoutineNameWithParams == oldRoutine.RoutineNameWithParams
}

return routine.RoutineName == oldRoutine.RoutineName && routine.RoutineSchema == oldRoutine.RoutineSchema
})

Expand Down
2 changes: 0 additions & 2 deletions private/dbGen/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ func Process(routines []DbRoutine, config *Config) ([]Routine, error) {
return nil, fmt.Errorf("filtering routines: %s", err)
}

err = PreprocessRoutines(&routines, config)

// don't need to compute for every property
typeMappings := getTypeMappings(config)
helpers.LogDebug("Got %d type mappings", len(typeMappings))
Expand Down
5 changes: 5 additions & 0 deletions private/helpers/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@ const colorReset = "\033[0m"
const colorBlue = "\033[34m"
const colorRed = "\033[31m"
const colorYellow = "\033[33m"
const colorBold = "\033[1m"

func Log(msg string, args ...any) {
log.Printf(msg, args...)
}

func LogBold(msg string, args ...any) {
log.Printf(colorBold+msg+colorReset, args...)
}

func LogError(msg string, args ...any) {
log.Printf(colorRed+msg+colorReset, args...)
}
Expand Down

0 comments on commit 015dcf3

Please sign in to comment.