Skip to content

Commit

Permalink
refactor: improve error message and code organization in amigo package
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Oct 31, 2024
1 parent 818266e commit 454dcf5
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 3 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package cmd

import (
"fmt"
"os"
"path/filepath"

"github.com/alexisvisco/amigo/pkg/amigo"
"github.com/alexisvisco/amigo/pkg/amigoctx"
"github.com/alexisvisco/amigo/pkg/utils/events"
"github.com/alexisvisco/amigo/pkg/utils/logger"
"github.com/spf13/viper"
"os"
"path/filepath"

"github.com/spf13/cobra"
)
Expand Down
6 changes: 5 additions & 1 deletion pkg/amigo/setup_slog.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package amigo

import (
"github.com/alexisvisco/amigo/pkg/utils/logger"
"io"
"log/slog"

"github.com/alexisvisco/amigo/pkg/utils/logger"
)

func (a Amigo) SetupSlog(writer io.Writer) {
logger.ShowSQLEvents = a.ctx.ShowSQL
if writer == nil {
return
}

if a.ctx.JSON {
slog.SetDefault(slog.New(slog.NewJSONHandler(writer, nil)))
Expand Down
8 changes: 6 additions & 2 deletions pkg/schema/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package schema
import (
"context"
"fmt"
"reflect"
"regexp"
"slices"
"time"
Expand Down Expand Up @@ -113,8 +114,11 @@ func (m *Migrator[T]) Apply(direction types.MigrationDirection, version *string,
case SimpleMigration[T]:
migrationFunc = t.Change
default:
logger.Error(events.MessageEvent{Message: fmt.Sprintf("Migration %s is not a valid migration",
migration.Name())})
reflectionType := fmt.Sprintf("%T", migration)
driverValue := fmt.Sprintf("%v", reflect.TypeOf(new(T)).Elem())

logger.Error(events.MessageEvent{Message: fmt.Sprintf("Migration %s is not a valid migration type, found %s (driver is %s). Did you set DSN correctly ?",
migration.Name(), reflectionType, driverValue)})
return false
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/schema/sql_migration_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type SQLMigration[T Schema] struct {
delimiter string
}

func NewSQLMigration[T Schema](fs embed.FS, name string, time string, delimiter string) SQLMigration[T] {
return SQLMigration[T]{fs: fs, name: name, time: time, delimiter: delimiter}
func NewSQLMigration[T Schema](fs embed.FS, name string, time string, delimiter string) DetailedMigration[T] {
return &SQLMigration[T]{fs: fs, name: name, time: time, delimiter: delimiter}
}

func (s SQLMigration[T]) Up(x T) {
Expand Down

0 comments on commit 454dcf5

Please sign in to comment.