Skip to content

Commit

Permalink
integrate squash all into CLI (#1623)
Browse files Browse the repository at this point in the history
  • Loading branch information
lolopinto authored Aug 24, 2023
1 parent 63dc2df commit 1fff89a
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 8 deletions.
7 changes: 6 additions & 1 deletion internal/db/db_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,16 @@ func DowngradeDB(cfg *codegen.Config, revision string, keepSchemaFiles bool) err
return auto_schema.RunPythonCommand(cfg, extraArgs...)
}

func Squash(cfg *codegen.Config, squash int) error {
func SquashN(cfg *codegen.Config, squash int) error {
extraArgs := []string{fmt.Sprintf("--squash=%d", squash)}
return auto_schema.RunPythonCommand(cfg, extraArgs...)
}

func SquashAll(cfg *codegen.Config, message string) error {
extraArgs := []string{"--squash=all", fmt.Sprintf("--message=%s", message)}
return auto_schema.RunPythonCommand(cfg, extraArgs...)
}

func FixEdges(cfg *codegen.Config) error {
return auto_schema.RunPythonCommand(cfg, "-f=True")
}
Expand Down
2 changes: 1 addition & 1 deletion internal/graphql/generate_ts_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ func searchForFiles(processor *codegen.Processor) []string {
return nil
}
if strings.Contains(err.Error(), "executable file not found") {
fmt.Print("\u001b[31mrg executable not found so can't search for custom files. local development error?\u001b[0m\n")
fmt.Printf("%s\n", util.WrapRed("rg executable not found so can't search for custom files. local development error?"))
return nil
}
// this could be because no files exist at all e.g. when running codegen first time...
Expand Down
71 changes: 71 additions & 0 deletions internal/util/terminal_colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package util

import "fmt"

type Color string

const (
// colors from https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html#8-colors
Black Color = "\033[030m"
Red Color = "\033[31m"
Green Color = "\033[32m"
Yellow Color = "\033[33m"
Blue Color = "\033[34m"
Magenta Color = "\033[35m"
Cyan Color = "\033[36m"
White Color = "\033[37m"
Reset Color = "\033[0m"

BrightBlack Color = "\033[30;1m"
BrightRed Color = "\033[31;1m"
BrightGreen Color = "\033[32;1m"
BrightYellow Color = "\033[33;1m"
BrightBlue Color = "\033[34;1m"
BrightMagenta Color = "\033[35;1m"
BrightCyan Color = "\033[36;1m"
BrightWhite Color = "\033[37;1m"
)

func wrap(color Color, text string) string {
return fmt.Sprintf("%s%s%s", color, text, Reset)
}

func WrapRed(text string) string {
return wrap(Red, text)
}

func WrapGreen(text string) string {
return wrap(Green, text)
}

func WrapYellow(text string) string {
return wrap(Yellow, text)
}

func WrapBlue(text string) string {
return wrap(Blue, text)
}

func WrapMagenta(text string) string {
return wrap(Magenta, text)
}

func WrapCyan(text string) string {
return wrap(Cyan, text)
}

func WrapWhite(text string) string {
return wrap(White, text)
}

func WrapBlack(text string) string {
return wrap(Black, text)
}

func WrapBrightRed(text string) string {
return wrap(BrightRed, text)
}

func WrapBrightGreen(text string) string {
return wrap(BrightGreen, text)
}
5 changes: 4 additions & 1 deletion tsent/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"
"time"

"github.com/lolopinto/ent/internal/util"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -121,11 +122,13 @@ func init() {
migratev1Cmd.Flags().StringVar(&migrateV1Info.transformPath, "transform_path", "", "path for new base class")

migrationCmd.Flags().StringVar(&migrateInfo.message, "message", "", "message for migration")

squashCmd.Flags().StringVar(&squashInfo.message, "message", "", "message for new file when squash all is run")
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Printf("\u001b[31mError:\u001b[0m \n %s\n", err.Error())
fmt.Printf("%s \n %s\n", util.WrapRed("Error"), err.Error())
fmt.Println(rootCmd.UsageString())
os.Exit(1)
}
Expand Down
37 changes: 32 additions & 5 deletions tsent/cmd/squash.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,53 @@ import (

"github.com/lolopinto/ent/internal/codegen"
"github.com/lolopinto/ent/internal/db"
"github.com/lolopinto/ent/internal/prompt"
"github.com/lolopinto/ent/internal/util"
"github.com/spf13/cobra"
)

type squashArgs struct {
message string
}

var squashInfo squashArgs

var squashCmd = &cobra.Command{
Use: "squash",
Short: "squash last N revs of the db into one",
Example: `tsent squash 2`,
// also needs a fancy --all which requires a prompt
Args: cobra.ExactArgs(1),
Short: "squash last N revs of the db into one or squash all revs into one to clean up migration files",
Example: `tsent squash 2\n tsent squash all --message "squash all"`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// another hardcoded place
cfg, err := codegen.NewConfig("src/schema", "")
if err != nil {
return err
}

if args[0] == "all" {

p := &prompt.YesNoQuestion{
// idk what the best color is here but we don't do it too often so it's fine for now...
Question: util.WrapBrightGreen(
"Note that you'll be left with just one migration file if you do this and won't be able to downgrade anymore.\n" +
"You should only do this if the current development environment matches production AND you don't have branches in your migration history.\n" +
"If you're not sure about branches, you should run `docker-compose -f docker-compose.dev.yml run --rm app tsent alembic heads` or `tsent alembic heads` to confirm.\n" +
"Are you sure you want to squash all migrations into one? Y/N:"),
NoHandler: prompt.ExitHandler,
}

if err := prompt.HandlePrompts([]prompt.Prompt{p}); err != nil {
return err
}

return db.SquashAll(cfg, squashInfo.message)
}

i, err := strconv.Atoi(args[0])
if err != nil {
return err
}
return db.Squash(cfg, i)

return db.SquashN(cfg, i)
},
}

0 comments on commit 1fff89a

Please sign in to comment.