func populateDBV2(db *db.DB, dbVersion int, aciInfos []*ACIInfoV0_2, remotes []*RemoteV2_7) error { 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, cachemaxage int, downloadtime time);", "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)", } fn := func(tx *sql.Tx) error { for _, stmt := range dbCreateStmts { _, err := tx.Exec(stmt) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } fn = func(tx *sql.Tx) error { for _, aciinfo := range aciInfos { _, err := tx.Exec("INSERT into aciinfo values ($1, $2, $3, $4)", aciinfo.BlobKey, aciinfo.AppName, aciinfo.ImportTime, aciinfo.Latest) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } fn = func(tx *sql.Tx) error { for _, remote := range remotes { _, err := tx.Exec("INSERT into remote values ($1, $2, $3, $4, $5, $6)", remote.ACIURL, remote.SigURL, remote.ETag, remote.BlobKey, remote.CacheMaxAge, remote.DownloadTime) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } return nil }
func populateDBV6(db *db.DB, dbVersion int, aciInfos []*ACIInfoV6, remotes []*RemoteV2_6) error { 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, cachemaxage int, downloadtime time);", "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, name string, importtime time, lastused time, latest bool, size int64, treestoresize int64, insecureoptions int64 DEFAULT 0);", "CREATE UNIQUE INDEX IF NOT EXISTS blobkeyidx ON aciinfo (blobkey)", "CREATE INDEX IF NOT EXISTS nameidx ON aciinfo (name)", } fn := func(tx *sql.Tx) error { for _, stmt := range dbCreateStmts { _, err := tx.Exec(stmt) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } fn = func(tx *sql.Tx) error { for _, aciinfo := range aciInfos { _, err := tx.Exec("INSERT INTO aciinfo (blobkey, name, importtime, lastused, latest, size, treestoresize, souceurl, insecureoptions) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", aciinfo.BlobKey, aciinfo.Name, aciinfo.ImportTime, aciinfo.LastUsed, aciinfo.Latest, aciinfo.Size, aciinfo.TreeStoreSize, aciinfo.InsecureOptions) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } fn = func(tx *sql.Tx) error { for _, remote := range remotes { _, err := tx.Exec("INSERT into remote values ($1, $2, $3, $4, $5, $6)", remote.ACIURL, remote.SigURL, remote.ETag, remote.BlobKey, remote.CacheMaxAge, remote.DownloadTime) if err != nil { return err } } return nil } if err := db.Do(fn); err != nil { return err } return nil }
// load populates the given struct with the data in db. // the given struct d should be empty func (d *DBV0) load(db *db.DB) error { fn := func(tx *sql.Tx) error { var err error d.aciinfos, err = getAllACIInfosV0_2(tx) if err != nil { return err } d.remotes, err = getAllRemoteV0_1(tx) if err != nil { return err } return nil } if err := db.Do(fn); err != nil { return err } return nil }