Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(cd-service): auto increment for apps version #2101

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
ALTER TABLE IF EXISTS apps ADD COLUMN IF NOT EXISTS version INTEGER;
WITH ordered_rows AS (
SELECT eslversion, appname, ROW_NUMBER() OVER (ORDER BY eslversion) AS row_num
FROM apps
)
UPDATE apps
SET version = ordered_rows.row_num
FROM ordered_rows
WHERE apps.eslversion = ordered_rows.eslversion AND apps.appname = ordered_rows.appname;

CREATE SEQUENCE IF NOT EXISTS apps_version_seq OWNED BY apps.version;

SELECT setval('apps_version_seq', coalesce(max(version), 0) + 1, false) FROM apps;

ALTER TABLE IF EXISTS apps
ALTER COLUMN version SET DEFAULT nextval('apps_version_seq');

ALTER TABLE IF EXISTS apps DROP CONSTRAINT IF EXISTS apps_pkey;

ALTER TABLE IF EXISTS apps ADD PRIMARY KEY (version, appname);

ALTER TABLE IF EXISTS apps DROP COLUMN IF EXISTS eslversion;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
CREATE TABLE IF NOT EXISTS apps_new
(
created TIMESTAMP,
appName VARCHAR,
stateChange VARCHAR,
metadata VARCHAR,
version INTEGER PRIMARY KEY AUTOINCREMENT
);

INSERT INTO apps_new (created, appname, statechange, metadata)
SELECT created, appname, statechange, metadata
FROM apps
ORDER BY eslversion;

DROP TABLE IF EXISTS apps;
ALTER TABLE apps_new RENAME TO apps;
28 changes: 13 additions & 15 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type DBConfig struct {
MaxOpenConnections uint
}

type InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion EslVersion, stateChange AppStateChange, metaData DBAppMetaData) error
type InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange AppStateChange, metaData DBAppMetaData) error

type DBHandler struct {
DbName string
Expand Down Expand Up @@ -126,9 +126,9 @@ func Connect(ctx context.Context, cfg DBConfig) (*DBHandler, error) {
WriteEslOnly: cfg.WriteEslOnly,
InsertAppFun: nil,
}
handler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion EslVersion, stateChange AppStateChange, metaData DBAppMetaData) error {
handler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange AppStateChange, metaData DBAppMetaData) error {
// by default, we just insert the app
return handler.DBInsertApplication(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
return handler.DBInsertApplication(ctx, transaction, appName, stateChange, metaData)
}
return handler, nil
}
Expand Down Expand Up @@ -2278,21 +2278,20 @@ type DBAppMetaData struct {
}

type DBAppWithMetaData struct {
EslVersion EslVersion
App string
Metadata DBAppMetaData
StateChange AppStateChange
}

