1
0
Fork 0
miniflux/storage/migration.go

56 lines
1.4 KiB
Go
Raw Normal View History

2017-11-20 00:10:04 -05:00
// 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"
2017-11-23 01:22:33 -05:00
2017-12-15 21:55:57 -05:00
"github.com/miniflux/miniflux/logger"
2017-12-13 00:48:13 -05:00
"github.com/miniflux/miniflux/sql"
2017-11-20 00:10:04 -05:00
)
2017-12-22 14:33:01 -05:00
const schemaVersion = 12
2017-11-20 00:10:04 -05:00
2017-11-23 01:22:33 -05:00
// Migrate run database migrations.
2017-11-20 00:10:04 -05:00
func (s *Storage) Migrate() {
var currentVersion int
s.db.QueryRow(`select version from schema_version`).Scan(&currentVersion)
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 {
2017-12-15 21:55:57 -05:00
logger.Fatal("[Storage:Migrate] %v", err)
2017-11-20 00:10:04 -05:00
}
rawSQL := sql.SqlMap["schema_version_"+strconv.Itoa(version)]
// fmt.Println(rawSQL)
_, err = tx.Exec(rawSQL)
if err != nil {
tx.Rollback()
2017-12-15 21:55:57 -05:00
logger.Fatal("[Storage:Migrate] %v", err)
2017-11-20 00:10:04 -05:00
}
if _, err := tx.Exec(`delete from schema_version`); err != nil {
tx.Rollback()
2017-12-15 21:55:57 -05:00
logger.Fatal("[Storage:Migrate] %v", err)
2017-11-20 00:10:04 -05:00
}
if _, err := tx.Exec(`insert into schema_version (version) values($1)`, version); err != nil {
tx.Rollback()
2017-12-15 21:55:57 -05:00
logger.Fatal("[Storage:Migrate] %v", err)
2017-11-20 00:10:04 -05:00
}
if err := tx.Commit(); err != nil {
2017-12-15 21:55:57 -05:00
logger.Fatal("[Storage:Migrate] %v", err)
2017-11-20 00:10:04 -05:00
}
}
}