mirror of
https://github.com/clearlinux/rkt.git
synced 2026-06-16 02:05:48 +00:00
679b3bfe9c
This patch adds migrate codes and tests. It simply updates the current db version to 1 without changing the schema. The migration needs an exclusive store lock to avoid concurrent old rkt versions operating to an updated db and returning strange errors or giving bad behaviours. As the store lock is removed when spawning the stage0, migration isn't blocked by running containers but only by rkt processes doing store operations (like downloading an image, rendering an aci etc...). Now migration covers only db updates. If the migration needs to also operate on the other "non transactional" store entities (like the diskv stores or the tree store) this should be thoroughly reasoned, as a rollback in case of an error is not an easy task. Test are added to introduce the migration testing framework and be the base for future migration tests.
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package store
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
)
|
|
|
|
const (
|
|
// Incremental db version at the current code revision.
|
|
dbVersion = 1
|
|
)
|
|
|
|
// Statement to run when creating a db. These are the statements to create the
|
|
// db at the latest db version (dbVersion) provided by this rkt version.
|
|
// If the db already exists migration statements should be executed
|
|
var dbCreateStmts = [...]string{
|
|
// version table
|
|
"CREATE TABLE IF NOT EXISTS version (version int);",
|
|
fmt.Sprintf("INSERT INTO version VALUES (%d)", dbVersion),
|
|
|
|
// remote table. The primary key is "aciurl".
|
|
"CREATE TABLE IF NOT EXISTS remote (aciurl string, sigurl string, etag string, blobkey string);",
|
|
"CREATE UNIQUE INDEX IF NOT EXISTS aciurlidx ON remote (aciurl)",
|
|
|
|
// aciinfo table. The primary key is "blobkey" and it matches the key used to save that aci in the blob store
|
|
"CREATE TABLE IF NOT EXISTS aciinfo (blobkey string, appname string, importtime time, latest bool);",
|
|
"CREATE UNIQUE INDEX IF NOT EXISTS blobkeyidx ON aciinfo (blobkey)",
|
|
"CREATE INDEX IF NOT EXISTS appnameidx ON aciinfo (appname)",
|
|
}
|
|
|
|
// dbIsPopulated checks if the db is already populated (at any version) verifing if the "version" table exists
|
|
func dbIsPopulated(tx *sql.Tx) (bool, error) {
|
|
rows, err := tx.Query("SELECT Name FROM __Table where Name == $1", "version")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
count := 0
|
|
for rows.Next() {
|
|
count++
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return false, err
|
|
}
|
|
if count > 0 {
|
|
return true, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// getDBVersion retrieves the current db version
|
|
func getDBVersion(tx *sql.Tx) (int, error) {
|
|
var version int
|
|
rows, err := tx.Query("SELECT version FROM version")
|
|
if err != nil {
|
|
return -1, err
|
|
}
|
|
found := false
|
|
for rows.Next() {
|
|
if err := rows.Scan(&version); err != nil {
|
|
return -1, err
|
|
}
|
|
found = true
|
|
break
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return -1, err
|
|
}
|
|
if !found {
|
|
return -1, fmt.Errorf("db version table empty")
|
|
}
|
|
return version, nil
|
|
}
|
|
|
|
// updateDBVersion updates the db version
|
|
func updateDBVersion(tx *sql.Tx, version int) error {
|
|
// ql doesn't have an INSERT OR UPDATE function so
|
|
// it's faster to remove and reinsert the row
|
|
_, err := tx.Exec("DELETE FROM version")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.Exec("INSERT INTO version VALUES ($1)", version)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|