aboutsummaryrefslogtreecommitdiffstats
path: root/internal/services/migration.go
blob: ef6a509cd0711027c2e34dd0b69e9d762d2c1c92 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package services

import (
	"fmt"
	"sort"

	"github.com/eugenetriguba/bolt/internal/models"
	"github.com/eugenetriguba/bolt/internal/output"
	"github.com/eugenetriguba/bolt/internal/repositories"
)

type MigrationService struct {
	dbRepo    *repositories.MigrationDBRepo
	fsRepo    *repositories.MigrationFsRepo
	outputter output.Outputter
}

func NewMigrationService(
	dbRepo *repositories.MigrationDBRepo,
	fsRepo *repositories.MigrationFsRepo,
	outputter output.Outputter,
) *MigrationService {
	return &MigrationService{dbRepo: dbRepo, fsRepo: fsRepo, outputter: outputter}
}

type sortOrder int

const (
	SortOrderDesc sortOrder = iota
	SortOrderAsc
)

func (ms *MigrationService) ApplyAllMigrations() error {
	migrations, err := ms.ListMigrations(SortOrderAsc)
	if err != nil {
		return err
	}

	for _, migration := range migrations {
		if !migration.Applied {
			ms.ApplyMigration(migration)
		}
	}

	return nil
}

func (ms *MigrationService) ApplyMigration(migration *models.Migration) error {
	ms.outputter.Output(
		fmt.Sprintf(
			"Applying migration %s_%s..",
			migration.Version,
			migration.Message,
		),
	)

	scriptContents, err := ms.fsRepo.ReadUpgradeScript(migration)
	if err != nil {
		return err
	}

	err = ms.dbRepo.Apply(scriptContents, migration)
	if err != nil {
		return err
	}
	ms.outputter.Output(
		fmt.Sprintf(
			"Successfully applied migration %s_%s!",
			migration.Version,
			migration.Message,
		),
	)

	return nil
}

func (ms *MigrationService) RevertAllMigrations() error {
	migrations, err := ms.ListMigrations(SortOrderDesc)
	if err != nil {
		return err
	}

	for _, migration := range migrations {
		if migration.Applied {
			ms.RevertMigration(migration)
		}
	}

	return nil
}

func (ms *MigrationService) RevertMigration(migration *models.Migration) error {
	ms.outputter.Output(
		fmt.Sprintf(
			"Reverting migration %s_%s..",
			migration.Version,
			migration.Message,
		),
	)
	scriptContents, err := ms.fsRepo.ReadDowngradeScript(migration)
	if err != nil {
		return err
	}

	err = ms.dbRepo.Revert(scriptContents, migration)
	if err != nil {
		return err
	}
	ms.outputter.Output(
		fmt.Sprintf(
			"Successfully reverted migration %s_%s!",
			migration.Version,
			migration.Message,
		),
	)

	return nil
}

func (ms *MigrationService) CreateMigration(migration *models.Migration) error {
	err := ms.fsRepo.Create(migration)
	if err != nil {
		return err
	}
	ms.outputter.Output(
		fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
	)
	return nil
}

func (ms *MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
	// Assumption: All migration versions that have been applied
	// to the database exist locally in the list of filesystem migrations.
	// This would hold true unless someone decided to apply a migration and,
	// for whatever reason, delete it later from their filesystem.
	migrations, err := ms.fsRepo.List()
	if err != nil {
		return nil, err
	}

	appliedMigrations, err := ms.dbRepo.List()
	if err != nil {
		return nil, err
	}

	var flatMigrations []*models.Migration
	for _, migration := range migrations {
		_, ok := appliedMigrations[migration.Version]
		if ok {
			migration.Applied = true
		}
		flatMigrations = append(flatMigrations, migration)
	}

	sort.Slice(flatMigrations, func(i, j int) bool {
		if order == SortOrderAsc {
			return flatMigrations[i].Version < flatMigrations[j].Version
		} else {
			return flatMigrations[i].Version > flatMigrations[j].Version
		}
	})

	return flatMigrations, nil
}