fix: [AH-357]: migrations (#2709)

* fix: [AH-357]: migrations
CODE-2402
Tudor Macari 2024-09-19 17:18:13 +00:00 committed by Harness
parent 2d96b62e07
commit 4482527dbf
8 changed files with 71 additions and 166 deletions

View File

@ -16,9 +16,10 @@ CREATE TABLE artifacts_temp
CONSTRAINT check_artifact_name_length CHECK ((LENGTH(artifact_name) <= 255))
);
INSERT INTO artifacts_temp (artifact_name, artifact_registry_id, artifact_labels, artifact_enabled,
INSERT INTO artifacts_temp (artifact_id, artifact_name, artifact_registry_id, artifact_labels, artifact_enabled,
artifact_created_at, artifact_updated_at, artifact_created_by, artifact_updated_by)
SELECT i.image_name AS artifact_name,
SELECT i.image_id AS artifact_id,
i.image_name AS artifact_name,
i.image_registry_id AS artifact_registry_id,
i.image_labels AS artifact_labels,
i.image_enabled AS artifact_enabled,
@ -26,22 +27,43 @@ SELECT i.image_name AS artifact_name,
i.image_updated_at AS artifact_updated_at,
i.image_created_by AS artifact_created_by,
i.image_updated_by AS artifact_updated_by
FROM artifacts a
JOIN images i ON a.artifact_image_id = i.image_id;
ALTER TABLE artifact_stats
DROP CONSTRAINT fk_artifacts_artifact_id;
FROM images i;
DROP TABLE artifacts;
ALTER TABLE artifacts_temp
RENAME TO artifacts;
ALTER TABLE artifact_stats
ADD CONSTRAINT fk_artifacts_artifact_id FOREIGN KEY (artifact_stat_artifact_id)
REFERENCES artifacts(artifact_id);
create table if not exists artifact_stats
(
artifact_stat_id SERIAL PRIMARY KEY,
artifact_stat_artifact_id INTEGER NOT NULL
CONSTRAINT fk_artifacts_artifact_id
REFERENCES artifacts(artifact_id),
artifact_stat_date BIGINT,
artifact_stat_download_count BIGINT,
artifact_stat_upload_bytes BIGINT,
artifact_stat_download_bytes BIGINT,
artifact_stat_created_at BIGINT NOT NULL,
artifact_stat_updated_at BIGINT NOT NULL,
artifact_stat_created_by INTEGER NOT NULL,
artifact_stat_updated_by INTEGER NOT NULL,
CONSTRAINT unique_artifact_stats_artifact_id_and_date UNIQUE (artifact_stat_artifact_id, artifact_stat_date)
);
INSERT INTO artifact_stats (artifact_stat_artifact_id, artifact_stat_date, artifact_stat_download_count,
artifact_stat_upload_bytes, artifact_stat_download_bytes, artifact_stat_created_at,
artifact_stat_updated_at, artifact_stat_created_by, artifact_stat_updated_by)
SELECT a.artifact_id AS artifact_stat_artifact_id,
(EXTRACT(EPOCH FROM now()) * 1000)::BIGINT AS artifact_stat_date,
0 AS artifact_stat_download_count,
0 AS artifact_stat_upload_bytes,
0 AS artifact_stat_download_bytes,
(EXTRACT(EPOCH FROM now()) * 1000)::BIGINT AS artifact_stat_created_at,
(EXTRACT(EPOCH FROM now()) * 1000)::BIGINT AS artifact_stat_updated_at,
a.artifact_created_by AS artifact_stat_created_by,
a.artifact_updated_by AS artifact_stat_updated_by
FROM artifacts a;
CREATE INDEX index_artifact_on_registry_id ON artifacts (artifact_registry_id);

View File

@ -15,9 +15,10 @@ CREATE TABLE images
CONSTRAINT check_image_name_length CHECK ((LENGTH(image_name) <= 255))
);
INSERT INTO images (image_name, image_registry_id, image_labels, image_enabled, image_created_at,
INSERT INTO images (image_id ,image_name, image_registry_id, image_labels, image_enabled, image_created_at,
image_updated_at, image_created_by, image_updated_by)
SELECT artifact_name AS image_name,
SELECT artifact_id AS image_id,
artifact_name AS image_name,
artifact_registry_id AS image_registry_id,
artifact_labels AS image_labels,
artifact_enabled AS image_enabled,
@ -52,62 +53,17 @@ SELECT encode(m.manifest_digest, 'hex') AS artifact_version,
m.manifest_updated_by AS artifact_updated_by
FROM artifacts a
JOIN images i ON a.artifact_name = i.image_name AND a.artifact_registry_id = i.image_registry_id
JOIN manifests m ON a.artifact_name = m.manifest_image_name AND a.artifact_registry_id = m.manifest_registry_id;
JOIN manifests m ON a.artifact_name = m.manifest_image_name AND a.artifact_registry_id = m.manifest_registry_id
JOIN tags t on a.artifact_name = t.tag_image_name
AND a.artifact_registry_id = t.tag_registry_id
AND t.tag_manifest_id = m.manifest_id;
DROP INDEX index_artifact_on_registry_id;
CREATE TABLE temp_artifact_stats AS
SELECT *
FROM artifact_stats;
DROP TABLE artifact_stats;
DROP TABLE artifacts;
ALTER TABLE artifacts_temp
RENAME TO artifacts;
create table if not exists artifact_stats
(
artifact_stat_id SERIAL primary key,
artifact_stat_artifact_id INTEGER not null
constraint fk_artifacts_artifact_id
references artifacts(artifact_id),
artifact_stat_date BIGINT,
artifact_stat_download_count BIGINT,
artifact_stat_upload_bytes BIGINT,
artifact_stat_download_bytes BIGINT,
artifact_stat_created_at BIGINT not null,
artifact_stat_updated_at BIGINT not null,
artifact_stat_created_by INTEGER not null,
artifact_stat_updated_by INTEGER not null,
constraint unique_artifact_stats_artifact_id_and_date unique (artifact_stat_artifact_id, artifact_stat_date)
);
INSERT INTO artifact_stats (
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
)
SELECT
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
FROM temp_artifact_stats;
DROP TABLE temp_artifact_stats;
RENAME TO artifacts;

View File

@ -1,5 +1,3 @@
DROP TABLE bandwidth_stats;
DROP TABLE download_stats;
ALTER TABLE artifact_stats_archive RENAME TO artifact_stats;
DROP TABLE download_stats;

View File

@ -24,6 +24,4 @@ CREATE TABLE bandwidth_stats
bandwidth_stat_updated_at BIGINT NOT NULL,
bandwidth_stat_created_by INTEGER,
bandwidth_stat_updated_by INTEGER
);
ALTER TABLE artifact_stats RENAME TO artifact_stats_archive;
);

