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

RHINENG-5394: migrate system_package per account #1353

Merged
merged 2 commits into from
Dec 11, 2023
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
41 changes: 41 additions & 0 deletions database_admin/migrations/119_migrate_system_package2.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
CREATE OR REPLACE PROCEDURE copy_system_packages()
LANGUAGE plpgsql
AS
$$
DECLARE
cnt bigint := 0;
prev_cnt bigint := 0;
rows_inserted bigint := 0;
account int := 0;
BEGIN
FOR account IN (SELECT id from rh_account ORDER BY hash_partition_id(id, 128), id)
LOOP
INSERT INTO system_package2
SELECT system_package.rh_account_id,
system_id,
name_id,
package_id,
(SELECT id
FROM package
WHERE package.name_id = system_package.name_id
AND evra =
JSONB_PATH_QUERY_ARRAY(update_data, '$[*] ? (@.status== "Installable").evra') ->> 0),
(SELECT id
FROM package
WHERE package.name_id = system_package.name_id
AND evra = JSONB_PATH_QUERY_ARRAY(update_data, '$[*] ? (@.status== "Applicable").evra') ->> 0)
FROM system_package
JOIN system_platform ON system_platform.id = system_package.system_id AND system_platform.rh_account_id = system_package.rh_account_id
WHERE system_package.rh_account_id = account;
COMMIT;

GET DIAGNOSTICS rows_inserted = ROW_COUNT;

cnt := cnt + rows_inserted;
IF (cnt/1000000)::int > (prev_cnt/1000000)::int THEN
RAISE NOTICE 'inserted % rows, account: %, partition: %', cnt, account, hash_partition_id(account, 128);
prev_cnt := cnt;
END IF;
END LOOP;
END
$$;
2 changes: 1 addition & 1 deletion database_admin/schema/create_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS schema_migrations


INSERT INTO schema_migrations
VALUES (118, false);
VALUES (119, false);

-- ---------------------------------------------------------------------------
-- Functions
Expand Down
54 changes: 26 additions & 28 deletions turnpike/controllers/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"gorm.io/gorm"
)

type Session struct {
Expand Down Expand Up @@ -301,50 +302,47 @@ func MigrateSystemPackage(c *gin.Context) {
var cnt int64
db := database.Db

execQuery(db, "VACUUM ANALYZE system_package;")
db.Table("system_package2").Count(&cnt)
if cnt > 0 {
utils.LogInfo("System_package2 table is not empty")
c.JSON(http.StatusNoContent, "System_package2 table is not empty, nothing to do.")
return
}

// nolint:lll
go func() {
if err := db.Exec(`
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_applicable_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_installable_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_name_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_package_id_fkey;
ALTER TABLE system_package2 DROP CONSTRAINT system_package2_rh_account_id_system_id_fkey;
DROP INDEX system_package2_account_pkg_name_idx;
DROP INDEX system_package2_package_id_idx;
`).Error; err != nil {
// don't fail when we couldn't remove constraints and indexes
// continue with migration
utils.LogWarn("err", err.Error(), "Couldn't remove constraints and indexes")
}
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_applicable_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_installable_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_name_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_package_id_fkey;")
execQuery(db, "ALTER TABLE system_package2 DROP CONSTRAINT system_package2_rh_account_id_system_id_fkey;")
execQuery(db, "DROP INDEX system_package2_account_pkg_name_idx;")
execQuery(db, "DROP INDEX system_package2_package_id_idx;")

if err := db.Exec("CALL copy_system_packages();").Error; err != nil {
// truncate system_package2 table on failed migration
db.Exec("TRUNCATE TABLE system_package2;")
execQuery(db, "TRUNCATE TABLE system_package2;")
utils.LogError("err", err.Error(), "Migration failed")
return
}

// nolint:lll
if err := db.Exec(`
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_applicable_id_fkey FOREIGN KEY (applicable_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_installable_id_fkey FOREIGN KEY (installable_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_name_id_fkey FOREIGN KEY (name_id) REFERENCES package_name(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_package_id_fkey FOREIGN KEY (package_id) REFERENCES package(id);
ALTER TABLE system_package2 ADD CONSTRAINT system_package2_rh_account_id_system_id_fkey FOREIGN KEY (rh_account_id, system_id) REFERENCES system_platform (rh_account_id, id);
CREATE INDEX IF NOT EXISTS system_package2_account_pkg_name_idx
ON system_package2 (rh_account_id, name_id) INCLUDE (system_id, package_id, installable_id, applicable_id);
CREATE INDEX IF NOT EXISTS system_package2_package_id_idx on system_package2 (package_id);
`).Error; err != nil {
utils.LogError("err", err.Error(), "Couldn't add constraints and indexes")
return
}
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_applicable_id_fkey FOREIGN KEY (applicable_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_installable_id_fkey FOREIGN KEY (installable_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_name_id_fkey FOREIGN KEY (name_id) REFERENCES package_name(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_package_id_fkey FOREIGN KEY (package_id) REFERENCES package(id);")
execQuery(db, "ALTER TABLE system_package2 ADD CONSTRAINT system_package2_rh_account_id_system_id_fkey FOREIGN KEY (rh_account_id, system_id) REFERENCES system_platform (rh_account_id, id);")
execQuery(db, `CREATE INDEX IF NOT EXISTS system_package2_account_pkg_name_idx
ON system_package2 (rh_account_id, name_id) INCLUDE (system_id, package_id, installable_id, applicable_id);`)
execQuery(db, "CREATE INDEX IF NOT EXISTS system_package2_package_id_idx on system_package2 (package_id);")
utils.LogInfo("System_package migration completed")
}()
c.JSON(http.StatusOK, "Migration started")
}

func execQuery(db *gorm.DB, query string) {
err := db.Exec(query).Error
if err != nil {
utils.LogWarn("err", err.Error(), "query", query, "Exec of query failed.")
}
}
Loading