Skip to content

Commit

Permalink
Merge pull request #99 from Icinga/simple-migration
Browse files Browse the repository at this point in the history
Migrate schema
  • Loading branch information
lippserd authored Sep 26, 2024
2 parents 3338daf + c1fc2da commit 7eaf35f
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
67 changes: 65 additions & 2 deletions cmd/icinga-kubernetes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/icinga/icinga-go-library/periodic"
"github.com/icinga/icinga-go-library/types"
"github.com/icinga/icinga-kubernetes/internal"
"github.com/icinga/icinga-kubernetes/pkg/backoff"
"github.com/icinga/icinga-kubernetes/pkg/com"
"github.com/icinga/icinga-kubernetes/pkg/database"
"github.com/icinga/icinga-kubernetes/pkg/metrics"
"github.com/icinga/icinga-kubernetes/pkg/retry"
schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1"
"github.com/icinga/icinga-kubernetes/pkg/sync"
syncv1 "github.com/icinga/icinga-kubernetes/pkg/sync/v1"
Expand All @@ -33,6 +35,8 @@ import (
"time"
)

const expectedSchemaVersion = "0.2.0"

func main() {
runtime.ReallyCrash = true

Expand Down Expand Up @@ -100,6 +104,67 @@ func main() {
klog.Fatal(err)
}

g, ctx := errgroup.WithContext(context.Background())

if hasSchema {
var version string

err = retry.WithBackoff(
ctx,
func(ctx context.Context) (err error) {
query := "SELECT version FROM kubernetes_schema ORDER BY id DESC LIMIT 1"
err = db.QueryRowxContext(ctx, query).Scan(&version)
if err != nil {
err = database.CantPerformQuery(err, query)
}
return
},
retry.Retryable,
backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute),
retry.Settings{})
if err != nil {
klog.Fatal(err)
}

if version != expectedSchemaVersion {
err = retry.WithBackoff(
ctx,
func(ctx context.Context) (err error) {
rows, err := db.Query(
db.Rebind("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?"),
cfg.Database.Database,
)
if err != nil {
klog.Fatal(err)
}
defer rows.Close()

dbLog.Info("Dropping schema")

for rows.Next() {
var tableName string
if err := rows.Scan(&tableName); err != nil {
klog.Fatal(err)
}

_, err := db.Exec("DROP TABLE " + tableName)
if err != nil {
klog.Fatal(err)
}
}
return
},
retry.Retryable,
backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute),
retry.Settings{})
if err != nil {
klog.Fatal(err)
}

hasSchema = false
}
}

if !hasSchema {
dbLog.Info("Importing schema")

Expand All @@ -112,8 +177,6 @@ func main() {
}
}

g, ctx := errgroup.WithContext(context.Background())

if _, err := db.ExecContext(ctx, "DELETE FROM kubernetes_instance"); err != nil {
klog.Fatal(errors.Wrap(err, "can't delete instance"))
}
Expand Down
2 changes: 1 addition & 1 deletion schema/mysql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -959,4 +959,4 @@ CREATE TABLE kubernetes_schema (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

INSERT INTO kubernetes_schema (version, timestamp, success, reason)
VALUES ('0.1.0', UNIX_TIMESTAMP() * 1000, 'y', 'Initial import');
VALUES ('0.2.0', UNIX_TIMESTAMP() * 1000, 'y', 'Initial import');

0 comments on commit 7eaf35f

Please sign in to comment.