Skip to content

Commit

Permalink
add new command database-changes
Browse files Browse the repository at this point in the history
- in generate show overview of all changes in database procedures
  • Loading branch information
ZelvaMan committed May 22, 2024
1 parent b494a0c commit 6464ad6
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 19 deletions.
77 changes: 77 additions & 0 deletions cmd/databaseChanges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package cmd

import (
"fmt"
"github.com/keenmate/db-gen/private/dbGen"
"github.com/keenmate/db-gen/private/helpers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
)

var databaseChangesFlags = []helpers.FlagArgument{
helpers.NewBoolFlag(keyUseRoutinesFile, "", false, "Use routines file to databaseChanges code"),
}

var databaseChangesCmd = &cobra.Command{
Use: "database-changes",
Short: "show changes made to database from last generation",
Long: `
prints differences between routines loaded from database/routines file
and routines used during generation
`,
Run: func(cmd *cobra.Command, args []string) {
helpers.BindFlags(cmd, append(commonFlags, databaseChangesFlags...))
_, err := dbGen.ReadConfig(viper.GetString(keyConfig))
if err != nil {
helpers.Exit("configuration error: %s", err)
}

viper.AutomaticEnv() // read in environment variables that match

err = doDatabaseChanges()
if err != nil {
helpers.Exit(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(databaseChangesCmd)

helpers.DefineFlags(databaseChangesCmd, append(commonFlags, databaseChangesFlags...))
}

func doDatabaseChanges() error {
config, err := dbGen.GetAndValidateConfig()
if err != nil {
return fmt.Errorf("error getting config %s", err)
}

helpers.LogDebug("Debug logging is enabled")

// TODO it will be ideal to load build information before loading and validating config
buildInfo, infoExist := dbGen.LoadGenerationInformation(config)

if !infoExist {
return fmt.Errorf("no generation information found")
}

if !buildInfo.CheckVersion() {
return nil
}

var routines []dbGen.DbRoutine

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

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

return nil
}
28 changes: 27 additions & 1 deletion cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/keenmate/db-gen/private/dbGen"
"github.com/keenmate/db-gen/private/helpers"
"github.com/keenmate/db-gen/private/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand Down Expand Up @@ -60,6 +61,18 @@ func doGenerate() error {
helpers.LogDebug("Debug logging is enabled")
timer.AddEntry("getting config")

// TODO it will be ideal to load build information before loading and validating config
buildInfo, infoExist := dbGen.LoadGenerationInformation(config)
timer.AddEntry("loading build information")

if infoExist {
log.Printf("Build information loaded, last build was at %s", buildInfo.Time.String())

if !buildInfo.CheckVersion() {
return nil
}
}

var routines []dbGen.DbRoutine

log.Printf("Getting routines...")
Expand All @@ -77,7 +90,14 @@ func doGenerate() error {
return fmt.Errorf("error saving debug file: %s", err)
}
timer.AddEntry("saving debug file")
}

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

log.Printf("Preprocessing...")
Expand All @@ -103,8 +123,14 @@ func doGenerate() error {
if err != nil {
return fmt.Errorf("error generating: %s", err)
}

timer.AddEntry("generating files")

err = dbGen.SaveGenerationInformation(config, routines, version.GetVersion())
if err != nil {
log.Printf("Error saving generation information: %v", err)
}

timer.AddEntry("saving generation info")
timer.Finish()
log.Printf(timer.String())
return nil
Expand Down
25 changes: 13 additions & 12 deletions cmd/routines.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package cmd

import (
"fmt"
dbGen2 "github.com/keenmate/db-gen/private/dbGen"
common2 "github.com/keenmate/db-gen/private/helpers"
"github.com/keenmate/db-gen/private/dbGen"
"github.com/keenmate/db-gen/private/helpers"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
Expand All @@ -14,13 +14,13 @@ var getRoutinesCmd = &cobra.Command{
Short: "Get routines",
Long: "Get routines from database and save them to file to generate later",
Run: func(cmd *cobra.Command, args []string) {
common2.BindFlags(cmd, commonFlags)
helpers.BindFlags(cmd, commonFlags)

configLocation := viper.GetString("config")

_, err := dbGen2.ReadConfig(configLocation)
_, err := dbGen.ReadConfig(configLocation)
if err != nil {
common2.Exit("configuration error: %s", err)
helpers.Exit("configuration error: %s", err)
}

log.Printf("arguments: %s", args)
Expand All @@ -34,31 +34,32 @@ var getRoutinesCmd = &cobra.Command{
err = doGetRoutines()

if err != nil {
common2.Exit(err.Error())
helpers.Exit(err.Error())
}
},
}

func init() {
rootCmd.AddCommand(getRoutinesCmd)

common2.DefineFlags(getRoutinesCmd, commonFlags)
helpers.DefineFlags(getRoutinesCmd, commonFlags)
}

func doGetRoutines() error {
log.Printf("Getting configurations...")

config, err := dbGen2.GetAndValidateConfig()
config, err := dbGen.GetAndValidateConfig()
if err != nil {
return fmt.Errorf("error getting config %s", err)
}

common2.LogDebug("Debug logging is enabled")
helpers.LogDebug("Debug logging is enabled")

// because we use shared config, we need to set this
// because we use shared config, we need to set this to force loading from database
config.UseRoutinesFile = false

log.Printf("Getting routines...")
routines, err := dbGen2.GetRoutines(config)
routines, err := dbGen.GetRoutines(config)
if err != nil {
return fmt.Errorf("error getting routines: %s", err)
}
Expand All @@ -67,7 +68,7 @@ func doGetRoutines() error {

// TODO show what routines changed

err = common2.SaveAsJson(config.RoutinesFile, routines)
err = dbGen.SaveRoutinesFile(routines, config)
if err != nil {
return fmt.Errorf("error saving routines: %s", err)
}
Expand Down
2 changes: 2 additions & 0 deletions private/dbGen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ func GetAndValidateConfig() (*Config, error) {
config.ProcessorTemplate = joinIfRelative(config.PathBase, config.ProcessorTemplate)
config.DbContextTemplate = joinIfRelative(config.PathBase, config.DbContextTemplate)
config.ModelTemplate = joinIfRelative(config.PathBase, config.ModelTemplate)

config.OutputFolder = joinIfRelative(config.PathBase, config.OutputFolder)
// TODO maybe it is better to be relative to Output folder, not Base path
config.RoutinesFile = joinIfRelative(config.PathBase, config.RoutinesFile)

config.GeneratedFileCase = strings.ToLower(config.GeneratedFileCase)
Expand Down
Loading

0 comments on commit 6464ad6

Please sign in to comment.