-
Notifications
You must be signed in to change notification settings - Fork 305
/
migration_store.go
71 lines (62 loc) · 2.26 KB
/
migration_store.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
63
64
65
66
67
68
69
70
71
// Copyright © 2020 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"runtime/trace"
"github.com/jinzhu/gorm"
)
// GetMigrationStore returns a MigrationStore on the given db (or transaction).
func GetMigrationStore(db *gorm.DB) MigrationStore {
return &migrationStore{store: newStore(db)}
}
type migrationStore struct {
*store
}
func (s *migrationStore) CreateMigration(ctx context.Context, migration *Migration) error {
defer trace.StartRegion(ctx, "create migration").End()
return s.createEntity(ctx, migration)
}
func (s *migrationStore) FindMigrations(ctx context.Context) ([]*Migration, error) {
defer trace.StartRegion(ctx, "find migrations").End()
query := s.query(ctx, Migration{}).Order(orderFromContext(ctx, "migrations", "created_at", "ASC"))
var models []*Migration
if err := query.Find(&models).Error; err != nil {
return nil, err
}
return models, nil
}
func (s *migrationStore) GetMigration(ctx context.Context, name string) (*Migration, error) {
defer trace.StartRegion(ctx, "get migration").End()
query := s.query(ctx, Migration{})
var model Migration
if err := query.Where(Migration{Name: name}).First(&model).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return nil, errMigrationNotFound.New()
}
return nil, err
}
return &model, nil
}
func (s *migrationStore) DeleteMigration(ctx context.Context, name string) error {
defer trace.StartRegion(ctx, "delete migration").End()
var model Migration
if err := s.query(ctx, Migration{}).Where(Migration{Name: name}).First(&model).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return errMigrationNotFound.New()
}
return err
}
return s.query(ctx, Migration{}).Delete(&model).Error
}