View File

@ -16,9 +16,10 @@ CREATE TABLE artifacts_temp
CONSTRAINT check_artifact_name_length CHECK ((LENGTH(artifact_name) <= 255))
);
INSERT INTO artifacts_temp (artifact_name, artifact_registry_id, artifact_labels, artifact_enabled,
INSERT INTO artifacts_temp (artifact_id, artifact_name, artifact_registry_id, artifact_labels, artifact_enabled,
artifact_created_at, artifact_updated_at, artifact_created_by, artifact_updated_by)
SELECT i.image_name AS artifact_name,
SELECT i.iamge_id AS artifact_id,
i.image_name AS artifact_name,
i.image_registry_id AS artifact_registry_id,
i.image_labels AS artifact_labels,
i.image_enabled AS artifact_enabled,
@ -26,15 +27,7 @@ SELECT i.image_name AS artifact_name,
i.image_updated_at AS artifact_updated_at,
i.image_created_by AS artifact_created_by,
i.image_updated_by AS artifact_updated_by
FROM artifacts a
JOIN images i ON a.artifact_image_id = i.image_id;
CREATE TABLE temp_artifact_stats AS
SELECT *
FROM artifact_stats;
DROP TABLE artifact_stats;
FROM images i;
DROP TABLE artifacts;
@ -58,32 +51,19 @@ create table if not exists artifact_stats
constraint unique_artifact_stats_artifact_id_and_date unique (artifact_stat_artifact_id, artifact_stat_date)
);
INSERT INTO artifact_stats (
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
)
SELECT
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
FROM temp_artifact_stats;
DROP TABLE temp_artifact_stats;
INSERT INTO artifact_stats (artifact_stat_artifact_id, artifact_stat_date, artifact_stat_download_count,
artifact_stat_upload_bytes, artifact_stat_download_bytes, artifact_stat_created_at,
artifact_stat_updated_at, artifact_stat_created_by, artifact_stat_updated_by)
SELECT a.artifact_id AS artifact_stat_artifact_id,
(strftime('%s', 'now') * 1000) AS artifact_stat_date,
0 AS artifact_stat_download_count,
0 AS artifact_stat_upload_bytes,
0 AS artifact_stat_download_bytes,
(strftime('%s', 'now') * 1000) AS artifact_stat_created_at,
(strftime('%s', 'now') * 1000) AS artifact_stat_updated_at,
a.artifact_created_by AS artifact_stat_created_by,
a.artifact_updated_by AS artifact_stat_updated_by
FROM artifacts a;
CREATE INDEX index_artifact_on_registry_id ON artifacts (artifact_registry_id);

