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

[batch] Add deduped id column to resources table #12723

Merged
merged 1 commit into from
Feb 28, 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
18 changes: 18 additions & 0 deletions batch/batch/driver/billing_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ async def _refresh_resources_from_retail_prices(self, prices: List[Price]):
@transaction(self.db)
async def insert_or_update(tx):
if resource_updates:
last_resource_id = await tx.execute_and_fetchone(
'''
SELECT COALESCE(MAX(resource_id), 0) AS last_resource_id
FROM resources
FOR UPDATE
'''
)
last_resource_id = last_resource_id['last_resource_id']

await tx.execute_many(
'''
INSERT INTO `resources` (resource, rate)
Expand All @@ -121,6 +130,15 @@ async def insert_or_update(tx):
resource_updates,
)

await tx.execute_update(
'''
UPDATE resources
SET deduped_resource_id = resource_id
WHERE resource_id > %s AND deduped_resource_id IS NULL
''',
(last_resource_id,),
)

if product_version_updates:
await tx.execute_many(
'''
Expand Down
3 changes: 3 additions & 0 deletions batch/sql/add-deduped-resource-ids-to-resources.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ALTER TABLE resources ADD COLUMN deduped_resource_id INT DEFAULT NULL, ALGORITHM=INSTANT;
CREATE INDEX `resources_deduped_resource_id` ON `resources` (`deduped_resource_id`);

2 changes: 2 additions & 0 deletions batch/sql/estimated-current.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ CREATE TABLE IF NOT EXISTS `resources` (
`resource` VARCHAR(100) NOT NULL,
`rate` DOUBLE NOT NULL,
`resource_id` INT AUTO_INCREMENT UNIQUE NOT NULL,
`deduped_resource_id` INT DEFAULT NULL,
PRIMARY KEY (`resource`)
) ENGINE = InnoDB;
CREATE INDEX `resources_deduped_resource_id` ON `resources` (`deduped_resource_id`);

CREATE TABLE IF NOT EXISTS `latest_product_versions` (
`product` VARCHAR(100) NOT NULL,
Expand Down
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2036,6 +2036,9 @@ steps:
- name: cleanup-old-billing-tables
script: /io/sql/cleanup-old-billing-tables.sql
online: true
- name: add-deduped-resource-ids-to-resources
script: /io/sql/add-deduped-resource-ids-to-resources.sql
online: true
inputs:
- from: /repo/batch/sql
to: /io/sql
Expand Down