Add temporary column `new_amount_used` for CI minutes tracking
What does this MR do and why?
Related to https://gitlab.com/gitlab-org/gitlab/-/issues/364282
In this MR we want to increase the accuracy of ci_project_monthly_usages.amount_used
from 2 decimal figures to 4 decimal figures.
In doing that we add a new_amount_used
column and set a trigger to keep that in sync with amount_used
. In a separate MR (for release N+1) we would remove the old column and rename the new one to amount_used
:
transaction do
remove_column :amount_used
rename_column :new_amount_used, :amount_used
# check if trigger is dropped automatically or drop the trigger
# explicitly inside the transaction.
end
Database migration
$ rails db:migrate
ci: == 20221010184839 AddNewAmountUsedToCiProjectMonthlyUsages: migrating =========
ci: -- add_column(:ci_project_monthly_usages, :new_amount_used, :decimal, {:default=>0.0, :precision=>18, :scale=>4, :null=>false, :if_not_exists=>true})
ci: -> 0.0077s
ci: == 20221010184839 AddNewAmountUsedToCiProjectMonthlyUsages: migrated (0.0167s)
$ rails db:rollback:ci
ci: == 20221010184839 AddNewAmountUsedToCiProjectMonthlyUsages: reverting =========
ci: -- remove_column(:ci_project_monthly_usages, :new_amount_used)
ci: -> 0.0014s
ci: == 20221010184839 AddNewAmountUsedToCiProjectMonthlyUsages: reverted (0.0065s)
Data migration
-
Count of records to update:
- SELECT COUNT(*) FROM "ci_project_monthly_usages" WHERE (amount_used > 0)
- https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/12884/commands/45252
-
Find first ID for batching:
SELECT "ci_project_monthly_usages"."id" FROM "ci_project_monthly_usages" WHERE (amount_used > 0) ORDER BY "ci_project_monthly_usages"."id" ASC LIMIT 1
- https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/12884/commands/45249
-
Find first batch:
SELECT “ci_project_monthly_usages”.“id” FROM “ci_project_monthly_usages” WHERE (amount_used > 0) AND “ci_project_monthly_usages”.“id” >= 1 ORDER BY “ci_project_monthly_usages”.“id” ASC LIMIT 1 OFFSET 500
- https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/12884/commands/45250
-
Update statement:
- `SELECT "ci_project_monthly_usages".* FROM "ci_project_monthly_usages" WHERE (amount_used > 0) AND "ci_project_monthly_usages"."id" >= 1 AND "ci_project_monthly_usages"."id" < 4
- https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/12884/commands/45251
TODO
-
Create same migration for ci_namespace_monthly_usages
-
Create background/post-deployment migration to SET new_amount_used = amount_used
for existing records (in batches)
Screenshots or screen recordings
Screenshots are required for UI changes, and strongly recommended for all other merge requests.
How to set up and validate locally
Numbered steps to set up and validate the change are strongly suggested.
MR acceptance checklist
This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.
-
I have evaluated the MR acceptance checklist for this MR.