diff --git a/database/migrations/postgres/1732637175997202_releases_autoincrement.up.sql b/database/migrations/postgres/1732637175997202_releases_autoincrement.up.sql index 5d955a4f6..109a3f143 100644 --- a/database/migrations/postgres/1732637175997202_releases_autoincrement.up.sql +++ b/database/migrations/postgres/1732637175997202_releases_autoincrement.up.sql @@ -1,4 +1,12 @@ -ALTER TABLE IF EXISTS releases ADD COLUMN IF NOT EXISTS version INTEGER; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS releases ADD COLUMN IF NOT EXISTS version INTEGER; + END IF; +END $$; DO $$ BEGIN IF EXISTS (SELECT 1 @@ -16,15 +24,55 @@ BEGIN END IF; END $$; -CREATE SEQUENCE IF NOT EXISTS releases_version_seq OWNED BY releases.version; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + CREATE SEQUENCE IF NOT EXISTS releases_version_seq OWNED BY releases.version; + END IF; +END $$; -SELECT setval('releases_version_seq', coalesce(max(version), 0) + 1, false) FROM releases; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + PERFORM setval('releases_version_seq', coalesce(max(version), 0) + 1, false) FROM releases; + END IF; +END $$; -ALTER TABLE IF EXISTS releases -ALTER COLUMN version SET DEFAULT nextval('releases_version_seq'); +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS releases + ALTER COLUMN version SET DEFAULT nextval('releases_version_seq'); + END IF; +END $$; -ALTER TABLE IF EXISTS releases DROP CONSTRAINT IF EXISTS releases_pkey; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS releases DROP CONSTRAINT IF EXISTS releases_pkey; + END IF; +END $$; -ALTER TABLE IF EXISTS releases ADD PRIMARY KEY (version, appname, releaseversion); +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'releases' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS releases ADD PRIMARY KEY (version, appname, releaseversion); + END IF; +END $$; ALTER TABLE IF EXISTS releases DROP COLUMN IF EXISTS eslversion; diff --git a/database/migrations/postgres/1733415120086267_deployments_autoincrement.up.sql b/database/migrations/postgres/1733415120086267_deployments_autoincrement.up.sql index 6fb44fe1a..f55d36763 100644 --- a/database/migrations/postgres/1733415120086267_deployments_autoincrement.up.sql +++ b/database/migrations/postgres/1733415120086267_deployments_autoincrement.up.sql @@ -1,4 +1,13 @@ -ALTER TABLE IF EXISTS deployments ADD COLUMN IF NOT EXISTS version INTEGER; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS deployments ADD COLUMN IF NOT EXISTS version INTEGER; + END IF; +END $$; + DO $$ BEGIN IF EXISTS (SELECT 1 @@ -16,15 +25,55 @@ BEGIN END IF; END $$; -CREATE SEQUENCE IF NOT EXISTS deployments_version_seq OWNED BY deployments.version; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + CREATE SEQUENCE IF NOT EXISTS deployments_version_seq OWNED BY deployments.version; + END IF; +END $$; -SELECT setval('deployments_version_seq', coalesce(max(version), 0) + 1, false) FROM deployments; -ALTER TABLE IF EXISTS deployments -ALTER COLUMN version SET DEFAULT nextval('deployments_version_seq'); +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + PERFORM setval('deployments_version_seq', coalesce(max(version), 0) + 1, false) FROM deployments; + END IF; +END $$; -ALTER TABLE IF EXISTS deployments DROP CONSTRAINT IF EXISTS deployments_pkey; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS deployments + ALTER COLUMN version SET DEFAULT nextval('deployments_version_seq'); + END IF; +END $$; +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS deployments DROP CONSTRAINT IF EXISTS deployments_pkey; + END IF; +END $$; -ALTER TABLE IF EXISTS deployments ADD PRIMARY KEY (version, appname, envname); +DO $$ +BEGIN + IF EXISTS (SELECT 1 + FROM information_schema.columns + WHERE table_name = 'deployments' + AND column_name = 'eslversion') THEN + ALTER TABLE IF EXISTS deployments ADD PRIMARY KEY (version, appname, envname); + END IF; +END $$; ALTER TABLE IF EXISTS deployments DROP COLUMN IF EXISTS eslversion; diff --git a/database/migrations/postgres/1733867056189286_environments_autoincrement.up.sql b/database/migrations/postgres/1733867056189286_environments_autoincrement.up.sql index b4385d9e3..58c44ca9b 100644 --- a/database/migrations/postgres/1733867056189286_environments_autoincrement.up.sql +++ b/database/migrations/postgres/1733867056189286_environments_autoincrement.up.sql @@ -24,7 +24,23 @@ SELECT setval('environments_version_seq', coalesce(max(row_version), 0) + 1, fal ALTER TABLE IF EXISTS environments ALTER COLUMN row_version SET DEFAULT nextval('environments_version_seq'); -ALTER TABLE IF EXISTS environments DROP CONSTRAINT IF EXISTS environments_pkey; +DO $$ +DECLARE + cmd TEXT; +BEGIN + FOR cmd IN + SELECT format( + 'ALTER TABLE %I DROP CONSTRAINT %I;', + relname, conname + ) + FROM pg_constraint c + JOIN pg_class t ON c.conrelid = t.oid + WHERE conname LIKE 'environments_pkey%' + LOOP + EXECUTE cmd; + END LOOP; +END $$; + ALTER TABLE IF EXISTS environments ADD PRIMARY KEY (row_version, name); diff --git a/database/migrations/postgres/1733913761808894_separate_releases_history.up.sql b/database/migrations/postgres/1733913761808894_separate_releases_history.up.sql new file mode 100644 index 000000000..dcbb3d7e5 --- /dev/null +++ b/database/migrations/postgres/1733913761808894_separate_releases_history.up.sql @@ -0,0 +1,69 @@ +-- rename releases table to releases_history if it doesn't exist +DO $$ +BEGIN + IF EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' AND table_name = 'releases' + ) AND NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' AND table_name = 'releases_history' + ) THEN + ALTER TABLE releases RENAME TO releases_history; + END IF; +END +$$; + +-- create releases table if it doesn't exist +CREATE TABLE IF NOT EXISTS releases +( + releaseVersion INTEGER, + created TIMESTAMP, + appName VARCHAR, + manifests VARCHAR, + metadata VARCHAR, + environments VARCHAR, + PRIMARY KEY(releaseVersion, appName) +); + +-- insert data into releases table from releases_history table if there's no data inside it +DO $$ +BEGIN + IF EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' AND table_name = 'releases' + ) AND NOT EXISTS ( + SELECT 1 FROM releases LIMIT 1 + ) AND EXISTS ( + SELECT FROM information_schema.tables + WHERE table_schema = 'public' AND table_name = 'releases_history' + ) THEN + INSERT INTO releases (releaseVersion, created, appName, manifests, metadata, environments) + SELECT DISTINCT + releases_history.releaseVersion, + releases_history.created, + releases_history.appName, + releases_history.manifests, + releases_history.metadata, + releases_history.environments + FROM ( + SELECT + MAX(version) AS latestRelease, + appname, + releaseversion + FROM + "releases_history" + GROUP BY + appname, releaseversion) AS latest + JOIN + releases_history AS releases_history + ON + latest.latestRelease=releases_history.version + AND latest.releaseVersion=releases_history.releaseVersion + AND latest.appname=releases_history.appname + WHERE releases_history.deleted=false; + END IF; +END +$$; + +-- Remove all_releases table +DROP TABLE IF EXISTS all_releases; diff --git a/database/migrations/sqlite/1733913761808894_separate_releases_history.up.sql b/database/migrations/sqlite/1733913761808894_separate_releases_history.up.sql new file mode 100644 index 000000000..a3c795d3e --- /dev/null +++ b/database/migrations/sqlite/1733913761808894_separate_releases_history.up.sql @@ -0,0 +1,31 @@ + +CREATE TABLE IF NOT EXISTS releases_history +( + releaseVersion INTEGER, + created TIMESTAMP, + appName VARCHAR, + manifests VARCHAR, + metadata VARCHAR, + deleted BOOLEAN, + environments VARCHAR, + version INTEGER PRIMARY KEY AUTOINCREMENT +); + +INSERT INTO releases_history (releaseversion, created, appname, manifests, metadata, deleted) +SELECT releaseversion, created, appname, manifests, metadata, deleted +FROM releases +ORDER BY version; + +DROP TABLE IF EXISTS releases; +DROP TABLE IF EXISTS all_releases; + +CREATE TABLE IF NOT EXISTS releases +( + releaseVersion INTEGER, + created TIMESTAMP, + appName VARCHAR, + manifests VARCHAR, + metadata VARCHAR, + environments VARCHAR, + PRIMARY KEY (releaseVersion, appName) +); \ No newline at end of file diff --git a/pkg/db/db.go b/pkg/db/db.go index e3fc5adef..898511daa 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -514,773 +514,6 @@ func (h *DBHandler) DBReadEslEventLaterThan(ctx context.Context, tx *sql.Tx, esl return row, nil } -// RELEASES -// Releases work a bit different, because they are already immutable. -// We still store the eslVersion for consistency with other tables, -// but technically it's not required here. - -type DBReleaseMetaData struct { - SourceAuthor string - SourceCommitId string - SourceMessage string - DisplayVersion string - UndeployVersion bool - IsMinor bool - CiLink string - IsPrepublish bool -} - -type DBReleaseManifests struct { - Manifests map[string]string -} - -type DBReleaseWithMetaData struct { - ReleaseNumber uint64 - Created time.Time - App string - Manifests DBReleaseManifests - Metadata DBReleaseMetaData - Deleted bool - Environments []string -} - -type DBAllReleaseMetaData struct { - Releases []int64 -} -type DBAllReleasesWithMetaData struct { - EslVersion EslVersion - Created time.Time - App string - Metadata DBAllReleaseMetaData -} - -func (h *DBHandler) DBSelectAnyRelease(ctx context.Context, tx *sql.Tx, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { - selectQuery := h.AdaptQuery(fmt.Sprintf( - "SELECT created, appName, metadata, releaseVersion, deleted, environments" + - " FROM releases " + - " LIMIT 1;")) - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAnyRelease") - defer span.Finish() - span.SetTag("query", selectQuery) - - rows, err := tx.QueryContext( - ctx, - selectQuery, - ) - processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrepublishes, false) - if err != nil { - return nil, err - } - if len(processedRows) == 0 { - return nil, nil - } - return processedRows[0], nil -} - -func (h *DBHandler) DBSelectReleasesWithoutEnvironments(ctx context.Context, tx *sql.Tx) ([]*DBReleaseWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesWithoutEnvironments") - defer span.Finish() - selectQuery := h.AdaptQuery(` - SELECT DISTINCT - releases.created, - releases.appName, - releases.metadata, - releases.manifests, - releases.releaseVersion, - releases.deleted, - releases.environments - FROM ( - SELECT - MAX(version) AS latestVersion, - appname, - releaseversion - FROM - "releases" - GROUP BY - appname, releaseversion) AS latest - JOIN - releases AS releases - ON - latest.latestVersion=releases.version - AND latest.releaseVersion=releases.releaseVersion - AND latest.appname=releases.appname - WHERE COALESCE(environments, '') = '' AND COALESCE(manifests, '') != '' - LIMIT 100; - `) - rows, err := tx.QueryContext(ctx, selectQuery) - processedRows, err := h.processReleaseRows(ctx, err, rows, true, true) - if err != nil { - return nil, err - } - return processedRows, nil -} - -func (h *DBHandler) DBSelectReleasesByVersions(ctx context.Context, tx *sql.Tx, app string, releaseVersions []uint64, ignorePrePublishes bool) ([]*DBReleaseWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByVersions") - defer span.Finish() - - if len(releaseVersions) == 0 { - return []*DBReleaseWithMetaData{}, nil - } - repeatedQuestionMarks := strings.Repeat(",?", len(releaseVersions)-1) - selectQuery := h.AdaptQuery(` - - SELECT DISTINCT - releases.created, - releases.appName, - releases.metadata, - releases.releaseVersion, - releases.deleted, - releases.environments - FROM - ( SELECT - MAX(version) AS latestVersion, - appname, - releaseversion - FROM - "releases" - WHERE - appname=? - AND - releaseversion IN (?` + repeatedQuestionMarks + `) - GROUP BY - appname, releaseversion - ) AS latest - JOIN - releases AS releases - ON - latest.latestVersion=releases.version - AND latest.releaseVersion=releases.releaseVersion - AND latest.appname=releases.appname - LIMIT ? - `) - - span.SetTag("query", selectQuery) - - args := []any{} - args = append(args, app) - for _, version := range releaseVersions { - args = append(args, version) - } - args = append(args, uint64(len(releaseVersions))) - - rows, err := tx.QueryContext( - ctx, - selectQuery, - args..., - ) - - processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrePublishes, false) - if err != nil { - return nil, err - } - if len(processedRows) == 0 { - return nil, nil - } - return processedRows, nil -} - -func (h *DBHandler) DBSelectReleaseByVersion(ctx context.Context, tx *sql.Tx, app string, releaseVersion uint64, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { - selectQuery := h.AdaptQuery(fmt.Sprintf( - "SELECT created, appName, metadata, manifests, releaseVersion, deleted, environments " + - " FROM releases " + - " WHERE appName=? AND releaseVersion=?" + - " ORDER BY version DESC " + - " LIMIT 1;")) - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleaseByVersion") - defer span.Finish() - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - app, - releaseVersion, - ) - - processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true) - if err != nil { - return nil, err - } - if len(processedRows) == 0 { - return nil, nil - } - return processedRows[0], nil -} - -func (h *DBHandler) DBSelectReleaseByVersionAtTimestamp(ctx context.Context, tx *sql.Tx, app string, releaseVersion uint64, ignorePrepublishes bool, ts time.Time) (*DBReleaseWithMetaData, error) { - selectQuery := h.AdaptQuery(fmt.Sprintf( - "SELECT created, appName, metadata, manifests, releaseVersion, deleted, environments " + - " FROM releases " + - " WHERE appName=? AND releaseVersion=? AND created <= (?)" + - " ORDER BY version DESC " + - " LIMIT 1;")) - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleaseByVersion") - defer span.Finish() - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - app, - releaseVersion, - ts, - ) - - processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true) - if err != nil { - return nil, err - } - if len(processedRows) == 0 { - return nil, nil - } - return processedRows[0], nil -} - -func (h *DBHandler) DBSelectAllManifestsForAllReleases(ctx context.Context, tx *sql.Tx) (map[string]map[uint64][]string, error) { - selectQuery := h.AdaptQuery( - ` - SELECT DISTINCT - releases.appname, - releases.releaseVersion, - releases.environments - FROM ( - SELECT - MAX(version) AS latestRelease, - appname, - releaseversion - FROM - "releases" - GROUP BY - appname, releaseversion) AS latest - JOIN - releases AS releases - ON - latest.latestRelease=releases.version - AND latest.releaseVersion=releases.releaseVersion - AND latest.appname=releases.appname; -`) - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllManifestsForAllReleases") - defer span.Finish() - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - ) - - return h.processReleaseManifestRows(ctx, err, rows) -} - -func (h *DBHandler) processReleaseManifestRows(ctx context.Context, err error, rows *sql.Rows) (map[string]map[uint64][]string, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "processReleaseManifestRows") - defer span.Finish() - - if err != nil { - return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) - } - defer func(rows *sql.Rows) { - err := rows.Close() - if err != nil { - logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) - } - }(rows) - //exhaustruct:ignore - var result = make(map[string]map[uint64][]string) - for rows.Next() { - var environmentsStr sql.NullString - var appName string - var releaseVersion uint64 - err := rows.Scan(&appName, &releaseVersion, &environmentsStr) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) - } - environments := make([]string, 0) - if environmentsStr.Valid && environmentsStr.String != "" { - err = json.Unmarshal(([]byte)(environmentsStr.String), &environments) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of environments for releases. Error: %w. Data: %s\n", err, environmentsStr.String) - } - } - if _, exists := result[appName]; !exists { - result[appName] = make(map[uint64][]string) - } - result[appName][releaseVersion] = environments - } - err = closeRows(rows) - if err != nil { - return nil, err - } - return result, nil -} - -// DBSelectReleasesByAppLatestEslVersion returns the latest eslversion -// for each release of an app. It includes deleted releases and loads manifests. -func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppLatestEslVersion") - defer span.Finish() - selectQuery := h.AdaptQuery( - `SELECT - releases.created, - releases.appName, - releases.metadata, - releases.manifests, - releases.releaseVersion, - releases.deleted, - releases.environments - FROM ( - SELECT - MAX(version) AS latestVersion, - appname, - releaseversion - FROM - releases - WHERE - appname=? - GROUP BY - appname, releaseversion - ) as currentReleases - JOIN - releases - ON - currentReleases.appname = releases.appname - AND - currentReleases.latestVersion = releases.version - AND - currentReleases.releaseversion = releases.releaseversion - ORDER BY currentReleases.releaseversion DESC;`, - ) - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - app, - ) - - return h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true) -} - -func (h *DBHandler) DBSelectReleasesByAppOrderedByEslVersion(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppOrderedByEslVersion") - defer span.Finish() - selectQuery := h.AdaptQuery(` -SELECT - releases.created, - releases.appName, - releases.metadata, - releases.releaseVersion, - releases.deleted, - releases.environments -FROM ( - SELECT max(version) as latestVersion, appName, releaseVersion - FROM releases - WHERE appName=? - GROUP BY appName, releaseVersion - ) as c -JOIN releases - ON c.appName= releases.appName - AND c.latestVersion = releases.version - AND c.releaseVersion = releases.releaseVersion -ORDER BY c.releaseVersion DESC -LIMIT 1; -`, - ) - span.SetTag("query", selectQuery) - span.SetTag("appName", app) - rows, err := tx.QueryContext( - ctx, - selectQuery, - app, - ) - - result, err := h.processReleaseRows(ctx, err, rows, ignorePrepublishes, false) - span.SetTag("resultCount", len(result)) - if err != nil { - span.Finish(tracer.WithError(err)) - return nil, err - } - if result == nil { - return nil, nil - } - return result[0], nil -} - -func (h *DBHandler) DBSelectAllReleasesOfApp(ctx context.Context, tx *sql.Tx, app string) (*DBAllReleasesWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllReleasesOfApp") - defer span.Finish() - selectQuery := h.AdaptQuery(fmt.Sprintf( - "SELECT eslVersion, created, appName, metadata " + - " FROM all_releases " + - " WHERE appName=?" + - " ORDER BY eslVersion DESC " + - " LIMIT 1;")) - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - app, - ) - return h.processAllReleasesRow(ctx, err, rows) -} - -func (h *DBHandler) DBSelectAllReleasesOfAllApps(ctx context.Context, tx *sql.Tx) (map[string][]int64, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllReleasesOfAllApps") - defer span.Finish() - selectQuery := h.AdaptQuery(`SELECT - all_releases.appname, - all_releases.metadata - FROM ( - SELECT - MAX(eslVersion) AS latest, - appname - FROM - all_releases - GROUP BY - appname) AS latest - JOIN - all_releases AS all_releases - ON - latest.latest=all_releases.eslVersion - AND latest.appname=all_releases.appname;`) - - span.SetTag("query", selectQuery) - rows, err := tx.QueryContext( - ctx, - selectQuery, - ) - return h.processAllReleasesForAllAppsRow(ctx, err, rows) -} - -func (h *DBHandler) processAllReleasesForAllAppsRow(ctx context.Context, err error, rows *sql.Rows) (map[string][]int64, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "processAllReleasesForAllAppsRow") - defer span.Finish() - - if err != nil { - return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) - } - defer func(rows *sql.Rows) { - err := rows.Close() - if err != nil { - logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) - } - }(rows) - - var result = make(map[string][]int64) - for rows.Next() { - var appName string - var jsonReleases string - - err := rows.Scan(&appName, &jsonReleases) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) - } - var metaData = DBAllReleaseMetaData{ - Releases: []int64{}, - } - - err = json.Unmarshal(([]byte)(jsonReleases), &metaData) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of releases. Error: %w. Data: %s\n", err, jsonReleases) - } - result[appName] = metaData.Releases - } - - err = closeRows(rows) - if err != nil { - return nil, err - } - return result, nil -} - -func (h *DBHandler) processAllReleasesRow(ctx context.Context, err error, rows *sql.Rows) (*DBAllReleasesWithMetaData, error) { - if err != nil { - return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) - } - defer func(rows *sql.Rows) { - err := rows.Close() - if err != nil { - logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) - } - }(rows) - //exhaustruct:ignore - var row = &DBAllReleasesWithMetaData{} - if rows.Next() { - var metadataStr string - err := rows.Scan(&row.EslVersion, &row.Created, &row.App, &metadataStr) - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) - } - var metaData = DBAllReleaseMetaData{ - Releases: []int64{}, - } - err = json.Unmarshal(([]byte)(metadataStr), &metaData) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of releases. Error: %w. Data: %s\n", err, metadataStr) - } - row.Metadata = metaData - } else { - row = nil - } - err = closeRows(rows) - if err != nil { - return nil, err - } - return row, nil -} - -func (h *DBHandler) processReleaseRows(ctx context.Context, err error, rows *sql.Rows, ignorePrepublishes bool, withManifests bool) ([]*DBReleaseWithMetaData, error) { - span, ctx := tracer.StartSpanFromContext(ctx, "processReleaseRows") - defer span.Finish() - - if err != nil { - return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) - } - defer func(rows *sql.Rows) { - err := rows.Close() - if err != nil { - logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) - } - }(rows) - //exhaustruct:ignore - var result []*DBReleaseWithMetaData - var lastSeenRelease uint64 = 0 - for rows.Next() { - //exhaustruct:ignore - var row = &DBReleaseWithMetaData{} - var metadataStr string - var manifestStr string - var environmentsStr sql.NullString - var err error - if withManifests { - err = rows.Scan(&row.Created, &row.App, &metadataStr, &manifestStr, &row.ReleaseNumber, &row.Deleted, &environmentsStr) - } else { - err = rows.Scan(&row.Created, &row.App, &metadataStr /*manifests*/, &row.ReleaseNumber, &row.Deleted, &environmentsStr) - } - if err != nil { - if errors.Is(err, sql.ErrNoRows) { - return nil, nil - } - return nil, fmt.Errorf("Error scanning releases row from DB withManifests=%v. Error: %w\n", withManifests, err) - } - if row.ReleaseNumber != lastSeenRelease { - lastSeenRelease = row.ReleaseNumber - } else { - continue - } - // handle meta data - var metaData = DBReleaseMetaData{ - SourceAuthor: "", - SourceCommitId: "", - SourceMessage: "", - DisplayVersion: "", - UndeployVersion: false, - IsMinor: false, - CiLink: "", - IsPrepublish: false, - } - err = json.Unmarshal(([]byte)(metadataStr), &metaData) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of metadata for releases. Error: %w. Data: %s\n", err, metadataStr) - } - row.Metadata = metaData - - // handle manifests - var manifestData = DBReleaseManifests{ - Manifests: map[string]string{}, - } - if withManifests { - err = json.Unmarshal(([]byte)(manifestStr), &manifestData) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of manifests for releases. Error: %w. Data: %s\n", err, metadataStr) - } - } - row.Manifests = manifestData - environments := make([]string, 0) - if environmentsStr.Valid && environmentsStr.String != "" { - err = json.Unmarshal(([]byte)(environmentsStr.String), &environments) - if err != nil { - return nil, fmt.Errorf("Error during json unmarshal of environments for releases. Error: %w. Data: %s\n", err, environmentsStr.String) - } - } - row.Environments = environments - if ignorePrepublishes && row.Metadata.IsPrepublish { - continue - } - result = append(result, row) - } - err = closeRows(rows) - if err != nil { - return nil, err - } - return result, nil -} - -func (h *DBHandler) DBInsertRelease(ctx context.Context, transaction *sql.Tx, release DBReleaseWithMetaData) error { - span, ctx := tracer.StartSpanFromContext(ctx, "DBInsertRelease") - defer span.Finish() - metadataJson, err := json.Marshal(release.Metadata) - if err != nil { - return fmt.Errorf("insert release: could not marshal json data: %w", err) - } - insertQuery := h.AdaptQuery( - "INSERT INTO releases (created, releaseVersion, appName, manifests, metadata, deleted, environments) VALUES (?, ?, ?, ?, ?, ?, ?);", - ) - span.SetTag("query", insertQuery) - manifestJson, err := json.Marshal(release.Manifests) - if err != nil { - return fmt.Errorf("could not marshal json data: %w", err) - } - - envs := make([]string, 0) - for env := range release.Manifests.Manifests { - envs = append(envs, env) - } - release.Environments = envs - slices.Sort(release.Environments) // we don't really *need* the sorting, it's just for convenience - environmentStr, err := json.Marshal(release.Environments) - if err != nil { - return fmt.Errorf("could not marshal release environments: %w", err) - } - - now, err := h.DBReadTransactionTimestamp(ctx, transaction) - if err != nil { - return fmt.Errorf("DBInsertRelease unable to get transaction timestamp: %w", err) - } - _, err = transaction.Exec( - insertQuery, - *now, - release.ReleaseNumber, - release.App, - manifestJson, - metadataJson, - release.Deleted, - environmentStr, - ) - if err != nil { - return fmt.Errorf( - "could not insert release for app '%s' and version '%v' into DB. Error: %w\n", - release.App, - release.ReleaseNumber, - err) - } - - logger.FromContext(ctx).Sugar().Infof( - "inserted release: app '%s' and version '%v'", - release.App, - release.ReleaseNumber) - return nil -} - -func (h *DBHandler) DBDeleteReleaseFromAllReleases(ctx context.Context, transaction *sql.Tx, application string, releaseToDelete uint64) error { - span, ctx := tracer.StartSpanFromContext(ctx, "DBDeleteReleaseFromAllReleases") - defer span.Finish() - - allReleases, err := h.DBSelectAllReleasesOfApp(ctx, transaction, application) - if err != nil { - return err - } - if allReleases == nil { - logger.FromContext(ctx).Sugar().Infof("Could not find release '%d' for appliation '%s' to delete. No releases found.", releaseToDelete, application) - return nil - } - idxToDelete := slices.Index(allReleases.Metadata.Releases, int64(releaseToDelete)) - - if idxToDelete == -1 { - logger.FromContext(ctx).Sugar().Infof("Could not find release '%d' for appliation '%s' to delete.", releaseToDelete, application) - return nil //If we don't find it, not an error, but we do nothing - } - allReleases.Metadata.Releases = append(allReleases.Metadata.Releases[:idxToDelete], allReleases.Metadata.Releases[idxToDelete+1:]...) - if err := h.DBInsertAllReleases(ctx, transaction, application, allReleases.Metadata.Releases, allReleases.EslVersion); err != nil { - return err - } - return nil -} - -// DBClearReleases : Sets all_releases for app to empty list and marks every release as deleted -func (h *DBHandler) DBClearReleases(ctx context.Context, transaction *sql.Tx, application string) error { - span, ctx := tracer.StartSpanFromContext(ctx, "DBClearReleases") - defer span.Finish() - - allReleases, err := h.DBSelectAllReleasesOfApp(ctx, transaction, application) - if err != nil { - return err - } - if allReleases == nil { - logger.FromContext(ctx).Sugar().Infof("App %s does not contain any releases. No action taken", application) - return nil - } - for _, releaseToDelete := range allReleases.Metadata.Releases { - err = h.DBDeleteFromReleases(ctx, transaction, application, uint64(releaseToDelete)) - if err != nil { - return err - } - } - - return h.DBInsertAllReleases(ctx, transaction, application, []int64{}, allReleases.EslVersion) -} - -func (h *DBHandler) DBDeleteFromReleases(ctx context.Context, transaction *sql.Tx, application string, releaseToDelete uint64) error { - span, ctx := tracer.StartSpanFromContext(ctx, "DBDeleteFromReleases") - defer span.Finish() - - targetRelease, err := h.DBSelectReleaseByVersion(ctx, transaction, application, releaseToDelete, true) - if err != nil { - return err - } - - if targetRelease.Deleted { - logger.FromContext(ctx).Sugar().Infof("Release '%d' for application '%s' has already been deleted.", releaseToDelete, application) - return nil - } - - targetRelease.Deleted = true - if err := h.DBInsertRelease(ctx, transaction, *targetRelease); err != nil { - return err - } - return nil -} - -func (h *DBHandler) DBInsertAllReleases(ctx context.Context, transaction *sql.Tx, app string, allVersions []int64, previousEslVersion EslVersion) error { - span, ctx := tracer.StartSpanFromContext(ctx, "DBInsertAllReleases") - defer span.Finish() - slices.Sort(allVersions) - metadataJson, err := json.Marshal(DBAllReleaseMetaData{ - Releases: allVersions, - }) - if err != nil { - return fmt.Errorf("insert release: could not marshal json data: %w", err) - } - insertQuery := h.AdaptQuery( - "INSERT INTO all_releases (eslVersion, created, appName, metadata) VALUES (?, ?, ?, ?);", - ) - - now, err := h.DBReadTransactionTimestamp(ctx, transaction) - if err != nil { - return fmt.Errorf("DBInsertAllReleases unable to get transaction timestamp: %w", err) - } - - span.SetTag("query", insertQuery) - - _, err = transaction.Exec( - insertQuery, - previousEslVersion+1, - *now, - app, - metadataJson, - ) - if err != nil { - return fmt.Errorf("could not insert all_releases for app '%s' and esl '%v' into DB. Error: %w\n", app, previousEslVersion+1, err) - } - logger.FromContext(ctx).Sugar().Infof("inserted all_releases for app '%s'", app) - return nil -} - // APPS func (h *DBHandler) DBWriteAllApplications(ctx context.Context, transaction *sql.Tx, previousVersion int64, applications []string) error { @@ -5845,7 +5078,7 @@ func (h *DBHandler) RunCustomMigrationReleaseEnvironments(ctx context.Context) e } logger.FromContext(ctx).Sugar().Infof("updating %d releases environments", len(releasesWithoutEnvironments)) for _, release := range releasesWithoutEnvironments { - err = h.DBInsertRelease(ctx, transaction, *release) + err = h.DBUpdateOrCreateRelease(ctx, transaction, *release) if err != nil { return fmt.Errorf("could not insert release, error: %w", err) } diff --git a/pkg/db/db_releases.go b/pkg/db/db_releases.go new file mode 100644 index 000000000..cb1e94bd4 --- /dev/null +++ b/pkg/db/db_releases.go @@ -0,0 +1,635 @@ +/*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 . + +Copyright freiheit.com*/ + +package db + +import ( + "context" + "database/sql" + "encoding/json" + "errors" + "fmt" + "github.com/freiheit-com/kuberpult/pkg/logger" + "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" + "slices" + "strings" + "time" +) + +type DBReleaseMetaData struct { + SourceAuthor string + SourceCommitId string + SourceMessage string + DisplayVersion string + UndeployVersion bool + IsMinor bool + CiLink string + IsPrepublish bool +} + +type DBReleaseManifests struct { + Manifests map[string]string +} + +type DBReleaseWithMetaData struct { + ReleaseNumber uint64 + Created time.Time + App string + Manifests DBReleaseManifests + Metadata DBReleaseMetaData + Environments []string +} + +// SELECTS + +func (h *DBHandler) DBSelectAnyRelease(ctx context.Context, tx *sql.Tx, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAnyRelease") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, releaseVersion, environments + FROM releases + LIMIT 1; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext(ctx, selectQuery) + return h.processReleaseRow(ctx, err, rows, ignorePrepublishes, false) +} + +func (h *DBHandler) DBSelectReleasesWithoutEnvironments(ctx context.Context, tx *sql.Tx) ([]*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesWithoutEnvironments") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, manifests, releaseVersion, environments + FROM releases + WHERE COALESCE(environments, '') = '' AND COALESCE(manifests, '') != '' + LIMIT 100; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext(ctx, selectQuery) + return h.processReleaseRows(ctx, err, rows, true, true) +} + +func (h *DBHandler) DBSelectReleasesByVersions(ctx context.Context, tx *sql.Tx, app string, releaseVersions []uint64, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByVersions") + defer span.Finish() + if len(releaseVersions) == 0 { + return []*DBReleaseWithMetaData{}, nil + } + repeatedQuestionMarks := strings.Repeat(",?", len(releaseVersions)-1) + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, releaseVersion, environments FROM releases + WHERE appname=? AND releaseversion IN (?` + repeatedQuestionMarks + `) + LIMIT ? + `) + span.SetTag("query", selectQuery) + + args := []any{} + args = append(args, app) + for _, version := range releaseVersions { + args = append(args, version) + } + args = append(args, uint64(len(releaseVersions))) + rows, err := tx.QueryContext( + ctx, + selectQuery, + args..., + ) + return h.processReleaseRows(ctx, err, rows, ignorePrepublishes, false) +} + +func (h *DBHandler) DBSelectReleaseByVersion(ctx context.Context, tx *sql.Tx, app string, releaseVersion uint64, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleaseByVersion") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, manifests, releaseVersion, environments + FROM releases + WHERE appName=? AND releaseVersion=? + LIMIT 1; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + app, + releaseVersion, + ) + return h.processReleaseRow(ctx, err, rows, ignorePrepublishes, true) +} + +func (h *DBHandler) DBSelectReleaseByVersionAtTimestamp(ctx context.Context, tx *sql.Tx, app string, releaseVersion uint64, ignorePrepublishes bool, ts time.Time) (*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleaseByVersionAtTimestamp") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, manifests, releaseVersion, environments + FROM releases_history + WHERE appName=? AND releaseVersion=? AND created <= (?) + ORDER BY version DESC + LIMIT 1; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + app, + releaseVersion, + ts, + ) + return h.processReleaseRow(ctx, err, rows, ignorePrepublishes, true) +} + +func (h *DBHandler) DBSelectAllManifestsForAllReleases(ctx context.Context, tx *sql.Tx) (map[string]map[uint64][]string, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllManifestsForAllReleases") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT appname, releaseVersion, environments + FROM releases; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + ) + + return h.processReleaseManifestRows(ctx, err, rows) +} + +func (h *DBHandler) DBSelectReleasesByAppLatestEslVersion(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) ([]*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectReleasesByAppLatestEslVersion") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, manifests, releaseVersion, environments + FROM releases + WHERE appname=? + ORDER BY releaseversion DESC; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + app, + ) + + return h.processReleaseRows(ctx, err, rows, ignorePrepublishes, true) +} + +func (h *DBHandler) DBSelectLatestReleaseOfApp(ctx context.Context, tx *sql.Tx, app string, ignorePrepublishes bool) (*DBReleaseWithMetaData, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectLatestReleaseOfApp") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT created, appName, metadata, releaseVersion, environments + FROM releases + WHERE appName=? + ORDER BY releaseVersion DESC + LIMIT 1; + `) + span.SetTag("query", selectQuery) + span.SetTag("appName", app) + rows, err := tx.QueryContext( + ctx, + selectQuery, + app, + ) + + return h.processReleaseRow(ctx, err, rows, ignorePrepublishes, false) +} + +func (h *DBHandler) DBSelectAllReleasesOfApp(ctx context.Context, tx *sql.Tx, app string) ([]int64, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllReleasesOfApp") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT releaseVersion + FROM releases + WHERE appName=? + ORDER BY releaseVersion; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + app, + ) + return h.processAppReleaseVersionsRows(ctx, err, rows) +} + +func (h *DBHandler) DBSelectAllReleasesOfAllApps(ctx context.Context, tx *sql.Tx) (map[string][]int64, error) { + span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAllReleasesOfAllApps") + defer span.Finish() + selectQuery := h.AdaptQuery(` + SELECT appname, releaseVersion + FROM releases; + `) + span.SetTag("query", selectQuery) + rows, err := tx.QueryContext( + ctx, + selectQuery, + ) + return h.processAllAppsReleaseVersionsRows(ctx, err, rows) +} + +// INSERT, UPDATE, DELETES + +func (h *DBHandler) DBUpdateOrCreateRelease(ctx context.Context, transaction *sql.Tx, release DBReleaseWithMetaData) error { + span, ctx := tracer.StartSpanFromContext(ctx, "DBUpdateOrCreateRelease") + defer span.Finish() + err := h.upsertReleaseRow(ctx, transaction, release) + if err != nil { + return err + } + err = h.insertReleaseHistoryRow(ctx, transaction, release, false) + if err != nil { + return err + } + return nil +} + +func (h *DBHandler) DBDeleteFromReleases(ctx context.Context, transaction *sql.Tx, application string, releaseToDelete uint64) error { + span, ctx := tracer.StartSpanFromContext(ctx, "DBDeleteFromReleases") + defer span.Finish() + + targetRelease, err := h.DBSelectReleaseByVersion(ctx, transaction, application, releaseToDelete, true) + if err != nil { + return err + } + if targetRelease == nil { + return nil + } + err = h.deleteReleaseRow(ctx, transaction, *targetRelease) + if err != nil { + return err + } + err = h.insertReleaseHistoryRow(ctx, transaction, *targetRelease, true) + if err != nil { + return err + } + return nil +} + +func (h *DBHandler) DBClearReleases(ctx context.Context, transaction *sql.Tx, application string) error { + span, ctx := tracer.StartSpanFromContext(ctx, "DBClearReleases") + defer span.Finish() + + allReleases, err := h.DBSelectAllReleasesOfApp(ctx, transaction, application) + if err != nil { + return err + } + if allReleases == nil { + logger.FromContext(ctx).Sugar().Infof("App %s does not contain any releases. No action taken", application) + return nil + } + for _, releaseToDelete := range allReleases { + err = h.DBDeleteFromReleases(ctx, transaction, application, uint64(releaseToDelete)) + if err != nil { + return err + } + } + + return nil +} + +// actual changes in tables + +func (h *DBHandler) deleteReleaseRow(ctx context.Context, transaction *sql.Tx, release DBReleaseWithMetaData) error { + span, _ := tracer.StartSpanFromContext(ctx, "deleteReleaseRow") + defer span.Finish() + deleteQuery := h.AdaptQuery(` + DELETE FROM releases WHERE appname=? AND releaseversion=? + `) + span.SetTag("query", deleteQuery) + _, err := transaction.Exec( + deleteQuery, + release.App, + release.ReleaseNumber, + ) + if err != nil { + return fmt.Errorf( + "could not delete release for app '%s' and version '%v' from DB. Error: %w\n", + release.App, + release.ReleaseNumber, + err) + } + return nil +} + +func (h *DBHandler) upsertReleaseRow(ctx context.Context, transaction *sql.Tx, release DBReleaseWithMetaData) error { + span, ctx := tracer.StartSpanFromContext(ctx, "upsertReleaseRow") + defer span.Finish() + upsertQuery := h.AdaptQuery(` + INSERT INTO releases (created, releaseVersion, appName, manifests, metadata, environments) + VALUES (?, ?, ?, ?, ?, ?) + ON CONFLICT(releaseVersion, appname) + DO UPDATE SET created = excluded.created, manifests = excluded.manifests, metadata = excluded.metadata, environments = excluded.environments; + `) + span.SetTag("query", upsertQuery) + metadataJson, err := json.Marshal(release.Metadata) + if err != nil { + return fmt.Errorf("upsert release: could not marshal json data: %w", err) + } + manifestJson, err := json.Marshal(release.Manifests) + if err != nil { + return fmt.Errorf("could not marshal json data: %w", err) + } + + envs := make([]string, 0) + for env := range release.Manifests.Manifests { + envs = append(envs, env) + } + release.Environments = envs + slices.Sort(release.Environments) + environmentStr, err := json.Marshal(release.Environments) + if err != nil { + return fmt.Errorf("could not marshal release environments: %w", err) + } + + now, err := h.DBReadTransactionTimestamp(ctx, transaction) + if err != nil { + return fmt.Errorf("DBInsertRelease unable to get transaction timestamp: %w", err) + } + _, err = transaction.Exec( + upsertQuery, + *now, + release.ReleaseNumber, + release.App, + manifestJson, + metadataJson, + environmentStr, + ) + if err != nil { + return fmt.Errorf( + "could not insert release for app '%s' and version '%v' into DB. Error: %w\n", + release.App, + release.ReleaseNumber, + err) + } + return nil +} + +func (h *DBHandler) insertReleaseHistoryRow(ctx context.Context, transaction *sql.Tx, release DBReleaseWithMetaData, deleted bool) error { + span, ctx := tracer.StartSpanFromContext(ctx, "insertReleaseHistoryRow") + defer span.Finish() + insertQuery := h.AdaptQuery(` + INSERT INTO releases_history (created, releaseVersion, appName, manifests, metadata, deleted, environments) + VALUES (?, ?, ?, ?, ?, ?, ?); + `) + span.SetTag("query", insertQuery) + metadataJson, err := json.Marshal(release.Metadata) + if err != nil { + return fmt.Errorf("insert release: could not marshal json data: %w", err) + } + manifestJson, err := json.Marshal(release.Manifests) + if err != nil { + return fmt.Errorf("could not marshal json data: %w", err) + } + + envs := make([]string, 0) + for env := range release.Manifests.Manifests { + envs = append(envs, env) + } + release.Environments = envs + slices.Sort(release.Environments) + environmentStr, err := json.Marshal(release.Environments) + if err != nil { + return fmt.Errorf("could not marshal release environments: %w", err) + } + + now, err := h.DBReadTransactionTimestamp(ctx, transaction) + if err != nil { + return fmt.Errorf("DBInsertRelease unable to get transaction timestamp: %w", err) + } + _, err = transaction.Exec( + insertQuery, + *now, + release.ReleaseNumber, + release.App, + manifestJson, + metadataJson, + deleted, + environmentStr, + ) + if err != nil { + return fmt.Errorf( + "could not insert release_history for app '%s' and version '%v' into DB. Error: %w\n", + release.App, + release.ReleaseNumber, + err) + } + return nil +} + +// process rows functions + +func (h *DBHandler) processReleaseRow(ctx context.Context, err error, rows *sql.Rows, ignorePrepublishes bool, withManifests bool) (*DBReleaseWithMetaData, error) { + processedRows, err := h.processReleaseRows(ctx, err, rows, ignorePrepublishes, withManifests) + if err != nil { + return nil, err + } + if len(processedRows) == 0 { + return nil, nil + } + return processedRows[0], nil +} + +func (h *DBHandler) processReleaseRows(ctx context.Context, err error, rows *sql.Rows, ignorePrepublishes bool, withManifests bool) ([]*DBReleaseWithMetaData, error) { + if err != nil { + return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) + } + defer func(rows *sql.Rows) { + err := rows.Close() + if err != nil { + logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) + } + }(rows) + //exhaustruct:ignore + var result []*DBReleaseWithMetaData + var lastSeenRelease uint64 = 0 + for rows.Next() { + //exhaustruct:ignore + var row = &DBReleaseWithMetaData{} + var metadataStr string + var manifestStr string + var environmentsStr sql.NullString + var err error + if withManifests { + err = rows.Scan(&row.Created, &row.App, &metadataStr, &manifestStr, &row.ReleaseNumber, &environmentsStr) + } else { + err = rows.Scan(&row.Created, &row.App, &metadataStr /*manifests*/, &row.ReleaseNumber, &environmentsStr) + } + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("Error scanning releases row from DB withManifests=%v. Error: %w\n", withManifests, err) + } + if row.ReleaseNumber != lastSeenRelease { + lastSeenRelease = row.ReleaseNumber + } else { + continue + } + // handle meta data + var metaData = DBReleaseMetaData{ + SourceAuthor: "", + SourceCommitId: "", + SourceMessage: "", + DisplayVersion: "", + UndeployVersion: false, + IsMinor: false, + CiLink: "", + IsPrepublish: false, + } + err = json.Unmarshal(([]byte)(metadataStr), &metaData) + if err != nil { + return nil, fmt.Errorf("Error during json unmarshal of metadata for releases. Error: %w. Data: %s\n", err, metadataStr) + } + row.Metadata = metaData + + // handle manifests + var manifestData = DBReleaseManifests{ + Manifests: map[string]string{}, + } + if withManifests { + err = json.Unmarshal(([]byte)(manifestStr), &manifestData) + if err != nil { + return nil, fmt.Errorf("Error during json unmarshal of manifests for releases. Error: %w. Data: %s\n", err, metadataStr) + } + } + row.Manifests = manifestData + environments := make([]string, 0) + if environmentsStr.Valid && environmentsStr.String != "" { + err = json.Unmarshal(([]byte)(environmentsStr.String), &environments) + if err != nil { + return nil, fmt.Errorf("Error during json unmarshal of environments for releases. Error: %w. Data: %s\n", err, environmentsStr.String) + } + } + row.Environments = environments + if ignorePrepublishes && row.Metadata.IsPrepublish { + continue + } + result = append(result, row) + } + err = closeRows(rows) + if err != nil { + return nil, err + } + return result, nil +} + +func (h *DBHandler) processReleaseManifestRows(ctx context.Context, err error, rows *sql.Rows) (map[string]map[uint64][]string, error) { + if err != nil { + return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) + } + defer func(rows *sql.Rows) { + err := rows.Close() + if err != nil { + logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) + } + }(rows) + //exhaustruct:ignore + var result = make(map[string]map[uint64][]string) + for rows.Next() { + var environmentsStr sql.NullString + var appName string + var releaseVersion uint64 + err := rows.Scan(&appName, &releaseVersion, &environmentsStr) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) + } + environments := make([]string, 0) + if environmentsStr.Valid && environmentsStr.String != "" { + err = json.Unmarshal(([]byte)(environmentsStr.String), &environments) + if err != nil { + return nil, fmt.Errorf("Error during json unmarshal of environments for releases. Error: %w. Data: %s\n", err, environmentsStr.String) + } + } + if _, exists := result[appName]; !exists { + result[appName] = make(map[uint64][]string) + } + result[appName][releaseVersion] = environments + } + err = closeRows(rows) + if err != nil { + return nil, err + } + return result, nil +} + +func (h *DBHandler) processAppReleaseVersionsRows(ctx context.Context, err error, rows *sql.Rows) ([]int64, error) { + if err != nil { + return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) + } + defer func(rows *sql.Rows) { + err := rows.Close() + if err != nil { + logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) + } + }(rows) + result := []int64{} + //exhaustruct:ignore + var row int64 + for rows.Next() { + err := rows.Scan(&row) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) + } + result = append(result, row) + } + err = closeRows(rows) + if err != nil { + return nil, err + } + return result, nil +} + +func (h *DBHandler) processAllAppsReleaseVersionsRows(ctx context.Context, err error, rows *sql.Rows) (map[string][]int64, error) { + if err != nil { + return nil, fmt.Errorf("could not query releases table from DB. Error: %w\n", err) + } + defer func(rows *sql.Rows) { + err := rows.Close() + if err != nil { + logger.FromContext(ctx).Sugar().Warnf("releases: row could not be closed: %v", err) + } + }(rows) + + var result = make(map[string][]int64) + for rows.Next() { + var appName string + var releaseVersion int64 + + err := rows.Scan(&appName, &releaseVersion) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return nil, nil + } + return nil, fmt.Errorf("Error scanning releases row from DB. Error: %w\n", err) + } + + if _, ok := result[appName]; !ok { + result[appName] = []int64{} + } + result[appName] = append(result[appName], releaseVersion) + } + + err = closeRows(rows) + if err != nil { + return nil, err + } + return result, nil +} diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go index 8ae459e08..d3e6f9d65 100644 --- a/pkg/db/db_test.go +++ b/pkg/db/db_test.go @@ -204,7 +204,6 @@ func TestCustomMigrationReleases(t *testing.T) { }, }, } - releaseNumbers := []int64{} for _, r := range releases { dbRelease := DBReleaseWithMetaData{ Created: time.Now().UTC(), @@ -220,17 +219,11 @@ func TestCustomMigrationReleases(t *testing.T) { SourceMessage: r.SourceMessage, DisplayVersion: r.DisplayVersion, }, - Deleted: false, } - err := dbHandler.DBInsertRelease(ctx, transaction, dbRelease) + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, dbRelease) if err != nil { return fmt.Errorf("error writing Release to DB for app %s: %v", app, err) } - releaseNumbers = append(releaseNumbers, int64(r.Version)) - } - err := dbHandler.DBInsertAllReleases(ctx, transaction, app, releaseNumbers, InitialEslVersion-1) - if err != nil { - return fmt.Errorf("error writing all_releases to DB for app %s: %v", app, err) } return nil } @@ -2494,7 +2487,6 @@ func TestDeleteRelease(t *testing.T) { tcs := []struct { Name string toInsert DBReleaseWithMetaData - expected DBReleaseWithMetaData }{ { Name: "Delete Release from database", @@ -2511,23 +2503,6 @@ func TestDeleteRelease(t *testing.T) { SourceMessage: "message", DisplayVersion: "1.0.0", }, - Deleted: false, - }, - expected: DBReleaseWithMetaData{ - Created: time.Now(), - ReleaseNumber: 1, - App: "app", - Manifests: DBReleaseManifests{ - Manifests: map[string]string{"development": "development"}, - }, - Metadata: DBReleaseMetaData{ - SourceAuthor: "me", - SourceCommitId: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", - SourceMessage: "message", - DisplayVersion: "1.0.0", - }, - Deleted: true, - Environments: []string{"development"}, }, }, } @@ -2540,11 +2515,7 @@ func TestDeleteRelease(t *testing.T) { dbHandler := setupDB(t) err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { - err2 := dbHandler.DBInsertRelease(ctx, transaction, tc.toInsert) - if err2 != nil { - return err2 - } - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, tc.toInsert.App, []int64{int64(tc.toInsert.ReleaseNumber)}, 0) + err2 := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, tc.toInsert) if err2 != nil { return err2 } @@ -2554,30 +2525,22 @@ func TestDeleteRelease(t *testing.T) { t.Fatalf("error: %v\n", errDelete) } - errDelete2 := dbHandler.DBDeleteReleaseFromAllReleases(ctx, transaction, tc.toInsert.App, tc.toInsert.ReleaseNumber) - if errDelete2 != nil { - return errDelete2 - } - allReleases, err := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, tc.toInsert.App) if err != nil { return err } - if len(allReleases.Metadata.Releases) != 0 { - t.Fatalf("number of team locks mismatch (-want, +got):\n%d", len(allReleases.Metadata.Releases)) + if len(allReleases) != 0 { + t.Fatalf("number of team locks mismatch (-want, +got):\n%d", len(allReleases)) } latestRelease, err := dbHandler.DBSelectReleaseByVersion(ctx, transaction, tc.toInsert.App, tc.toInsert.ReleaseNumber, true) if err != nil { return err } - if !latestRelease.Deleted { + if latestRelease != nil { t.Fatalf("Release has not beed deleted\n") } - if diff := cmp.Diff(&tc.expected, latestRelease, cmpopts.IgnoreFields(DBReleaseWithMetaData{}, "Created")); diff != "" { - t.Fatalf("team locks mismatch (-want, +got):\n%s", diff) - } return nil }) if err != nil { @@ -3297,32 +3260,6 @@ func TestReadReleasesByApp(t *testing.T) { }, }, }, - { - Name: "Retrieve deleted release", - Releases: []DBReleaseWithMetaData{ - { - ReleaseNumber: 10, - App: "app1", - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}}, - }, - { - ReleaseNumber: 10, - App: "app1", - Deleted: true, - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}}, - }, - }, - AppName: "app1", - Expected: []*DBReleaseWithMetaData{ - { - ReleaseNumber: 10, - Deleted: true, - App: "app1", - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}}, - Environments: []string{"dev"}, - }, - }, - }, { Name: "Retrieve multiple releases", Releases: []DBReleaseWithMetaData{ @@ -3508,7 +3445,7 @@ func TestReadReleasesByApp(t *testing.T) { err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { for _, release := range tc.Releases { - err := dbHandler.DBInsertRelease(ctx, transaction, release) + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return fmt.Errorf("error while writing release, error: %w", err) } @@ -3665,7 +3602,7 @@ func TestReadReleasesByVersion(t *testing.T) { err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { for _, release := range tc.Releases { - err := dbHandler.DBInsertRelease(ctx, transaction, release) + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return fmt.Errorf("error while writing release, error: %w", err) } @@ -3742,10 +3679,10 @@ func TestReadAllReleasesOfAllApps(t *testing.T) { } err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { - for app, releases := range allReleases { - err := dbHandler.DBInsertAllReleases(ctx, transaction, app, releases, 1) + for _, release := range tc.Releases { + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { - return fmt.Errorf("error while writing releases, error: %w", err) + return err } } releases, err := dbHandler.DBSelectAllReleasesOfAllApps(ctx, transaction) @@ -3845,7 +3782,7 @@ func TestReadAllManifestsAllReleases(t *testing.T) { err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { for _, release := range tc.Releases { - err := dbHandler.DBInsertRelease(ctx, transaction, release) + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return fmt.Errorf("error while writing release, error: %w", err) } @@ -4174,7 +4111,7 @@ func TestFindEnvAppsFromReleases(t *testing.T) { err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error { for _, release := range tc.Releases { - err := dbHandler.DBInsertRelease(ctx, transaction, release) + err := dbHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return err } @@ -4294,31 +4231,6 @@ func TestReadReleasesWithoutEnvironments(t *testing.T) { }, }, }, - { - Name: "Retrieve only latest releases without envionments", - Releases: []DBReleaseWithMetaData{ - { - ReleaseNumber: 1, - App: "app1", - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1"}}, - Environments: []string{"dev"}, - }, - { - ReleaseNumber: 1, - App: "app1", - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1", "staging": "manifest2"}}, - }, - }, - AppName: "app1", - Expected: []*DBReleaseWithMetaData{ - { - ReleaseNumber: 1, - App: "app1", - Manifests: DBReleaseManifests{Manifests: map[string]string{"dev": "manifest1", "staging": "manifest2"}}, - Environments: []string{}, - }, - }, - }, } for _, tc := range tcs { @@ -4360,7 +4272,7 @@ func (h *DBHandler) DBInsertReleaseWithoutEnvironment(ctx context.Context, trans return fmt.Errorf("insert release: could not marshal json data: %w", err) } insertQuery := h.AdaptQuery( - "INSERT INTO releases (created, releaseVersion, appName, manifests, metadata, deleted, environments) VALUES (?, ?, ?, ?, ?, ?, ?);", + "INSERT INTO releases (created, releaseVersion, appName, manifests, metadata, environments) VALUES (?, ?, ?, ?, ?, ?);", ) manifestJson, err := json.Marshal(release.Manifests) if err != nil { @@ -4380,7 +4292,6 @@ func (h *DBHandler) DBInsertReleaseWithoutEnvironment(ctx context.Context, trans release.App, manifestJson, metadataJson, - release.Deleted, environmentStr, ) if err != nil { diff --git a/services/cd-service/pkg/repository/repository.go b/services/cd-service/pkg/repository/repository.go index d792421d4..c30958d23 100644 --- a/services/cd-service/pkg/repository/repository.go +++ b/services/cd-service/pkg/repository/repository.go @@ -2785,7 +2785,6 @@ func (s *State) WriteAllReleases(ctx context.Context, transaction *sql.Tx, app s if err != nil { return fmt.Errorf("cannot get releases of app %s: %v", app, err) } - releaseNumbers := []int64{} for i := range releases { releaseVersion := releases[i] repoRelease, err := s.GetApplicationReleaseFromManifest(app, releaseVersion) @@ -2831,32 +2830,26 @@ func (s *State) WriteAllReleases(ctx context.Context, transaction *sql.Tx, app s IsPrepublish: false, CiLink: "", }, - Deleted: false, Environments: []string{}, } - err = dbHandler.DBInsertRelease(ctx, transaction, dbRelease) + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, dbRelease) if err != nil { return fmt.Errorf("error writing Release to DB for app %s: %v", app, err) } - releaseNumbers = append(releaseNumbers, int64(repoRelease.Version)) - } - err = dbHandler.DBInsertAllReleases(ctx, transaction, app, releaseNumbers, db.InitialEslVersion-1) - if err != nil { - return fmt.Errorf("error writing all_releases to DB for app %s: %v", app, err) } return nil } func (s *State) GetAllApplicationReleases(ctx context.Context, transaction *sql.Tx, application string) ([]uint64, error) { if s.DBHandler.ShouldUseOtherTables() { - app, err := s.DBHandler.DBSelectAllReleasesOfApp(ctx, transaction, application) + releases, err := s.DBHandler.DBSelectAllReleasesOfApp(ctx, transaction, application) if err != nil { return nil, fmt.Errorf("could not get all releases of app %s: %v", application, err) } - if app == nil { + if releases == nil { return nil, fmt.Errorf("could not get all releases of app %s (nil)", application) } - res := conversion.ToUint64Slice(app.Metadata.Releases) + res := conversion.ToUint64Slice(releases) return res, nil } else { return s.GetAllApplicationReleasesFromManifest(application) diff --git a/services/cd-service/pkg/repository/transformer.go b/services/cd-service/pkg/repository/transformer.go index 8d2fa251f..2d811cef8 100644 --- a/services/cd-service/pkg/repository/transformer.go +++ b/services/cd-service/pkg/repository/transformer.go @@ -519,11 +519,11 @@ func (s *State) GetLastRelease(ctx context.Context, transaction *sql.Tx, fs bill if err != nil { return 0, fmt.Errorf("could not get releases of app %s: %v", application, err) } - if releases == nil || len(releases.Metadata.Releases) == 0 { + if len(releases) == 0 { return 0, nil } - l := len(releases.Metadata.Releases) - return uint64(releases.Metadata.Releases[l-1]), nil + l := len(releases) + return uint64(releases[l-1]), nil } else { return GetLastReleaseFromFile(fs, application) } @@ -763,32 +763,8 @@ func (c *CreateApplicationVersion) Transform( }, Environments: []string{}, Created: *now, - Deleted: false, - } - err = state.DBHandler.DBInsertRelease(ctx, transaction, release) - if err != nil { - return "", GetCreateReleaseGeneralFailure(err) - } - allReleases, err := state.DBHandler.DBSelectAllReleasesOfApp(ctx, transaction, c.Application) - if err != nil { - return "", GetCreateReleaseGeneralFailure(err) - } - if allReleases == nil { - - allReleases = &db.DBAllReleasesWithMetaData{ - EslVersion: db.InitialEslVersion - 1, - Created: *now, - App: c.Application, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{int64(release.ReleaseNumber)}, - }, - } - } else { - allReleases.Metadata.Releases = append(allReleases.Metadata.Releases, int64(release.ReleaseNumber)) - } - if !c.IsPrepublish { - err = state.DBHandler.DBInsertAllReleases(ctx, transaction, c.Application, allReleases.Metadata.Releases, allReleases.EslVersion) } + err = state.DBHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return "", GetCreateReleaseGeneralFailure(err) } @@ -874,14 +850,13 @@ func (c *CreateApplicationVersion) Transform( } func (c *CreateApplicationVersion) checkMinorFlags(ctx context.Context, transaction *sql.Tx, dbHandler *db.DBHandler, version uint64, minorRegexes []*regexp.Regexp) (bool, error) { - allReleases, err := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, c.Application) + releaseVersions, err := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, c.Application) if err != nil { return false, err } - if allReleases == nil { - return false, err + if releaseVersions == nil { + return false, nil } - releaseVersions := allReleases.Metadata.Releases sort.Slice(releaseVersions, func(i, j int) bool { return releaseVersions[i] > releaseVersions[j] }) nextVersion := int64(-1) previousVersion := int64(-1) @@ -906,7 +881,7 @@ func (c *CreateApplicationVersion) checkMinorFlags(ctx context.Context, transact return false, fmt.Errorf("next release exists in the all releases but not in the release table!") } nextRelease.Metadata.IsMinor = compareManifests(ctx, c.Manifests, nextRelease.Manifests.Manifests, minorRegexes) - err = dbHandler.DBInsertRelease(ctx, transaction, *nextRelease) + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, *nextRelease) if err != nil { return false, err } @@ -1288,8 +1263,8 @@ func isLatestVersion(ctx context.Context, transaction *sql.Tx, state *State, app if all == nil { rels = make([]uint64, 0) } else { - rels = make([]uint64, len(all.Metadata.Releases)) - for idx, rel := range all.Metadata.Releases { + rels = make([]uint64, len(all)) + for idx, rel := range all { rels[idx] = uint64(rel) } } @@ -1366,29 +1341,8 @@ func (c *CreateUndeployApplicationVersion) Transform( }, Environments: []string{}, Created: *now, - Deleted: false, - } - err = state.DBHandler.DBInsertRelease(ctx, transaction, release) - if err != nil { - return "", GetCreateReleaseGeneralFailure(err) } - allReleases, err := state.DBHandler.DBSelectAllReleasesOfApp(ctx, transaction, c.Application) - if err != nil { - return "", GetCreateReleaseGeneralFailure(err) - } - if allReleases == nil { - allReleases = &db.DBAllReleasesWithMetaData{ - EslVersion: db.InitialEslVersion - 1, - Created: *now, - App: c.Application, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{int64(release.ReleaseNumber)}, - }, - } - } else { - allReleases.Metadata.Releases = append(allReleases.Metadata.Releases, int64(release.ReleaseNumber)) - } - err = state.DBHandler.DBInsertAllReleases(ctx, transaction, c.Application, allReleases.Metadata.Releases, allReleases.EslVersion) + err = state.DBHandler.DBUpdateOrCreateRelease(ctx, transaction, release) if err != nil { return "", GetCreateReleaseGeneralFailure(err) } @@ -1805,10 +1759,9 @@ func (u *DeleteEnvFromApp) Transform( Created: *now, Manifests: db.DBReleaseManifests{Manifests: newManifests}, Metadata: dbReleaseWithMetadata.Metadata, - Deleted: dbReleaseWithMetadata.Deleted, Environments: []string{}, } - err = state.DBHandler.DBInsertRelease(ctx, transaction, newRelease) + err = state.DBHandler.DBUpdateOrCreateRelease(ctx, transaction, newRelease) if err != nil { return "", err } @@ -1942,10 +1895,6 @@ func (c *CleanupOldApplicationVersions) Transform( msg := "" for _, oldRelease := range oldVersions { if state.DBHandler.ShouldUseOtherTables() { - // delete release from all_releases - if err := state.DBHandler.DBDeleteReleaseFromAllReleases(ctx, transaction, c.Application, oldRelease); err != nil { - return "", err - } //'Delete' from releases table if err := state.DBHandler.DBDeleteFromReleases(ctx, transaction, c.Application, oldRelease); err != nil { return "", err diff --git a/services/cd-service/pkg/repository/transformer_db_test.go b/services/cd-service/pkg/repository/transformer_db_test.go index 545ba4fc1..0677ec385 100644 --- a/services/cd-service/pkg/repository/transformer_db_test.go +++ b/services/cd-service/pkg/repository/transformer_db_test.go @@ -640,7 +640,7 @@ func TestCreateApplicationVersionDB(t *testing.T) { Name string Transformers []Transformer expectedDbContent *db.DBAppWithMetaData - expectedDbReleases *db.DBAllReleasesWithMetaData + expectedDbReleases []int64 }{ { Name: "create one version", @@ -664,14 +664,7 @@ func TestCreateApplicationVersionDB(t *testing.T) { Team: "", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 1, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10000}, - }, - }, + expectedDbReleases: []int64{10000}, }, { Name: "create two versions, same team", @@ -704,14 +697,7 @@ func TestCreateApplicationVersionDB(t *testing.T) { Team: "noteam", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 2, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10, 11}, - }, - }, + expectedDbReleases: []int64{10, 11}, }, { Name: "create two versions, different teams", @@ -744,14 +730,7 @@ func TestCreateApplicationVersionDB(t *testing.T) { Team: "new", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 2, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10, 11}, - }, - }, + expectedDbReleases: []int64{10, 11}, }, } for _, tc := range tcs { @@ -777,7 +756,7 @@ func TestCreateApplicationVersionDB(t *testing.T) { if err3 != nil { return fmt.Errorf("error: %v", err3) } - if diff := cmp.Diff(tc.expectedDbReleases, actualRelease, cmpopts.IgnoreFields(db.DBAllReleasesWithMetaData{}, "Created")); diff != "" { + if diff := cmp.Diff(tc.expectedDbReleases, actualRelease); diff != "" { t.Errorf("error mismatch (-want, +got):\n%s", diff) } environment, err4 := state.DBHandler.DBSelectEnvironment(ctx, transaction, "acceptance") @@ -1510,7 +1489,7 @@ func TestCleanupOldVersionDB(t *testing.T) { if err2 != nil { return fmt.Errorf("error: %v", err2) } - if diff := cmp.Diff(tc.ExpectedActiveReleases, res.Metadata.Releases); diff != "" { + if diff := cmp.Diff(tc.ExpectedActiveReleases, res); diff != "" { t.Errorf("error mismatch (-want, +got):\n%s", diff) } return nil @@ -3062,13 +3041,13 @@ func TestCreateUndeployDBState(t *testing.T) { if err2 != nil { t.Fatal(err) } - if allReleases == nil || len(allReleases.Metadata.Releases) == 0 { + if allReleases == nil || len(allReleases) == 0 { t.Fatal("Expected some releases, but got none") } - if diff := cmp.Diff(tc.expectedReleaseNumbers, allReleases.Metadata.Releases); diff != "" { + if diff := cmp.Diff(tc.expectedReleaseNumbers, allReleases); diff != "" { t.Fatalf("error mismatch on expected lock ids (-want, +got):\n%s", diff) } - release, err2 := s.DBHandler.DBSelectReleaseByVersion(ctx, transaction, appName, uint64(allReleases.Metadata.Releases[len(allReleases.Metadata.Releases)-1]), true) + release, err2 := s.DBHandler.DBSelectReleaseByVersion(ctx, transaction, appName, uint64(allReleases[len(allReleases)-1]), true) if err2 != nil { t.Fatal(err) } @@ -3277,7 +3256,7 @@ func TestAllowedCILinksState(t *testing.T) { t.Fatal(err) } - if diff := cmp.Diff(tc.expectedAllReleases, allReleases.Metadata.Releases); diff != "" { + if diff := cmp.Diff(tc.expectedAllReleases, allReleases); diff != "" { t.Fatalf("error mismatch on expected lock ids (-want, +got):\n%s", diff) } @@ -3395,7 +3374,7 @@ func TestUndeployDBState(t *testing.T) { t.Fatal(err) } - if diff := cmp.Diff(tc.expectedAllReleases, allReleases.Metadata.Releases); diff != "" { + if diff := cmp.Diff(tc.expectedAllReleases, allReleases); diff != "" { t.Fatalf("error mismatch on expected lock ids (-want, +got):\n%s", diff) } @@ -3440,7 +3419,7 @@ func TestTransaction(t *testing.T) { Name string Transformers []Transformer expectedDbContent *db.DBAppWithMetaData - expectedDbReleases *db.DBAllReleasesWithMetaData + expectedDbReleases []int64 }{ { Name: "create one version", @@ -3464,14 +3443,7 @@ func TestTransaction(t *testing.T) { Team: "", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 1, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10000}, - }, - }, + expectedDbReleases: []int64{10000}, }, { Name: "create two versions, same team", @@ -3504,14 +3476,7 @@ func TestTransaction(t *testing.T) { Team: "noteam", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 2, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10, 11}, - }, - }, + expectedDbReleases: []int64{10, 11}, }, { Name: "create two versions, different teams", @@ -3544,14 +3509,7 @@ func TestTransaction(t *testing.T) { Team: "new", }, }, - expectedDbReleases: &db.DBAllReleasesWithMetaData{ - EslVersion: 2, - Created: gotime.Time{}, - App: appName, - Metadata: db.DBAllReleaseMetaData{ - Releases: []int64{10, 11}, - }, - }, + expectedDbReleases: []int64{10, 11}, }, } for _, tc := range tcs { @@ -3577,7 +3535,7 @@ func TestTransaction(t *testing.T) { if err3 != nil { return fmt.Errorf("error: %v", err3) } - if diff := cmp.Diff(tc.expectedDbReleases, actualRelease, cmpopts.IgnoreFields(db.DBAllReleasesWithMetaData{}, "Created")); diff != "" { + if diff := cmp.Diff(tc.expectedDbReleases, actualRelease); diff != "" { t.Errorf("error mismatch (-want, +got):\n%s", diff) } return nil diff --git a/services/cd-service/pkg/service/overview.go b/services/cd-service/pkg/service/overview.go index 18eef6812..8485e6071 100644 --- a/services/cd-service/pkg/service/overview.go +++ b/services/cd-service/pkg/service/overview.go @@ -99,7 +99,7 @@ func (o *OverviewServiceServer) GetAppDetails( logger.FromContext(ctx).Sugar().Warnf("app without releases: %v", err) } if retrievedReleasesOfApp != nil { - rels = retrievedReleasesOfApp.Metadata.Releases + rels = retrievedReleasesOfApp } uintRels := make([]uint64, len(rels)) diff --git a/services/manifest-repo-export-service/pkg/repository/transformer.go b/services/manifest-repo-export-service/pkg/repository/transformer.go index 49fe02d2f..99bd15343 100644 --- a/services/manifest-repo-export-service/pkg/repository/transformer.go +++ b/services/manifest-repo-export-service/pkg/repository/transformer.go @@ -1809,7 +1809,7 @@ func (u *UndeployApplication) Transform( transaction *sql.Tx, ) (string, error) { fs := state.Filesystem - lastRelease, err := state.DBHandler.DBSelectReleasesByAppOrderedByEslVersion(ctx, transaction, u.Application, true) + lastRelease, err := state.DBHandler.DBSelectLatestReleaseOfApp(ctx, transaction, u.Application, true) if err != nil { return "", err } diff --git a/services/manifest-repo-export-service/pkg/repository/transformer_test.go b/services/manifest-repo-export-service/pkg/repository/transformer_test.go index 9d8fe91e3..725c3f5ab 100644 --- a/services/manifest-repo-export-service/pkg/repository/transformer_test.go +++ b/services/manifest-repo-export-service/pkg/repository/transformer_test.go @@ -394,7 +394,7 @@ func TestTransformerWorksWithDb(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, @@ -404,10 +404,6 @@ func TestTransformerWorksWithDb(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1}, db.InitialEslVersion) - if err != nil { - return err - } // actual transformer to be tested: err = repo.Apply(ctx, transaction, tc.Transformers...) if err != nil { @@ -604,7 +600,7 @@ func TestDeploymentEvent(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, @@ -614,10 +610,6 @@ func TestDeploymentEvent(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1}, db.InitialEslVersion) - if err != nil { - return err - } // actual transformer to be tested: err = repo.Apply(ctx, transaction, tc.Transformers...) if err != nil { @@ -802,7 +794,7 @@ func TestReleaseTrain(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, @@ -812,10 +804,6 @@ func TestReleaseTrain(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1}, db.InitialEslVersion) - if err != nil { - return err - } err = dbHandler.DBWriteEnvironment(ctx, transaction, "staging", config.EnvironmentConfig{ Upstream: &config.EnvironmentConfigUpstream{ @@ -1346,7 +1334,7 @@ func TestCleanupOldApplicationVersions(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, @@ -1355,7 +1343,7 @@ func TestCleanupOldApplicationVersions(t *testing.T) { }) if tc.MinorRelease != 0 { - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: tc.MinorRelease, Created: time.Time{}, App: appName, @@ -1366,7 +1354,7 @@ func TestCleanupOldApplicationVersions(t *testing.T) { }) } if tc.PrepublishRelease != 0 { - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: tc.PrepublishRelease, Created: time.Time{}, App: appName, @@ -1377,10 +1365,6 @@ func TestCleanupOldApplicationVersions(t *testing.T) { }) } - if err != nil { - return err - } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1}, db.InitialEslVersion) if err != nil { return err } @@ -1590,14 +1574,14 @@ func TestReplacedByEvents(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, Manifests: db.DBReleaseManifests{}, Metadata: db.DBReleaseMetaData{}, }) - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, Created: time.Time{}, App: appName, @@ -1607,10 +1591,6 @@ func TestReplacedByEvents(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1, 2}, db.InitialEslVersion) - if err != nil { - return err - } // actual transformer to be tested: err = repo.Apply(ctx, transaction, tc.Transformers...) if err != nil { @@ -1734,14 +1714,14 @@ func TestCreateUndeployApplicationVersion(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: time.Time{}, App: appName, Manifests: db.DBReleaseManifests{}, Metadata: db.DBReleaseMetaData{}, }) - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, Created: time.Time{}, App: appName, @@ -1751,10 +1731,6 @@ func TestCreateUndeployApplicationVersion(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1, 2}, db.InitialEslVersion) - if err != nil { - return err - } err = repo.Apply(ctx, transaction, tc.Transformers...) if err != nil { return err @@ -2291,17 +2267,12 @@ func TestLocks(t *testing.T) { if err2 != nil { t.Fatal(err2) } - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{int64(concreteTransformer.Version)}, 0) - if err2 != nil { - t.Fatal(err2) - } - err2 = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err2 = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: concreteTransformer.Version, App: concreteTransformer.Application, Manifests: db.DBReleaseManifests{ Manifests: concreteTransformer.Manifests, }, - Deleted: false, }) if err2 != nil { t.Fatal(err2) @@ -2607,17 +2578,12 @@ func TestCreateUndeployLogic(t *testing.T) { if err2 != nil { t.Fatal(err2) } - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{int64(concreteTransformer.Version)}, 0) - if err2 != nil { - t.Fatal(err2) - } - err2 = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err2 = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: concreteTransformer.Version, App: concreteTransformer.Application, Manifests: db.DBReleaseManifests{ Manifests: concreteTransformer.Manifests, }, - Deleted: false, }) if err2 != nil { t.Fatal(err2) @@ -2641,7 +2607,7 @@ func TestCreateUndeployLogic(t *testing.T) { } } if tr.GetDBEventType() == db.EvtCreateUndeployApplicationVersion { - err2 = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err2 = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, App: appName, Manifests: db.DBReleaseManifests{ @@ -2657,24 +2623,10 @@ func TestCreateUndeployLogic(t *testing.T) { UndeployVersion: true, }, Created: time.Now(), - Deleted: false, }) if err2 != nil { t.Fatal(err2) } - allReleases, err2 := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, appName) - if err2 != nil { - t.Fatal(err2) - } - allReleases.Metadata.Releases = append(allReleases.Metadata.Releases, int64(2)) - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, appName, allReleases.Metadata.Releases, 1) - if err2 != nil { - t.Fatal(err2) - } - allReleases, err2 = dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, appName) - if err2 != nil { - t.Fatal(err2) - } } } err := repo.Apply(ctx, transaction, tc.Transformers...) @@ -3027,17 +2979,12 @@ func TestUndeployLogic(t *testing.T) { if err2 != nil { t.Fatal(err2) } - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, concreteTransformer.Application, []int64{int64(concreteTransformer.Version)}, 0) - if err2 != nil { - t.Fatal(err2) - } - err2 = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err2 = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: concreteTransformer.Version, App: concreteTransformer.Application, Manifests: db.DBReleaseManifests{ Manifests: concreteTransformer.Manifests, }, - Deleted: false, }) if err2 != nil { t.Fatal(err2) @@ -3062,7 +3009,7 @@ func TestUndeployLogic(t *testing.T) { } if tr.GetDBEventType() == db.EvtCreateUndeployApplicationVersion { concreteTransformer := tr.(*CreateUndeployApplicationVersion) - err2 = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err2 = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, App: concreteTransformer.Application, Manifests: db.DBReleaseManifests{ @@ -3078,24 +3025,10 @@ func TestUndeployLogic(t *testing.T) { UndeployVersion: true, }, Created: time.Now(), - Deleted: false, }) if err2 != nil { t.Fatal(err2) } - allReleases, err2 := dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, concreteTransformer.Application) - if err2 != nil { - t.Fatal(err2) - } - allReleases.Metadata.Releases = append(allReleases.Metadata.Releases, int64(2)) - err2 = dbHandler.DBInsertAllReleases(ctx, transaction, concreteTransformer.Application, allReleases.Metadata.Releases, 1) - if err2 != nil { - t.Fatal(err2) - } - allReleases, err2 = dbHandler.DBSelectAllReleasesOfApp(ctx, transaction, concreteTransformer.Application) - if err2 != nil { - t.Fatal(err2) - } } } var commitMsg []string diff --git a/services/manifest-repo-export-service/pkg/service/git_test.go b/services/manifest-repo-export-service/pkg/service/git_test.go index 3d7fd7e4d..e69e1a95e 100644 --- a/services/manifest-repo-export-service/pkg/service/git_test.go +++ b/services/manifest-repo-export-service/pkg/service/git_test.go @@ -90,7 +90,7 @@ func setupDBFixtures(ctx context.Context, dbHandler *db.DBHandler, transaction * return err } for releaseNumber := 1; releaseNumber < 4; releaseNumber++ { - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: uint64(releaseNumber), Created: time.Time{}, App: app, @@ -101,10 +101,6 @@ func setupDBFixtures(ctx context.Context, dbHandler *db.DBHandler, transaction * return err } } - err = dbHandler.DBInsertAllReleases(ctx, transaction, app, []int64{1, 2, 3}, db.EslVersion(eslVersion)) - if err != nil { - return err - } eslVersion++ } fixtureEnvironments := []string{"development-1", "development-2", "development-3"} diff --git a/services/manifest-repo-export-service/pkg/service/version_test.go b/services/manifest-repo-export-service/pkg/service/version_test.go index f6d4e21df..63436c320 100644 --- a/services/manifest-repo-export-service/pkg/service/version_test.go +++ b/services/manifest-repo-export-service/pkg/service/version_test.go @@ -231,7 +231,7 @@ func TestVersion(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: gotime.Time{}, App: "test", @@ -245,10 +245,6 @@ func TestVersion(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, "test", []int64{1}, db.InitialEslVersion) - if err != nil { - return err - } var version int64 = 1 err = dbHandler.DBWriteDeployment(ctx, transaction, db.Deployment{ App: "test", @@ -463,7 +459,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: gotime.Time{}, App: appName, @@ -473,7 +469,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, Created: gotime.Time{}, App: appName, @@ -483,7 +479,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 3, Created: gotime.Time{}, App: appName, @@ -493,11 +489,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appName, []int64{1, 2, 3}, db.InitialEslVersion) - if err != nil { - return err - } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 1, Created: gotime.Time{}, App: appNameOther, @@ -507,7 +499,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 2, Created: gotime.Time{}, App: appNameOther, @@ -517,7 +509,7 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertRelease(ctx, transaction, db.DBReleaseWithMetaData{ + err = dbHandler.DBUpdateOrCreateRelease(ctx, transaction, db.DBReleaseWithMetaData{ ReleaseNumber: 3, Created: gotime.Time{}, App: appNameOther, @@ -527,10 +519,6 @@ func TestGetManifests(t *testing.T) { if err != nil { return err } - err = dbHandler.DBInsertAllReleases(ctx, transaction, appNameOther, []int64{1, 2, 3}, db.InitialEslVersion) - if err != nil { - return err - } err = repo.Apply(ctx, transaction, tc.setup...) if err != nil { return err