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(db): add auto increment for deployments table #2161

Merged
merged 2 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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
EXECUTE 'WITH ordered_rows AS (
SELECT eslversion, envname, appname, ROW_NUMBER() OVER (ORDER BY eslversion) AS row_num
FROM deployments
)
UPDATE deployments
SET version = ordered_rows.row_num
FROM ordered_rows
WHERE deployments.eslversion = ordered_rows.eslversion AND deployments.appname = ordered_rows.appname AND deployments.envname = ordered_rows.envname;';
AminSlk marked this conversation as resolved.
Show resolved Hide resolved
END IF;
END $$;

CREATE SEQUENCE IF NOT EXISTS deployments_version_seq OWNED BY deployments.version;

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');

ALTER TABLE IF EXISTS deployments DROP CONSTRAINT IF EXISTS deployments_pkey;

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

ALTER TABLE IF EXISTS deployments DROP COLUMN IF EXISTS eslversion;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS deployments_new
(
created TIMESTAMP,
releaseVersion BIGINT NULL,
appName VARCHAR,
envName VARCHAR,
metadata VARCHAR,
transformereslVersion INTEGER DEFAULT 0,
version INTEGER PRIMARY KEY AUTOINCREMENT,
FOREIGN KEY(transformereslVersion) REFERENCES event_sourcing_light(eslVersion)
);

INSERT INTO deployments_new (created, releaseversion, appname, envname, metadata, transformereslversion)
SELECT created, releaseversion, appname, envname, metadata, transformereslversion
FROM deployments
ORDER BY eslversion;

DROP TABLE IF EXISTS deployments;
ALTER TABLE deployments_new RENAME TO deployments;

CREATE INDEX IF NOT EXISTS deployments_idx ON deployments (appName, envname);
55 changes: 23 additions & 32 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -1696,7 +1696,6 @@ func (h *DBHandler) DBSelectAllApplications(ctx context.Context, transaction *sq
}