func (h *DBHandler) DBInsertApplication(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion EslVersion, stateChange AppStateChange, metaData DBAppMetaData) error {
func (h *DBHandler) DBInsertApplication(ctx context.Context, transaction *sql.Tx, appName string, stateChange AppStateChange, metaData DBAppMetaData) error {
span, ctx := tracer.StartSpanFromContext(ctx, "DBInsertApplication")
defer span.Finish()
jsonToInsert, err := json.Marshal(metaData)
if err != nil {
return fmt.Errorf("could not marshal json data: %w", err)
}
insertQuery := h.AdaptQuery(
"INSERT INTO apps (eslVersion, created, appName, stateChange, metadata) VALUES (?, ?, ?, ?, ?);",
"INSERT INTO apps (created, appName, stateChange, metadata) VALUES (?, ?, ?, ?);",
)
now, err := h.DBReadTransactionTimestamp(ctx, transaction)
if err != nil {
Expand All @@ -2301,7 +2300,6 @@ func (h *DBHandler) DBInsertApplication(ctx context.Context, transaction *sql.Tx
span.SetTag("query", insertQuery)
_, err = transaction.Exec(
insertQuery,
previousEslVersion+1,
*now,
appName,
stateChange,
Expand Down Expand Up @@ -2330,7 +2328,7 @@ func (h *DBHandler) DBSelectAnyApp(ctx context.Context, tx *sql.Tx) (*DBAppWithM
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAnyApp")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, appName, metadata " +
"SELECT appName, metadata " +
" FROM apps " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)
Expand All @@ -2351,7 +2349,7 @@ func (h *DBHandler) DBSelectAnyApp(ctx context.Context, tx *sql.Tx) (*DBAppWithM
var row = &DBAppWithMetaData{}
if rows.Next() {
var metadataStr string
err := rows.Scan(&row.EslVersion, &row.App, &metadataStr)
err := rows.Scan(&row.App, &metadataStr)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand Down Expand Up @@ -2380,10 +2378,10 @@ func (h *DBHandler) DBSelectApp(ctx context.Context, tx *sql.Tx, appName string)
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectApp")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, appName, stateChange, metadata" +
"SELECT appName, stateChange, metadata" +
" FROM apps " +
" WHERE appName=? " +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)

Expand All @@ -2399,10 +2397,10 @@ func (h *DBHandler) DBSelectAppAtTimestamp(ctx context.Context, tx *sql.Tx, appN
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAppAtTimestamp")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, appName, stateChange, metadata" +
"SELECT appName, stateChange, metadata" +
" FROM apps " +
" WHERE appName=? AND created <= ?" +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)

Expand Down Expand Up @@ -2430,7 +2428,7 @@ func (h *DBHandler) processAppsRow(ctx context.Context, rows *sql.Rows, err erro
var row = &DBAppWithMetaData{}
if rows.Next() {
var metadataStr string
err := rows.Scan(&row.EslVersion, &row.App, &row.StateChange, &metadataStr)
err := rows.Scan(&row.App, &row.StateChange, &metadataStr)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand Down Expand Up @@ -3064,7 +3062,7 @@ func (h *DBHandler) runCustomMigrationApps(ctx context.Context, transaction *sql
defer span.Finish()

for app, team := range *appsMap {
err := h.DBInsertApplication(ctx, transaction, app, InitialEslVersion, AppStateChangeMigrate, DBAppMetaData{Team: team})
err := h.DBInsertApplication(ctx, transaction, app, AppStateChangeMigrate, DBAppMetaData{Team: team})
if err != nil {
return fmt.Errorf("could not write dbApp %s: %v", app, err)
}
Expand Down
1 change: 0 additions & 1 deletion pkg/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ func TestCustomMigrationsApps(t *testing.T) {
Name: "Simple migration",
expectedApps: []*DBAppWithMetaData{
{
EslVersion: 2,
App: appName,
StateChange: AppStateChangeMigrate,
Metadata: DBAppMetaData{
Expand Down
2 changes: 1 addition & 1 deletion pkg/db/overview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func TestUpdateOverviewApplicationLock(t *testing.T) {
dbHandler := setupDB(t)

err := dbHandler.WithTransaction(ctx, false, func(ctx context.Context, transaction *sql.Tx) error {
err := dbHandler.DBInsertApplication(ctx, transaction, "test", 0, AppStateChangeCreate, DBAppMetaData{})
err := dbHandler.DBInsertApplication(ctx, transaction, "test", AppStateChangeCreate, DBAppMetaData{})
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,8 @@ func SetupRepositoryTestWithDBOptions(t *testing.T, writeEslOnly bool) (reposito
if err != nil {
t.Fatal(err)
}
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, stateChange, metaData)
}
return repo, &repoCfg
}
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ func RunServer() {

if dbHandler.ShouldUseOtherTables() {
// we overwrite InsertApp in order to also update the overview:
dbHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
dbHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, stateChange, metaData)
}
//Check for migrations -> for pulling
logger.FromContext(ctx).Sugar().Warnf("checking if migrations are required...")
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2461,9 +2461,9 @@ func (s *State) WriteAllCommitEvents(ctx context.Context, transaction *sql.Tx, d
return nil
}

func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
func (s *State) DBInsertApplicationWithOverview(ctx context.Context, transaction *sql.Tx, appName string, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
h := s.DBHandler
err := h.DBInsertApplication(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
err := h.DBInsertApplication(ctx, transaction, appName, stateChange, metaData)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion services/cd-service/pkg/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2353,7 +2353,7 @@ func TestUpdateOverviewCache(t *testing.T) {
metaData := db.DBAppMetaData{
Team: "",
}
err = state.DBInsertApplicationWithOverview(ctx, transaction, "app1", db.InitialEslVersion-1, tc.StateChange, metaData)
err = state.DBInsertApplicationWithOverview(ctx, transaction, "app1", tc.StateChange, metaData)
if err != nil {
return err
}
Expand Down
14 changes: 3 additions & 11 deletions services/cd-service/pkg/repository/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,21 +532,14 @@ func (c *CreateApplicationVersion) Transform(
if err != nil {
return "", GetCreateReleaseGeneralFailure(fmt.Errorf("could not read apps: %v", err))
}
var ver db.EslVersion
if app == nil {
ver = db.InitialEslVersion
} else {
if app.StateChange != db.AppStateChangeDelete {
return "", GetCreateReleaseGeneralFailure(fmt.Errorf("could not write new app, app already exists: %v", err)) //Should never happen
}
ver = app.EslVersion + 1
if app != nil && app.StateChange != db.AppStateChangeDelete {
return "", GetCreateReleaseGeneralFailure(fmt.Errorf("could not write new app, app already exists: %v", err)) //Should never happen
}

err = state.DBHandler.InsertAppFun(
ctx,
transaction,
c.Application,
ver,
db.AppStateChangeCreate,
db.DBAppMetaData{Team: c.Team},
)
Expand All @@ -569,7 +562,6 @@ func (c *CreateApplicationVersion) Transform(
ctx,
transaction,
c.Application,
existingApp.EslVersion,
db.AppStateChangeUpdate,
newMeta,
)
Expand Down Expand Up @@ -1637,7 +1629,7 @@ func (u *UndeployApplication) Transform(
if err != nil {
return "", fmt.Errorf("UndeployApplication: could not select app '%s': %v", u.Application, err)
}
err = state.DBHandler.InsertAppFun(ctx, transaction, dbApp.App, dbApp.EslVersion, db.AppStateChangeDelete, db.DBAppMetaData{Team: dbApp.Metadata.Team})
err = state.DBHandler.InsertAppFun(ctx, transaction, dbApp.App, db.AppStateChangeDelete, db.DBAppMetaData{Team: dbApp.Metadata.Team})
if err != nil {
return "", fmt.Errorf("UndeployApplication: could not insert app '%s': %v", u.Application, err)
}
Expand Down
6 changes: 0 additions & 6 deletions services/cd-service/pkg/repository/transformer_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,6 @@ func TestCreateApplicationVersionDB(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 2,
App: appName,
StateChange: db.AppStateChangeCreate,
Metadata: db.DBAppMetaData{
Expand Down Expand Up @@ -699,7 +698,6 @@ func TestCreateApplicationVersionDB(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 2, // even when CreateApplicationVersion is called twice, we still write the app only once
App: appName,
StateChange: db.AppStateChangeCreate,
Metadata: db.DBAppMetaData{
Expand Down Expand Up @@ -740,7 +738,6 @@ func TestCreateApplicationVersionDB(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 3, // CreateApplicationVersion was called twice with different teams, so there's 2 new entries, instead of onc
App: appName,
StateChange: db.AppStateChangeUpdate,
Metadata: db.DBAppMetaData{
Expand Down Expand Up @@ -3380,7 +3377,6 @@ func TestTransaction(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 2,
App: appName,
StateChange: db.AppStateChangeCreate,
Metadata: db.DBAppMetaData{
Expand Down Expand Up @@ -3421,7 +3417,6 @@ func TestTransaction(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 2, // even when CreateApplicationVersion is called twice, we still write the app only once
App: appName,
StateChange: db.AppStateChangeCreate,
Metadata: db.DBAppMetaData{
Expand Down Expand Up @@ -3462,7 +3457,6 @@ func TestTransaction(t *testing.T) {
},
},
expectedDbContent: &db.DBAppWithMetaData{
EslVersion: 3, // CreateApplicationVersion was called twice with different teams, so there's 2 new entries, instead of onc
App: appName,
StateChange: db.AppStateChangeUpdate,
Metadata: db.DBAppMetaData{
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/repository/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7097,8 +7097,8 @@ func SetupRepositoryTestWithDBOptions(t *testing.T, writeEslOnly bool) Repositor
if err != nil {
t.Fatal(err)
}
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, stateChange, metaData)
}
return repo
}
Expand Down
4 changes: 2 additions & 2 deletions services/cd-service/pkg/service/batch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -680,8 +680,8 @@ func setupRepositoryTestWithDB(t *testing.T, dbConfig *db.DBConfig) (repository.
}

if dbConfig != nil {
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, previousEslVersion db.EslVersion, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, previousEslVersion, stateChange, metaData)
repo.State().DBHandler.InsertAppFun = func(ctx context.Context, transaction *sql.Tx, appName string, stateChange db.AppStateChange, metaData db.DBAppMetaData) error {
return repo.State().DBInsertApplicationWithOverview(ctx, transaction, appName, stateChange, metaData)
}
}
return repo, nil
Expand Down
Loading
Loading