github.com/rkt/rkt@v1.30.1-0.20200224141603-171c416fac02/store/imagestore/schema.go (about) 1 // Copyright 2015 The rkt Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package imagestore 16 17 import ( 18 "database/sql" 19 "fmt" 20 ) 21 22 const ( 23 // Incremental db version at the current code revision. 24 dbVersion = 7 25 ) 26 27 // Statement to run when creating a db. These are the statements to create the 28 // db at the latest db version (dbVersion) provided by this rkt version. 29 // If the db already exists migration statements should be executed 30 var dbCreateStmts = [...]string{ 31 // version table 32 "CREATE TABLE IF NOT EXISTS version (version int);", 33 fmt.Sprintf("INSERT INTO version VALUES (%d)", dbVersion), 34 35 // remote table. The primary key is "aciurl". 36 "CREATE TABLE IF NOT EXISTS remote (aciurl string, sigurl string, etag string, blobkey string, cachemaxage int, downloadtime time);", 37 "CREATE UNIQUE INDEX IF NOT EXISTS aciurlidx ON remote (aciurl)", 38 39 // aciinfo table. The primary key is "blobkey" and it matches the key used to save that aci in the blob store 40 "CREATE TABLE IF NOT EXISTS aciinfo (blobkey string, name string, importtime time, lastused time, latest bool, size int64 DEFAULT 0, treestoresize int64 DEFAULT 0);", 41 "CREATE UNIQUE INDEX IF NOT EXISTS blobkeyidx ON aciinfo (blobkey)", 42 "CREATE INDEX IF NOT EXISTS nameidx ON aciinfo (name)", 43 } 44 45 // dbIsPopulated checks if the db is already populated (at any version) verifing if the "version" table exists 46 func dbIsPopulated(tx *sql.Tx) (bool, error) { 47 rows, err := tx.Query("SELECT Name FROM __Table where Name == $1", "version") 48 if err != nil { 49 return false, err 50 } 51 defer rows.Close() 52 count := 0 53 for rows.Next() { 54 count++ 55 } 56 if err := rows.Err(); err != nil { 57 return false, err 58 } 59 if count > 0 { 60 return true, nil 61 } 62 return false, nil 63 } 64 65 // getDBVersion retrieves the current db version 66 func getDBVersion(tx *sql.Tx) (int, error) { 67 var version int 68 rows, err := tx.Query("SELECT version FROM version") 69 if err != nil { 70 return -1, err 71 } 72 defer rows.Close() 73 found := false 74 for rows.Next() { 75 if err := rows.Scan(&version); err != nil { 76 return -1, err 77 } 78 found = true 79 break 80 } 81 if err := rows.Err(); err != nil { 82 return -1, err 83 } 84 if !found { 85 return -1, fmt.Errorf("db version table empty") 86 } 87 return version, nil 88 } 89 90 // updateDBVersion updates the db version 91 func updateDBVersion(tx *sql.Tx, version int) error { 92 // ql doesn't have an INSERT OR UPDATE function so 93 // it's faster to remove and reinsert the row 94 _, err := tx.Exec("DELETE FROM version") 95 if err != nil { 96 return err 97 } 98 _, err = tx.Exec("INSERT INTO version VALUES ($1)", version) 99 if err != nil { 100 return err 101 } 102 return nil 103 }