Skip to content

Commit

Permalink
add logs in diff (#52)
Browse files Browse the repository at this point in the history
* add logs in diff
  • Loading branch information
alexandremr01 authored Jul 6, 2022
1 parent 51f8b4f commit 467e8a7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
10 changes: 8 additions & 2 deletions applications/generate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package applications

import (
"path"

"github.com/migratemgr8/mgr8/domain"
"github.com/migratemgr8/mgr8/global"
"github.com/migratemgr8/mgr8/infrastructure"
Expand All @@ -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
}

Expand All @@ -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 {
Expand All @@ -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)
}
3 changes: 2 additions & 1 deletion applications/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

})
Expand Down
4 changes: 3 additions & 1 deletion applications/migrationscripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package applications

import (
"fmt"

"github.com/migratemgr8/mgr8/domain"
"github.com/migratemgr8/mgr8/infrastructure"
)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions applications/migrationscripts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"fmt"
"log"

"github.com/migratemgr8/mgr8/applications"
"github.com/migratemgr8/mgr8/domain"
Expand All @@ -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{
Expand All @@ -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
}
26 changes: 13 additions & 13 deletions infrastructure/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -93,4 +94,3 @@ func (l *logService) Debug(args ...interface{}) {
func (l *logService) Critical(args ...interface{}) {
l.logger.Error(args)
}

0 comments on commit 467e8a7

Please sign in to comment.