From 891581e11fd367d69ac96518c448e77432dee828 Mon Sep 17 00:00:00 2001 From: Colleen Murphy Date: Wed, 8 May 2024 16:39:45 -0700 Subject: [PATCH] Run CREATE statement in backfill script (#2109) For the case where we want to start populating a new MySQL instance but the Rekor server storage provider is not actually switched to MySQL yet, ensure the table is created before trying to copy the data. Signed-off-by: Colleen Murphy --- cmd/backfill-index/main.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/backfill-index/main.go b/cmd/backfill-index/main.go index aa05e47ab..efb85ea3c 100644 --- a/cmd/backfill-index/main.go +++ b/cmd/backfill-index/main.go @@ -69,7 +69,14 @@ import ( ) const ( - mysqlWriteStmt = "INSERT IGNORE INTO EntryIndex (EntryKey, EntryUUID) VALUES (:key, :uuid)" + mysqlWriteStmt = "INSERT IGNORE INTO EntryIndex (EntryKey, EntryUUID) VALUES (:key, :uuid)" + mysqlCreateTableStmt = `CREATE TABLE IF NOT EXISTS EntryIndex ( + PK BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, + EntryKey varchar(512) NOT NULL, + EntryUUID char(80) NOT NULL, + PRIMARY KEY(PK), + UNIQUE(EntryKey, EntryUUID) + )` ) type provider int @@ -190,6 +197,9 @@ func getIndexClient(backend provider) (indexClient, error) { if err = dbClient.Ping(); err != nil { return nil, err } + if _, err = dbClient.Exec(mysqlCreateTableStmt); err != nil { + return nil, err + } return &mysqlClient{client: dbClient}, nil default: return nil, fmt.Errorf("could not create client for unexpected provider")