View File

@ -15,9 +15,10 @@ CREATE TABLE images
CONSTRAINT check_image_name_length CHECK ((LENGTH(image_name) <= 255))
);
INSERT INTO images (image_name, image_registry_id, image_labels, image_enabled, image_created_at,
INSERT INTO images (image_id, image_name, image_registry_id, image_labels, image_enabled, image_created_at,
image_updated_at, image_created_by, image_updated_by)
SELECT artifact_name AS image_name,
SELECT artifact_id AS image_id,
artifact_name AS image_name,
artifact_registry_id AS image_registry_id,
artifact_labels AS image_labels,
artifact_enabled AS image_enabled,
@ -52,62 +53,16 @@ SELECT LOWER(hex(m.manifest_digest)) AS artifact_version,
m.manifest_updated_by AS artifact_updated_by
FROM artifacts a
JOIN images i ON a.artifact_name = i.image_name AND a.artifact_registry_id = i.image_registry_id
JOIN manifests m ON a.artifact_name = m.manifest_image_name AND a.artifact_registry_id = m.manifest_registry_id;
JOIN manifests m ON a.artifact_name = m.manifest_image_name AND a.artifact_registry_id = m.manifest_registry_id
JOIN tags t on a.artifact_name = t.tag_image_name
AND a.artifact_registry_id = t.tag_registry_id
AND t.tag_manifest_id = m.manifest_id;
DROP INDEX index_artifact_on_registry_id;
CREATE TABLE temp_artifact_stats AS
SELECT *
FROM artifact_stats;
DROP TABLE artifact_stats;
DROP TABLE artifacts;
ALTER TABLE artifacts_temp
RENAME TO artifacts;
create table if not exists artifact_stats
(
artifact_stat_id INTEGER PRIMARY KEY AUTOINCREMENT,
artifact_stat_artifact_id INTEGER not null
constraint fk_artifacts_artifact_id
references artifacts(artifact_id),
artifact_stat_date INTEGER,
artifact_stat_download_count INTEGER,
artifact_stat_upload_bytes INTEGER,
artifact_stat_download_bytes INTEGER,
artifact_stat_created_at INTEGER not null,
artifact_stat_updated_at INTEGER not null,
artifact_stat_created_by INTEGER not null,
artifact_stat_updated_by INTEGER not null,
constraint unique_artifact_stats_artifact_id_and_date unique (artifact_stat_artifact_id, artifact_stat_date)
);
INSERT INTO artifact_stats (
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
)
SELECT
artifact_stat_id,
artifact_stat_artifact_id,
artifact_stat_date,
artifact_stat_download_count,
artifact_stat_upload_bytes,
artifact_stat_download_bytes,
artifact_stat_created_at,
artifact_stat_updated_at,
artifact_stat_created_by,
artifact_stat_updated_by
FROM temp_artifact_stats;
DROP TABLE temp_artifact_stats;
RENAME TO artifacts;

View File

@ -1,5 +1,3 @@
DROP TABLE bandwidth_stats;
DROP TABLE download_stats;
ALTER TABLE artifact_stats_archive RENAME TO artifact_stats;
DROP TABLE download_stats;

View File

@ -24,6 +24,4 @@ CREATE TABLE bandwidth_stats
bandwidth_stat_updated_at INTEGER NOT NULL,
bandwidth_stat_created_by INTEGER NOT NULL,
bandwidth_stat_updated_by INTEGER NOT NULL
);
ALTER TABLE artifact_stats RENAME TO artifact_stats_archive;
);