-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move custom migrations to manifest-export
By "custom migrations" we mean those db migrations that can not be solved by plain SQL, so they are implemented in go. This provides the basic setup to move the custom migrations to the manifest-export-service. * It creates a new endpoint to accept migrations, however the endpoint is not called yet. * It provides the setup like reading/writing to the custom_migration_cutoff table However, this change does not include actually moving the migrations from cd- to export-service. This will be done in a separate PR. Ref: SRX-V6RVYF
- Loading branch information
1 parent
1da7225
commit 87806f0
Showing
13 changed files
with
588 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
database/migrations/postgres/1733938275776162_migrationcutoff.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS custom_migration_cutoff | ||
( | ||
migration_done_at TIMESTAMP NOT NULL, | ||
kuberpult_version varchar(100) PRIMARY KEY -- the version as it appears on GitHub, e.g. "1.2.3" | ||
); |
5 changes: 5 additions & 0 deletions
5
database/migrations/sqlite/1733938275776162_migrationcutoff.up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
CREATE TABLE IF NOT EXISTS custom_migration_cutoff | ||
( | ||
migration_done_at TIMESTAMP NOT NULL, | ||
kuberpult_version varchar(100) PRIMARY KEY -- the version as it appears on GitHub, e.g. "1.2.3" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
services/manifest-repo-export-service/pkg/migrations/migration.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/*This file is part of kuberpult. | ||
Kuberpult is free software: you can redistribute it and/or modify | ||
it under the terms of the Expat(MIT) License as published by | ||
the Free Software Foundation. | ||
Kuberpult is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
MIT License for more details. | ||
You should have received a copy of the MIT License | ||
along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>. | ||
Copyright freiheit.com*/ | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"github.com/freiheit-com/kuberpult/pkg/api/v1" | ||
"github.com/freiheit-com/kuberpult/pkg/db" | ||
"github.com/freiheit-com/kuberpult/pkg/logger" | ||
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
) | ||
|
||
type OnErrFunc = func(err error) error | ||
|
||
// StartSpanFromContext is the same as tracer.StartSpanFromContext, but also returns an onError function that tags the span as error | ||
// You should call the onErrorFunc when the span should be marked as failed. | ||
func StartSpanFromContext(ctx context.Context, name string) (tracer.Span, context.Context, OnErrFunc) { | ||
mySpan, ctx := tracer.StartSpanFromContext(ctx, name) | ||
onErr := func(err error) error { | ||
if err == nil { | ||
return nil | ||
} | ||
mySpan.Finish(tracer.WithError(err)) | ||
return err | ||
} | ||
return mySpan, ctx, onErr | ||
} | ||
|
||
func DBReadCustomMigrationCutoff(h *db.DBHandler, ctx context.Context, transaction *sql.Tx, requestedVersion *api.KuberpultVersion) (*api.KuberpultVersion, error) { | ||
span, ctx, onErr := StartSpanFromContext(ctx, "DBReadCustomMigrationCutoff") | ||
defer span.Finish() | ||
|
||
requestedVersionString := FormatKuberpultVersion(requestedVersion) | ||
|
||
selectQuery := h.AdaptQuery(` | ||
SELECT kuberpult_version | ||
FROM custom_migration_cutoff | ||
WHERE kuberpult_version=? | ||
LIMIT 1;`) | ||
span.SetTag("query", selectQuery) | ||
span.SetTag("requestedVersion", requestedVersionString) | ||
rows, err := transaction.QueryContext( | ||
ctx, | ||
selectQuery, | ||
requestedVersionString, | ||
) | ||
if err != nil { | ||
return nil, onErr(fmt.Errorf("could not query cutoff table from DB. Error: %w\n", err)) | ||
} | ||
defer func(rows *sql.Rows) { | ||
err := rows.Close() | ||
if err != nil { | ||
logger.FromContext(ctx).Sugar().Warnf("migration_cutoff: row closing error: %v", err) | ||
} | ||
}(rows) | ||
|
||
if !rows.Next() { | ||
return nil, nil | ||
} | ||
var rawVersion string | ||
err = rows.Scan(&rawVersion) | ||
if err != nil { | ||
if errors.Is(err, sql.ErrNoRows) { | ||
return nil, nil | ||
} | ||
return nil, onErr(fmt.Errorf("migration_cutoff: Error scanning row from DB. Error: %w\n", err)) | ||
} | ||
err = rows.Close() | ||
if err != nil { | ||
return nil, onErr(fmt.Errorf("migration_cutoff: row closing error: %v\n", err)) | ||
} | ||
err = rows.Err() | ||
if err != nil { | ||
return nil, onErr(fmt.Errorf("migration_cutoff: row has error: %v\n", err)) | ||
} | ||
|
||
var kuberpultVersion *api.KuberpultVersion | ||
kuberpultVersion, err = ParseKuberpultVersion(rawVersion) | ||
if err != nil { | ||
return nil, onErr(fmt.Errorf("migration_cutoff: Error parsing kuberpult version. Error: %w", err)) | ||
} | ||
return kuberpultVersion, nil | ||
} | ||
|
||
func DBWriteCustomMigrationCutoff(h *db.DBHandler, ctx context.Context, tx *sql.Tx, kuberpultVersion *api.KuberpultVersion) error { | ||
span, ctx, onErr := StartSpanFromContext(ctx, "DBWriteCustomMigrationCutoff") | ||
defer span.Finish() | ||
|
||
timestamp, err := h.DBReadTransactionTimestamp(ctx, tx) | ||
if err != nil { | ||
return onErr(fmt.Errorf("DBWriteCustomMigrationCutoff: Error reading transaction timestamp from DB. Error: %w", err)) | ||
} | ||
|
||
insertQuery := h.AdaptQuery("INSERT INTO custom_migration_cutoff (migration_done_at, kuberpult_version) VALUES (?, ?);") | ||
span.SetTag("query", insertQuery) | ||
|
||
_, err = tx.Exec( | ||
insertQuery, | ||
timestamp, | ||
FormatKuberpultVersion(kuberpultVersion), | ||
) | ||
if err != nil { | ||
return onErr(fmt.Errorf("could not write to cutoff table from DB. Error: %w\n", err)) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.