forked from miniflux/v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
55 lines (43 loc) · 1.37 KB
/
migration.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
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package storage
import (
"fmt"
"strconv"
"github.com/miniflux/miniflux/logger"
"github.com/miniflux/miniflux/sql"
)
const schemaVersion = 12
// Migrate run database migrations.
func (s *Storage) Migrate() {
var currentVersion int
s.db.QueryRow(`select version from schema_version`).Scan(¤tVersion)
fmt.Println("Current schema version:", currentVersion)
fmt.Println("Latest schema version:", schemaVersion)
for version := currentVersion + 1; version <= schemaVersion; version++ {
fmt.Println("Migrating to version:", version)
tx, err := s.db.Begin()
if err != nil {
logger.Fatal("[Storage:Migrate] %v", err)
}
rawSQL := sql.SqlMap["schema_version_"+strconv.Itoa(version)]
// fmt.Println(rawSQL)
_, err = tx.Exec(rawSQL)
if err != nil {
tx.Rollback()
logger.Fatal("[Storage:Migrate] %v", err)
}
if _, err := tx.Exec(`delete from schema_version`); err != nil {
tx.Rollback()
logger.Fatal("[Storage:Migrate] %v", err)
}
if _, err := tx.Exec(`insert into schema_version (version) values($1)`, version); err != nil {
tx.Rollback()
logger.Fatal("[Storage:Migrate] %v", err)
}
if err := tx.Commit(); err != nil {
logger.Fatal("[Storage:Migrate] %v", err)
}
}
}