github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/scripts/update/migrations/1-create-events-triggers-history.go (about)

     1  package migrations
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/pkg/errors"
     7  	"github.com/sharovik/devbot/internal/container"
     8  	"github.com/sharovik/devbot/internal/dto/databasedto"
     9  	"github.com/sharovik/orm/clients"
    10  	"github.com/sharovik/orm/dto"
    11  	"github.com/sharovik/orm/query"
    12  )
    13  
    14  type EventsTriggersHistoryMigration struct {
    15  	Client clients.BaseClientInterface
    16  }
    17  
    18  func (m EventsTriggersHistoryMigration) SetClient(client clients.BaseClientInterface) {
    19  	m.Client = client
    20  }
    21  
    22  func (m EventsTriggersHistoryMigration) GetName() string {
    23  	return "create-events-triggers-history"
    24  }
    25  
    26  func (m EventsTriggersHistoryMigration) Execute() error {
    27  	client := container.C.Dictionary.GetDBClient()
    28  
    29  	//Create events table
    30  	q := new(clients.Query).
    31  		Create(databasedto.EventTriggerHistoryModel).
    32  		IfNotExists().
    33  		AddIndex(dto.Index{
    34  			Name:   "user_index",
    35  			Target: databasedto.EventTriggerHistoryModel.GetTableName(),
    36  			Key:    "user",
    37  			Unique: false,
    38  		}).
    39  		AddForeignKey(dto.ForeignKey{
    40  			Name: "event_id",
    41  			Target: query.Reference{
    42  				Table: databasedto.EventModel.GetTableName(),
    43  				Key:   "id",
    44  			},
    45  			With: query.Reference{
    46  				Table: databasedto.EventTriggerHistoryModel.GetTableName(),
    47  				Key:   "event_id",
    48  			},
    49  			OnDelete: dto.CascadeAction,
    50  			OnUpdate: dto.NoActionAction,
    51  		})
    52  	if _, err := client.Execute(q); err != nil {
    53  		return errors.Wrap(err, fmt.Sprintf("Failed to create %s table", databasedto.EventTriggerHistoryModel.GetTableName()))
    54  	}
    55  
    56  	return nil
    57  }