type DBDeployment struct {
EslVersion EslVersion
Created time.Time
ReleaseVersion *int64
App string
Expand All @@ -1706,7 +1705,6 @@ type DBDeployment struct {
}

type Deployment struct {
EslVersion EslVersion
Created time.Time
App string
Env string
Expand Down Expand Up @@ -1854,10 +1852,10 @@ func (h *DBHandler) DBSelectLatestDeployment(ctx context.Context, tx *sql.Tx, ap
defer span.Finish()

selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, releaseVersion, appName, envName, metadata, transformereslVersion" +
"SELECT created, releaseVersion, appName, envName, metadata, transformereslVersion" +
" FROM deployments " +
" WHERE appName=? AND envName=? " +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
Expand Down Expand Up @@ -1885,15 +1883,14 @@ func (h *DBHandler) DBSelectAllLatestDeploymentsForApplication(ctx context.Conte
selectQuery := h.AdaptQuery(
`
SELECT
deployments.eslVersion,
deployments.created,
deployments.appname,
deployments.releaseVersion,
deployments.envName,
deployments.metadata
FROM (
SELECT
MAX(eslVersion) AS latest,
MAX(version) AS latest,
appname,
envname
FROM
Expand All @@ -1904,7 +1901,7 @@ func (h *DBHandler) DBSelectAllLatestDeploymentsForApplication(ctx context.Conte
JOIN
deployments AS deployments
ON
latest.latest=deployments.eslVersion
latest.latest=deployments.version
AND latest.appname=deployments.appname
AND latest.envName=deployments.envName
WHERE deployments.appname = (?) AND deployments.releaseVersion IS NOT NULL ;`)
Expand Down Expand Up @@ -1937,7 +1934,7 @@ func (h *DBHandler) DBSelectAllLatestDeploymentsOnEnvironment(ctx context.Contex
deployments.releaseVersion
FROM (
SELECT
MAX(eslVersion) AS latest,
MAX(version) AS latest,
appname,
envname
FROM
Expand All @@ -1947,7 +1944,7 @@ FROM (
JOIN
deployments AS deployments
ON
latest.latest=deployments.eslVersion
latest.latest=deployments.version
AND latest.appname=deployments.appname
AND latest.envName=deployments.envName
WHERE deployments.envName= ?;
Expand Down Expand Up @@ -2003,11 +2000,10 @@ func processAllLatestDeploymentsForApp(rows *sql.Rows) (map[string]Deployment, e
result := make(map[string]Deployment)
for rows.Next() {
var curr = Deployment{
EslVersion: 0,
Created: time.Time{},
Env: "",
App: "",
Version: nil,
Created: time.Time{},
Env: "",
App: "",
Version: nil,
Metadata: DeploymentMetadata{
DeployedByName: "",
DeployedByEmail: "",
Expand All @@ -2017,7 +2013,7 @@ func processAllLatestDeploymentsForApp(rows *sql.Rows) (map[string]Deployment, e
}
var releaseVersion sql.NullInt64
var jsonMetadata string
err := rows.Scan(&curr.EslVersion, &curr.Created, &curr.App, &releaseVersion, &curr.Env, &jsonMetadata)
err := rows.Scan(&curr.Created, &curr.App, &releaseVersion, &curr.Env, &jsonMetadata)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand Down Expand Up @@ -2049,10 +2045,10 @@ func (h *DBHandler) DBSelectSpecificDeployment(ctx context.Context, tx *sql.Tx,
defer span.Finish()

selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, releaseVersion, appName, envName, metadata, transformereslVersion" +
"SELECT created, releaseVersion, appName, envName, metadata, transformereslVersion" +
" FROM deployments " +
" WHERE appName=? AND envName=? and releaseVersion=?" +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
Expand All @@ -2077,7 +2073,6 @@ func (h *DBHandler) DBSelectSpecificDeployment(ctx context.Context, tx *sql.Tx,
func processDeployment(rows *sql.Rows) (*Deployment, error) {
var releaseVersion sql.NullInt64
var row = &DBDeployment{
EslVersion: 0,
Created: time.Time{},
ReleaseVersion: nil,
App: "",
Expand All @@ -2088,7 +2083,7 @@ func processDeployment(rows *sql.Rows) (*Deployment, error) {
//exhaustruct:ignore
var resultJson = DeploymentMetadata{}
if rows.Next() {
err := rows.Scan(&row.EslVersion, &row.Created, &releaseVersion, &row.App, &row.Env, &row.Metadata, &row.TransformerID)
err := rows.Scan(&row.Created, &releaseVersion, &row.App, &row.Env, &row.Metadata, &row.TransformerID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand All @@ -2113,7 +2108,6 @@ func processDeployment(rows *sql.Rows) (*Deployment, error) {
return nil, fmt.Errorf("deployments: row has error: %v\n", err)
}
return &Deployment{
EslVersion: row.EslVersion,
Created: row.Created,
App: row.App,
Env: row.Env,
Expand All @@ -2128,10 +2122,10 @@ func (h *DBHandler) DBSelectDeploymentHistory(ctx context.Context, tx *sql.Tx, a
defer span.Finish()

selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, releaseVersion, appName, envName, metadata, transformereslVersion" +
"SELECT created, releaseVersion, appName, envName, metadata, transformereslVersion" +
" FROM deployments " +
" WHERE appName=? AND envName=? " +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT ?;"))
span.SetTag("query", selectQuery)
rows, err := tx.QueryContext(
Expand Down Expand Up @@ -2172,10 +2166,10 @@ func (h *DBHandler) DBSelectDeploymentsByTransformerID(ctx context.Context, tx *
defer span.Finish()

selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, releaseVersion, appName, envName, metadata, transformereslVersion" +
"SELECT created, releaseVersion, appName, envName, metadata, transformereslVersion" +
" FROM deployments " +
" WHERE transformereslVersion=? " +
" ORDER BY eslVersion DESC " +
" ORDER BY version DESC " +
" LIMIT ?;"))

span.SetTag("query", selectQuery)
Expand Down Expand Up @@ -2213,7 +2207,7 @@ func (h *DBHandler) DBSelectAnyDeployment(ctx context.Context, tx *sql.Tx) (*DBD
span, ctx := tracer.StartSpanFromContext(ctx, "DBSelectAnyDeployment")
defer span.Finish()
selectQuery := h.AdaptQuery(fmt.Sprintf(
"SELECT eslVersion, created, releaseVersion, appName, envName" +
"SELECT created, releaseVersion, appName, envName" +
" FROM deployments " +
" LIMIT 1;"))
span.SetTag("query", selectQuery)
Expand All @@ -2235,7 +2229,7 @@ func (h *DBHandler) DBSelectAnyDeployment(ctx context.Context, tx *sql.Tx) (*DBD
var row = &DBDeployment{}
if rows.Next() {
var releaseVersion sql.NullInt64
err := rows.Scan(&row.EslVersion, &row.Created, &releaseVersion, &row.App, &row.Env)
err := rows.Scan(&row.Created, &releaseVersion, &row.App, &row.Env)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand Down Expand Up @@ -2491,7 +2485,7 @@ func processAppRow(ctx context.Context, rows *sql.Rows) (*DBAppWithMetaData, err
}

// DBWriteDeployment writes one deployment, meaning "what should be deployed"
func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deployment Deployment, previousEslVersion EslVersion, skipOverview bool) error {
func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deployment Deployment, skipOverview bool) error {
span, ctx := tracer.StartSpanFromContext(ctx, "DBWriteDeployment")
defer span.Finish()
if h == nil {
Expand All @@ -2507,7 +2501,7 @@ func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deploymen
}

insertQuery := h.AdaptQuery(
"INSERT INTO deployments (eslVersion, created, releaseVersion, appName, envName, metadata, transformereslVersion) VALUES (?, ?, ?, ?, ?, ?, ?);")
"INSERT INTO deployments (created, releaseVersion, appName, envName, metadata, transformereslVersion) VALUES (?, ?, ?, ?, ?, ?);")

now, err := h.DBReadTransactionTimestamp(ctx, tx)
if err != nil {
Expand All @@ -2518,7 +2512,6 @@ func (h *DBHandler) DBWriteDeployment(ctx context.Context, tx *sql.Tx, deploymen

_, err = tx.Exec(
insertQuery,
previousEslVersion+1,
*now,
nullVersion,
deployment.App,
Expand Down Expand Up @@ -5226,7 +5219,6 @@ func (h *DBHandler) processSingleDeploymentAttemptsRow(ctx context.Context, rows
// processSingleDeploymentRow only processes the row. It assumes that there is an element ready to be processed in rows.
func (h *DBHandler) processSingleDeploymentRow(ctx context.Context, rows *sql.Rows) (*Deployment, error) {
var row = &DBDeployment{
EslVersion: 0,
Created: time.Time{},
ReleaseVersion: nil,
App: "",
Expand All @@ -5238,7 +5230,7 @@ func (h *DBHandler) processSingleDeploymentRow(ctx context.Context, rows *sql.Ro
//exhaustruct:ignore
var resultJson = DeploymentMetadata{}

err := rows.Scan(&row.EslVersion, &row.Created, &releaseVersion, &row.App, &row.Env, &row.Metadata, &row.TransformerID)
err := rows.Scan(&row.Created, &releaseVersion, &row.App, &row.Env, &row.Metadata, &row.TransformerID)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
Expand All @@ -5255,7 +5247,6 @@ func (h *DBHandler) processSingleDeploymentRow(ctx context.Context, rows *sql.Ro
}

return &Deployment{
EslVersion: row.EslVersion,
Created: row.Created,
App: row.App,
Env: row.Env,
Expand Down
Loading
Loading