go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/migration/event.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package migration
     9  
    10  import (
    11  	"fmt"
    12  	"strings"
    13  )
    14  
    15  // NewEvent returns a new event.
    16  func NewEvent(result, body string, labels ...string) *Event {
    17  	return &Event{
    18  		Result: result,
    19  		Body:   body,
    20  		Labels: labels,
    21  	}
    22  }
    23  
    24  // Event is a migration logger event.
    25  type Event struct {
    26  	Result string
    27  	Body   string
    28  	Labels []string
    29  }
    30  
    31  // WriteText writes the migration event as text.
    32  func (e Event) String() string {
    33  	wr := new(strings.Builder)
    34  	if len(e.Result) > 0 {
    35  		wr.WriteString("--")
    36  		wr.WriteString(" ")
    37  		wr.WriteString(e.Result)
    38  	}
    39  	if len(e.Labels) > 0 {
    40  		wr.WriteString(" ")
    41  		wr.WriteString(strings.Join(e.Labels, " > "))
    42  	}
    43  	if len(e.Body) > 0 {
    44  		wr.WriteString(" -- ")
    45  		wr.WriteString(e.Body)
    46  	}
    47  	return wr.String()
    48  }
    49  
    50  // NewStatsEvent returns a new stats event.
    51  func NewStatsEvent(applied, skipped, failed, total int) *StatsEvent {
    52  	return &StatsEvent{
    53  		applied: applied,
    54  		skipped: skipped,
    55  		failed:  failed,
    56  		total:   total,
    57  	}
    58  }
    59  
    60  // StatsEvent is a migration logger event.
    61  type StatsEvent struct {
    62  	applied int
    63  	skipped int
    64  	failed  int
    65  	total   int
    66  }
    67  
    68  // WriteText writes the event to a text writer.
    69  func (se StatsEvent) String() string {
    70  	return fmt.Sprintf("%d applied %d skipped %d failed %d total", se.applied, se.skipped, se.failed, se.total)
    71  }