Skip to content

Commit b472835

Browse files
Abl UUID lower (#650)
* Ensure uuid is lowercase * Update base image version for dev frontend image * Ensure uuids from rex release are lowercase * Account for database book uuid letter casing Also add similar safety to commit sha, just in case * Ensure uuid and commit_sha are lowercase in api response * Add some additional validation for uuids
1 parent 7cb3bc8 commit b472835

File tree

13 files changed

+75
-44
lines changed

13 files changed

+75
-44
lines changed

backend/app/app/data_models/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class ApprovedBook(RequestApproveBook):
8686
class Config:
8787
class Getter(GetterDict):
8888
getters = {
89-
"uuid": lambda self: self._obj.book.uuid,
90-
"commit_sha": lambda self: self._obj.book.commit.sha,
89+
"uuid": lambda self: self._obj.book.uuid.lower(),
90+
"commit_sha": lambda self: self._obj.book.commit.sha.lower(),
9191
"code_version": lambda self: self._obj.code_version.version,
9292
"consumer": lambda self: self._obj.consumer.name,
9393
"created_at": lambda self: self._obj.created_at,

backend/app/app/service/abl.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from typing import Any, Dict, List, Optional
33

44
from httpx import AsyncClient, HTTPStatusError
5-
from sqlalchemy import and_, delete, or_, select
5+
from sqlalchemy import and_, delete, func, or_, select
66
from sqlalchemy.orm import Session, lazyload
77

88
from app.core import config
@@ -36,7 +36,7 @@ async def get_rex_release_json(client: AsyncClient):
3636

3737
async def get_rex_books(client: AsyncClient):
3838
release_json = await get_rex_release_json(client)
39-
return release_json["books"]
39+
return {uuid.lower(): v for uuid, v in release_json["books"].items()}
4040

4141

4242
async def get_rex_release_version(client: AuthenticatedClient):
@@ -65,7 +65,7 @@ def get_rex_book_versions(rex_books: Dict[str, Any], book_uuids: List[str]):
6565
version is not None
6666
), f"Could not get defaultVersion for {book_uuid}"
6767
rex_book_versions.append(
68-
BaseApprovedBook(commit_sha=version, uuid=book_uuid)
68+
BaseApprovedBook(commit_sha=version.lower(), uuid=book_uuid)
6969
)
7070
return rex_book_versions
7171

@@ -84,18 +84,20 @@ def remove_old_versions(
8484
.options(lazyload("*"))
8585
.join(Book)
8686
.join(Commit)
87-
.where(Book.uuid.in_([b.uuid for b in to_add]))
87+
.where(func.lower(Book.uuid).in_([b.uuid for b in to_add]))
8888
.where(ApprovedBook.consumer_id == consumer_id)
8989
)
9090
have = db.scalars(query).all()
9191
to_delete = []
9292
for record in have:
93-
ident = (record.book.uuid, record.book.commit.sha[:7])
93+
record_uuid = record.book.uuid.lower()
94+
record_sha = record.book.commit.sha[:7].lower()
95+
ident = (record_uuid, record_sha)
9496
# Delete entries that exist in both to_add and to_keep
9597
# Do not delete existing entries that share a uuid with an intersection
9698
if ident in intersection or (
9799
ident not in keep_uuid_sha
98-
and not any(record.book.uuid == uuid for uuid, _ in intersection)
100+
and not any(record_uuid == uuid for uuid, _ in intersection)
99101
):
100102
to_delete.append(record)
101103
# NOTE: If to_delete is empty, the condition will be True (delete all)
@@ -181,6 +183,14 @@ async def add_new_entries(
181183
raise CustomBaseError(
182184
f"Found multiple versions for {uuid} - ({items})"
183185
)
186+
to_add = [
187+
RequestApproveBook(
188+
commit_sha=entry.commit_sha.lower(),
189+
uuid=entry.uuid.lower(),
190+
code_version=entry.code_version,
191+
)
192+
for entry in to_add
193+
]
184194
db_books = db.scalars(
185195
select(Book)
186196
.options(lazyload("*"))
@@ -189,14 +199,15 @@ async def add_new_entries(
189199
or_(
190200
*[
191201
and_(
192-
Book.uuid == entry.uuid, Commit.sha == entry.commit_sha
202+
func.lower(Book.uuid) == entry.uuid,
203+
func.lower(Commit.sha) == entry.commit_sha,
193204
)
194205
for entry in to_add
195206
]
196207
)
197208
)
198209
).all()
199-
db_books_by_uuid = {dbb.uuid: dbb for dbb in db_books}
210+
db_books_by_uuid = {dbb.uuid.lower(): dbb for dbb in db_books}
200211
book_info_by_consumer = groupby(
201212
to_add, lambda entry: guess_consumer(db_books_by_uuid[entry.uuid].slug)
202213
)

backend/app/app/service/jobs.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
from datetime import datetime
44
from typing import Dict, Generator, List, Optional, Union, cast
5+
from uuid import UUID
56

67
from lxml import etree
78
from sqlalchemy.exc import IntegrityError
@@ -64,6 +65,10 @@ def add_books_to_commit(
6465
uuid = xpath1(collection, "//*[local-name()='uuid']")
6566
if uuid is None or not uuid.text:
6667
raise CustomBaseError("Could not get uuid from collection xml")
68+
try:
69+
_ = UUID(uuid.text)
70+
except ValueError as ve:
71+
raise CustomBaseError(f"Invalid UUID: {uuid.text}") from ve
6772
# TODO: Edition should be either nullable or in a different table
6873
db_book = Book(uuid=uuid.text, slug=slug, edition=0, style=style)
6974
commit.books.append(db_book)

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add0-to_keep0/add_new_entries.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
SELECT book.id, book.uuid, book.commit_id, book.edition, book.slug, book.style
22
FROM book JOIN commit ON commit.id = book.commit_id
3-
WHERE book.uuid = :uuid_1 AND commit.sha = :sha_1
3+
WHERE lower(book.uuid) = :lower_1 AND lower(commit.sha) = :lower_2
44

55
SELECT consumer.id
66
FROM consumer
77
WHERE consumer.name = :name_1
88

99
SELECT approved_book.book_id, approved_book.consumer_id, approved_book.code_version_id, approved_book.created_at, approved_book.updated_at
1010
FROM approved_book JOIN book ON book.id = approved_book.book_id JOIN commit ON commit.id = book.commit_id
11-
WHERE book.uuid IN (__[POSTCOMPILE_uuid_1]) AND approved_book.consumer_id = :consumer_id_1
11+
WHERE lower(book.uuid) IN (__[POSTCOMPILE_lower_1]) AND approved_book.consumer_id = :consumer_id_1
1212

1313
DELETE FROM approved_book WHERE approved_book.consumer_id = :consumer_id_1 AND approved_book.book_id = :book_id_1 AND approved_book.code_version_id = :code_version_id_1
1414

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add0-to_keep0/add_new_entries_params.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[
22
{
3-
"uuid_1": "uuid-book-b",
4-
"sha_1": "commit1"
3+
"lower_1": "uuid-book-b",
4+
"lower_2": "commit1"
55
},
66
{
77
"name_1": "REX"
88
},
99
{
10-
"uuid_1": [
10+
"lower_1": [
1111
"uuid-book-b"
1212
],
1313
"consumer_id_1": 1

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add1-to_keep1/add_new_entries.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
SELECT book.id, book.uuid, book.commit_id, book.edition, book.slug, book.style
22
FROM book JOIN commit ON commit.id = book.commit_id
3-
WHERE book.uuid = :uuid_1 AND commit.sha = :sha_1
3+
WHERE lower(book.uuid) = :lower_1 AND lower(commit.sha) = :lower_2
44

55
SELECT consumer.id
66
FROM consumer
77
WHERE consumer.name = :name_1
88

99
SELECT approved_book.book_id, approved_book.consumer_id, approved_book.code_version_id, approved_book.created_at, approved_book.updated_at
1010
FROM approved_book JOIN book ON book.id = approved_book.book_id JOIN commit ON commit.id = book.commit_id
11-
WHERE book.uuid IN (__[POSTCOMPILE_uuid_1]) AND approved_book.consumer_id = :consumer_id_1
11+
WHERE lower(book.uuid) IN (__[POSTCOMPILE_lower_1]) AND approved_book.consumer_id = :consumer_id_1
1212

1313
DELETE FROM approved_book WHERE approved_book.consumer_id = :consumer_id_1 AND approved_book.book_id = :book_id_1 AND approved_book.code_version_id = :code_version_id_1
1414

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add1-to_keep1/add_new_entries_params.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[
22
{
3-
"uuid_1": "uuid-book-a",
4-
"sha_1": "commit-new"
3+
"lower_1": "uuid-book-a",
4+
"lower_2": "commit-new"
55
},
66
{
77
"name_1": "REX"
88
},
99
{
10-
"uuid_1": [
10+
"lower_1": [
1111
"uuid-book-a"
1212
],
1313
"consumer_id_1": 1

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add2-to_keep2/add_new_entries.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
SELECT book.id, book.uuid, book.commit_id, book.edition, book.slug, book.style
22
FROM book JOIN commit ON commit.id = book.commit_id
3-
WHERE book.uuid = :uuid_1 AND commit.sha = :sha_1 OR book.uuid = :uuid_2 AND commit.sha = :sha_2
3+
WHERE lower(book.uuid) = :lower_1 AND lower(commit.sha) = :lower_2 OR lower(book.uuid) = :lower_3 AND lower(commit.sha) = :lower_4
44

55
SELECT consumer.id
66
FROM consumer
77
WHERE consumer.name = :name_1
88

99
SELECT approved_book.book_id, approved_book.consumer_id, approved_book.code_version_id, approved_book.created_at, approved_book.updated_at
1010
FROM approved_book JOIN book ON book.id = approved_book.book_id JOIN commit ON commit.id = book.commit_id
11-
WHERE book.uuid IN (__[POSTCOMPILE_uuid_1]) AND approved_book.consumer_id = :consumer_id_1
11+
WHERE lower(book.uuid) IN (__[POSTCOMPILE_lower_1]) AND approved_book.consumer_id = :consumer_id_1
1212

1313
DELETE FROM approved_book WHERE approved_book.consumer_id = :consumer_id_1 AND approved_book.book_id = :book_id_1 AND approved_book.code_version_id = :code_version_id_1 OR approved_book.consumer_id = :consumer_id_2 AND approved_book.book_id = :book_id_2 AND approved_book.code_version_id = :code_version_id_2
1414

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add2-to_keep2/add_new_entries_params.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
[
22
{
3-
"uuid_1": "uuid-book-a",
4-
"sha_1": "commit1",
5-
"uuid_2": "uuid-book-b",
6-
"sha_2": "commit3"
3+
"lower_1": "uuid-book-a",
4+
"lower_2": "commit1",
5+
"lower_3": "uuid-book-b",
6+
"lower_4": "commit3"
77
},
88
{
99
"name_1": "REX"
1010
},
1111
{
12-
"uuid_1": [
12+
"lower_1": [
1313
"uuid-book-a",
1414
"uuid-book-b"
1515
],

backend/app/tests/unit/snapshots/test_abl/test_add_new_entries_rex/to_add3-to_keep3/add_new_entries.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
SELECT book.id, book.uuid, book.commit_id, book.edition, book.slug, book.style
22
FROM book JOIN commit ON commit.id = book.commit_id
3-
WHERE book.uuid = :uuid_1 AND commit.sha = :sha_1
3+
WHERE lower(book.uuid) = :lower_1 AND lower(commit.sha) = :lower_2
44

55
SELECT consumer.id
66
FROM consumer
77
WHERE consumer.name = :name_1
88

99
SELECT approved_book.book_id, approved_book.consumer_id, approved_book.code_version_id, approved_book.created_at, approved_book.updated_at
1010
FROM approved_book JOIN book ON book.id = approved_book.book_id JOIN commit ON commit.id = book.commit_id
11-
WHERE book.uuid IN (__[POSTCOMPILE_uuid_1]) AND approved_book.consumer_id = :consumer_id_1
11+
WHERE lower(book.uuid) IN (__[POSTCOMPILE_lower_1]) AND approved_book.consumer_id = :consumer_id_1
1212

1313
DELETE FROM approved_book WHERE approved_book.consumer_id = :consumer_id_1 AND approved_book.book_id = :book_id_1 AND approved_book.code_version_id = :code_version_id_1
1414

0 commit comments

Comments
 (0)