Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DB: Add environment variable option to skip DB table creationˆ #2245

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/db/v1beta1/common/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ const (
DefaultPostgreSQLDatabase = "katib"
DefaultPostgreSQLHost = "katib-postgres"
DefaultPostgreSQLPort = "5432"

SkipDbInitializationEnvName = "SKIP_DB_INITIALIZATION"
)
22 changes: 18 additions & 4 deletions pkg/db/v1beta1/mysql/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,34 @@ import (
"fmt"

"k8s.io/klog"

"github.com/kubeflow/katib/pkg/db/v1beta1/common"
"github.com/kubeflow/katib/pkg/util/v1beta1/env"
)
lkaybob marked this conversation as resolved.
Show resolved Hide resolved

func (d *dbConn) DBInit() {
db := d.db
klog.Info("Initializing v1beta1 DB schema")
skipDbInitialization := env.GetEnvOrDefault(common.SkipDbInitializationEnvName, "false")

if skipDbInitialization == "false" {
klog.Info("Initializing v1beta1 DB schema")

_, err := db.Exec(`CREATE TABLE IF NOT EXISTS observation_logs
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS observation_logs
(trial_name VARCHAR(255) NOT NULL,
id INT AUTO_INCREMENT PRIMARY KEY,
time DATETIME(6),
metric_name VARCHAR(255) NOT NULL,
value TEXT NOT NULL)`)
if err != nil {
klog.Fatalf("Error creating observation_logs table: %v", err)
if err != nil {
klog.Fatalf("Error creating observation_logs table: %v", err)
}
} else {
klog.Info("Skipping v1beta1 DB schema initialization.")

_, err := db.Query(`SELECT trial_name, id, time, metric_name, value FROM observation_logs LIMIT 1`)
lkaybob marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
klog.Fatalf("Error validating observation_logs table: %v", err)
}
}
}

Expand Down
22 changes: 18 additions & 4 deletions pkg/db/v1beta1/postgres/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,34 @@ import (
"fmt"

"k8s.io/klog"

"github.com/kubeflow/katib/pkg/db/v1beta1/common"
"github.com/kubeflow/katib/pkg/util/v1beta1/env"
)

func (d *dbConn) DBInit() {
db := d.db
klog.Info("Initializing v1beta1 DB schema")
skipDbInitialization := env.GetEnvOrDefault(common.SkipDbInitializationEnvName, "false")

if skipDbInitialization == "false" {
klog.Info("Initializing v1beta1 DB schema")

_, err := db.Exec(`CREATE TABLE IF NOT EXISTS observation_logs
_, err := db.Exec(`CREATE TABLE IF NOT EXISTS observation_logs
(trial_name VARCHAR(255) NOT NULL,
id serial PRIMARY KEY,
time TIMESTAMP(6),
metric_name VARCHAR(255) NOT NULL,
value TEXT NOT NULL)`)
if err != nil {
klog.Fatalf("Error creating observation_logs table: %v", err)
if err != nil {
klog.Fatalf("Error creating observation_logs table: %v", err)
}
} else {
klog.Info("Skipping v1beta1 DB schema initialization.")

_, err := db.Query(`SELECT trial_name, id, time, metric_name, value FROM observation_logs LIMIT 1`)
lkaybob marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
klog.Fatalf("Error validating observation_logs table: %v", err)
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/util/v1beta1/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ limitations under the License.

package env

import "os"
import (
"os"
)

func GetEnvOrDefault(key string, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
Expand Down