diff --git a/cmd/notary-server/main.go b/cmd/notary-server/main.go index b0ba03e141..72f4a6acda 100644 --- a/cmd/notary-server/main.go +++ b/cmd/notary-server/main.go @@ -19,7 +19,7 @@ import ( "github.com/docker/notary/config" "github.com/docker/notary/server" - "github.com/docker/notary/server/version" + "github.com/docker/notary/server/storage" "github.com/docker/notary/signer" ) @@ -77,7 +77,7 @@ func main() { logrus.Fatal("Error starting DB driver: ", err.Error()) return // not strictly needed but let's be explicit } - ctx = context.WithValue(ctx, "versionStore", version.NewVersionDB(db)) + ctx = context.WithValue(ctx, "versionStore", storage.NewMySQLStorage(db)) for { logrus.Info("[Notary Server] Starting Server") childCtx, cancel := context.WithCancel(ctx) diff --git a/server/handlers/default.go b/server/handlers/default.go index ba4c54d08b..cf8abf11fd 100644 --- a/server/handlers/default.go +++ b/server/handlers/default.go @@ -12,7 +12,7 @@ import ( "golang.org/x/net/context" "github.com/docker/notary/errors" - "github.com/docker/notary/server/version" + "github.com/docker/notary/server/storage" ) // MainHandler is the default handler for the server @@ -44,7 +44,7 @@ func UpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) Err: fmt.Errorf("Version store is nil"), } } - store, ok := s.(*version.VersionDB) + store, ok := s.(*storage.MySQLStorage) if !ok { return &errors.HTTPError{ HTTPStatus: http.StatusInternalServerError, @@ -87,7 +87,7 @@ func UpdateHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) // GetHandler accepts urls in the form //.json func GetHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) *errors.HTTPError { s := ctx.Value("versionStore") - store, ok := s.(*version.VersionDB) + store, ok := s.(*storage.MySQLStorage) if !ok { return &errors.HTTPError{ HTTPStatus: http.StatusInternalServerError, diff --git a/server/version/database.go b/server/storage/database.go similarity index 81% rename from server/version/database.go rename to server/storage/database.go index 23c9a7e506..a4d20dfb38 100644 --- a/server/version/database.go +++ b/server/storage/database.go @@ -1,12 +1,11 @@ -// version implementes a versioned store for TUF metadata -package version +package storage import ( "database/sql" "fmt" ) -// VersionDB implements a versioned store using a relational database. +// MySQLStorage implements a versioned store using a relational database. // The database table must look like: // CREATE TABLE `tuf_files` ( // `id` INT AUTO_INCREMENT, @@ -17,19 +16,19 @@ import ( // PRIMARY KEY (`id`) // UNIQUE INDEX (`qdn`, `role`, `version`) // ) DEFAULT CHARSET=utf8; -type VersionDB struct { +type MySQLStorage struct { sql.DB } -func NewVersionDB(db *sql.DB) *VersionDB { - return &VersionDB{ +func NewMySQLStorage(db *sql.DB) *MySQLStorage { + return &MySQLStorage{ DB: *db, } } // Update multiple TUF records in a single transaction. // Always insert a new row. The unique constraint will ensure there is only ever -func (vdb *VersionDB) UpdateCurrent(qdn, role string, version int, data []byte) error { +func (vdb *MySQLStorage) UpdateCurrent(qdn, role string, version int, data []byte) error { checkStmt := "SELECT count(*) FROM `tuf_files` WHERE `qdn`=? AND `role`=? AND `version`>=?;" insertStmt := "INSERT INTO `tuf_files` (`qdn`, `role`, `version`, `data`) VALUES (?,?,?,?) ;" @@ -55,7 +54,7 @@ func (vdb *VersionDB) UpdateCurrent(qdn, role string, version int, data []byte) } // Get a specific TUF record -func (vdb *VersionDB) GetCurrent(qdn, tufRole string) (data []byte, err error) { +func (vdb *MySQLStorage) GetCurrent(qdn, tufRole string) (data []byte, err error) { stmt := "SELECT `data` FROM `tuf_files` WHERE `qdn`=? AND `role`=? ORDER BY `version` DESC LIMIT 1;" rows, err := vdb.Query(stmt, qdn, tufRole) // this should be a QueryRow() if err != nil {