From 0dc457ec9c84857634ecffbc19ba63220bd5fc01 Mon Sep 17 00:00:00 2001 From: Marc Cataford Date: Thu, 11 Jul 2024 21:15:37 -0400 Subject: [PATCH] feat: add inspect by index command --- cobble.go | 17 +++++++++++++++++ migration.go | 28 +++++++++++++++++++++++----- migrate.go => parser.go | 27 ++++++++++++++++++++++----- 3 files changed, 62 insertions(+), 10 deletions(-) rename migrate.go => parser.go (70%) diff --git a/cobble.go b/cobble.go index cc61d56..7076ef0 100644 --- a/cobble.go +++ b/cobble.go @@ -2,6 +2,7 @@ package main import ( "context" + "fmt" "github.com/spf13/cobra" "log" "os" @@ -45,8 +46,24 @@ var up = &cobra.Command{ }, } +var inspect = &cobra.Command{ + Use: "inspect", + Short: "Prints the nth migration in the history", + Run: func(cmd *cobra.Command, args []string) { + migrationRoot := args[0] + migrationIndex, _ := strconv.Atoi(args[1]) + + migrationGraph, _ := NewMigrationGraphFromDirectory(migrationRoot) + migrationHistory, _ := migrationGraph.GetLinearHistory() + migration := migrationHistory[migrationIndex] + sql, _ := migration.Sql() + fmt.Printf("%s:\n%s", migration.Name, sql) + }, +} + func main() { cli.AddCommand(up) + cli.AddCommand(inspect) if err := cli.Execute(); err != nil { log.Fatal(err) diff --git a/migration.go b/migration.go index 952ea84..4be086f 100644 --- a/migration.go +++ b/migration.go @@ -21,20 +21,38 @@ func NewMigration(path string, name string, requires string) Migration { } } +func (m *Migration) Bytes() ([]byte, error) { + migrationPath := m.Path + migrationBytes, err := os.ReadFile(migrationPath) + + if err != nil { + return []byte{}, err + } + + return migrationBytes, nil +} + +func (m *Migration) Text() (string, error) { + migrationBytes, err := m.Bytes() + return string(migrationBytes), err +} + +func (m *Migration) Sql() (string, error) { + migrationBytes, err := m.Bytes() + + return string(StripComments(migrationBytes)), err +} + // Applies a migration to the given database connection. // // If an error is returned while trying to read the migration file // or execute the SQL it contains, the error is returned. func (m *Migration) ApplyMigration(db DB) error { - migrationPath := m.Path - migrationBytes, err := os.ReadFile(migrationPath) + migrationSql, err := m.Sql() if err != nil { return err } - - migrationSql := string(migrationBytes) - log.Printf("SQL: %s", migrationSql) err = db.Execute(migrationSql) diff --git a/migrate.go b/parser.go similarity index 70% rename from migrate.go rename to parser.go index 69a5b9d..d445874 100644 --- a/migrate.go +++ b/parser.go @@ -1,8 +1,3 @@ -// Migration tooling -// -// Runs migrations in-order based on files found in the migration -// root directory provided as argument. - package main import ( @@ -24,6 +19,28 @@ func cutPrefixAndTrim(b []byte, prefix []byte) []byte { return bytes.TrimSpace(withoutPrefix) } +func StripComments(sql []byte) []byte { + scanner := bufio.NewScanner(bytes.NewReader(sql)) + + sqlWithoutComments := []byte{} + + for scanner.Scan() { + currentBytes := scanner.Bytes() + + if !bytes.HasPrefix(currentBytes, commentPrefix) { + sqlWithoutComments = append(sqlWithoutComments, currentBytes...) + sqlWithoutComments = append(sqlWithoutComments, []byte("\n")...) + } + } + + totalLength := len(sqlWithoutComments) + + if totalLength > 0 { + return sqlWithoutComments[:totalLength-1] + } + return sqlWithoutComments +} + // Extracts any migration headers present in the migration source file. // Headers are left in comments of the form: // -- : ...