-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigration_skipper.go
62 lines (53 loc) · 1.66 KB
/
migration_skipper.go
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
package migration
import (
"fmt"
"github.com/aleksanderaleksic/tgmigrate/common"
"github.com/aleksanderaleksic/tgmigrate/config"
"github.com/aleksanderaleksic/tgmigrate/history"
"github.com/aws/aws-sdk-go/service/s3"
)
type Skipper struct {
Context *common.Context
Config *config.Config
S3StateClient *s3.S3
HistoryInterface history.History
}
func (r Skipper) Skip(migrationName string) error {
hist, err := r.HistoryInterface.InitializeHistory()
if err != nil {
return err
}
migrationFiles, err := GetMigrationFiles(r.Config.AbsoluteMigrationDir)
if err != nil {
return fmt.Errorf("could not get migration files from '%s', error: %s", r.Config.AbsoluteMigrationDir, err)
}
migrationFile := getMigration(migrationName, migrationFiles)
if migrationFile == nil {
return fmt.Errorf("migration file with name: '%s' was not found", migrationName)
}
for _, m := range hist.AppliedMigration {
if m.Name == migrationName {
return fmt.Errorf("migration file with name: '%s' is already applied", migrationName)
}
}
for _, m := range hist.SkippedMigrations {
if m.Name == migrationName {
return fmt.Errorf("migration file with name: '%s' is already skipped", migrationName)
}
}
r.HistoryInterface.StoreSkippedMigration(&history.SkippedStorageHistoryObject{
SchemaVersion: history.StorageHistoryObjectVersion,
Skipped: common.JSONTime{},
Name: migrationName,
})
r.HistoryInterface.WriteToStorage()
return nil
}
func getMigration(migrationName string, migrations *[]File) *File {
for _, migration := range *migrations {
if migration.Metadata.FileName == migrationName {
return &migration
}
}
return nil
}