diff --git a/applications/generate.go b/applications/generate.go index 37a2320..fcded52 100644 --- a/applications/generate.go +++ b/applications/generate.go @@ -1,6 +1,8 @@ package applications import ( + "path" + "github.com/migratemgr8/mgr8/domain" "github.com/migratemgr8/mgr8/global" "github.com/migratemgr8/mgr8/infrastructure" @@ -20,15 +22,17 @@ type generateCommand struct { driver domain.Driver fService infrastructure.FileService migrationFService MigrationFileService + logService infrastructure.LogService } -func NewGenerateCommand(driver domain.Driver, migrationFService MigrationFileService, fService infrastructure.FileService) *generateCommand { - return &generateCommand{driver: driver, migrationFService: migrationFService, fService: fService} +func NewGenerateCommand(driver domain.Driver, migrationFService MigrationFileService, fService infrastructure.FileService, logService infrastructure.LogService) *generateCommand { + return &generateCommand{driver: driver, migrationFService: migrationFService, fService: fService, logService: logService} } func (g *generateCommand) Execute(parameters *GenerateParameters) error { newSchemaContent, err := g.fService.Read(parameters.NewSchemaPath) if err != nil { + g.logService.Critical("Could not read from", parameters.NewSchemaPath) return err } @@ -50,6 +54,7 @@ func (g *generateCommand) Execute(parameters *GenerateParameters) error { if err != nil { return err } + g.logService.Debug("Latest migration found:", nextMigration) err = g.migrationFService.WriteStatementsToFile(parameters.MigrationDir, upStatements, nextMigration, "up") if err != nil { @@ -61,5 +66,6 @@ func (g *generateCommand) Execute(parameters *GenerateParameters) error { return err } + g.logService.Debug("Updating reference file at", path.Join(global.ApplicationFolder, global.ReferenceFile)) return g.fService.Write(global.ApplicationFolder, global.ReferenceFile, newSchemaContent) } diff --git a/applications/generate_test.go b/applications/generate_test.go index 10548e7..c7fa69a 100644 --- a/applications/generate_test.go +++ b/applications/generate_test.go @@ -29,7 +29,8 @@ var _ = Describe("Generate Command", func() { driver = domain_mock.NewMockDriver(ctrl) migrationFileServiceMock = applications_mock.NewMockMigrationFileService(ctrl) fileService = infrastructure_mock.NewMockFileService(ctrl) - subject = NewGenerateCommand(driver, migrationFileServiceMock, fileService) + loggerMock := anyLog(ctrl) + subject = NewGenerateCommand(driver, migrationFileServiceMock, fileService, loggerMock) deparser = domain_mock.NewMockDeparser(ctrl) }) diff --git a/applications/migrationscripts.go b/applications/migrationscripts.go index 987e077..40da7dc 100644 --- a/applications/migrationscripts.go +++ b/applications/migrationscripts.go @@ -2,6 +2,7 @@ package applications import ( "fmt" + "github.com/migratemgr8/mgr8/domain" "github.com/migratemgr8/mgr8/infrastructure" ) @@ -46,6 +47,7 @@ func (m *migrationFileService) GetNextMigrationNumber(dir string) (int, error) { func (g *migrationFileService) GetSchemaFromFile(filename string) (*domain.Schema, error) { content, err := g.fileService.Read(filename) if err != nil { + g.logService.Critical("Could not read from", filename) return nil, err } @@ -54,7 +56,7 @@ func (g *migrationFileService) GetSchemaFromFile(filename string) (*domain.Schem func (g *migrationFileService) WriteStatementsToFile(migrationDir string, statements []string, migrationNumber int, migrationType string) error { filename := g.fileNameFormatter.FormatFilename(migrationNumber, migrationType) - g.logService.Info("Generating file %s migration %s", migrationType, filename) + g.logService.Info("Generating file for", migrationType, "migration:", filename) content := g.driver.Deparser().WriteScript(statements) return g.fileService.Write(migrationDir, filename, content) } diff --git a/applications/migrationscripts_test.go b/applications/migrationscripts_test.go index 05ae2cc..0a33cc4 100644 --- a/applications/migrationscripts_test.go +++ b/applications/migrationscripts_test.go @@ -134,7 +134,7 @@ var _ = Describe("Migration Scripts", func() { clock := infrastructure_mock.NewMockClock(ctrl) fileService = infrastructure_mock.NewMockFileService(ctrl) driver = domain_mock.NewMockDriver(ctrl) - subject = NewMigrationFileService(fileService, NewFileNameFormatter(clock), driver, applications_mock.NewMockLogService(ctrl)) + subject = NewMigrationFileService(fileService, NewFileNameFormatter(clock), driver, anyLog(ctrl)) }) When("Reads file successfully", func() { It("Generates expected filename", func() { @@ -158,7 +158,7 @@ var _ = Describe("Migration Scripts", func() { }) -func anyLog(ctrl *gomock.Controller) *applications_mock.MockLogService{ +func anyLog(ctrl *gomock.Controller) *applications_mock.MockLogService { mockLogService := applications_mock.NewMockLogService(ctrl) mockLogService.EXPECT().Info(gomock.Any()).AnyTimes() mockLogService.EXPECT().Critical(gomock.Any()).AnyTimes() diff --git a/cmd/diff.go b/cmd/diff.go index bdadf6c..2316f19 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -2,7 +2,6 @@ package cmd import ( "fmt" - "log" "github.com/migratemgr8/mgr8/applications" "github.com/migratemgr8/mgr8/domain" @@ -25,6 +24,7 @@ func (g *diff) execute(args []string, databaseURL string, migrationsDir string, driver, applications.NewMigrationFileService(fileService, applications.NewFileNameFormatter(clock), driver, logService), fileService, + logService, ) err = generateCommand.Execute(&applications.GenerateParameters{ @@ -33,7 +33,7 @@ func (g *diff) execute(args []string, databaseURL string, migrationsDir string, MigrationDir: migrationsDir, }) if err != nil { - log.Print(err) + logService.Critical("Failed generating diff migration") } - return err + return nil } diff --git a/infrastructure/logger.go b/infrastructure/logger.go index 7d0f914..5afdc45 100644 --- a/infrastructure/logger.go +++ b/infrastructure/logger.go @@ -17,12 +17,12 @@ type LogService interface { type LogLevel string const ( - InfoLogLevel = "info" - DebugLogLevel = "debug" + InfoLogLevel = "info" + DebugLogLevel = "debug" CriticalLogLevel = "critical" ) -type zapFactory struct { } +type zapFactory struct{} func newZapFactory() *zapFactory { return &zapFactory{} @@ -36,13 +36,14 @@ func (*zapFactory) getConfig(level zapcore.Level) zap.Config { } config := zap.Config{ - Level: zap.NewAtomicLevelAt(level), - Development: true, - Encoding: "console", - EncoderConfig: encoderConfig, - OutputPaths: []string{"stdout"}, - ErrorOutputPaths: []string{"stdout"}, - DisableCaller: true, + Level: zap.NewAtomicLevelAt(level), + Development: true, + Encoding: "console", + EncoderConfig: encoderConfig, + OutputPaths: []string{"stdout"}, + ErrorOutputPaths: []string{"stdout"}, + DisableCaller: true, + DisableStacktrace: true, } return config @@ -70,10 +71,10 @@ func (z *zapFactory) Build(level LogLevel) (*zap.SugaredLogger, error) { } type logService struct { - logger *zap.SugaredLogger + logger *zap.SugaredLogger } -func NewLogService(logLevel LogLevel) (*logService, error){ +func NewLogService(logLevel LogLevel) (*logService, error) { zapFact := newZapFactory() logger, err := zapFact.Build(logLevel) if err != nil { @@ -93,4 +94,3 @@ func (l *logService) Debug(args ...interface{}) { func (l *logService) Critical(args ...interface{}) { l.logger.Error(args) } -