Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/cas/storage/index/sql_dialect_delegates/postgresqldelegate.py: 100.00%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

15 statements  

1# Copyright (C) 2020 Bloomberg LP 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# <http://www.apache.org/licenses/LICENSE-2.0> 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15""" 

16PostgreSQLDelegate 

17================== 

18 

19Extra functionality for the SQL index when using a PostgreSQL backend. 

20 

21""" 

22from datetime import datetime 

23from typing import List 

24 

25from sqlalchemy.dialects.postgresql import insert 

26from sqlalchemy.orm.session import Session as SessionType 

27 

28from buildgrid._protos.build.bazel.remote.execution.v2.remote_execution_pb2 import Digest 

29from buildgrid.server.persistence.sql.models import IndexEntry 

30 

31 

32class PostgreSQLDelegate: 

33 

34 @staticmethod 

35 def _save_digests_to_index(digests: List[Digest], session: SessionType) -> None: 

36 index_table = IndexEntry.__table__ 

37 update_time = datetime.utcnow() 

38 new_rows = [{ 

39 'digest_hash': digest.hash, 

40 'digest_size_bytes': digest.size_bytes, 

41 'accessed_timestamp': update_time 

42 } for digest in digests] 

43 stmt = insert(index_table).values(new_rows).on_conflict_do_update( 

44 index_elements=['digest_hash'], 

45 set_={'accessed_timestamp': update_time} 

46 ) 

47 session.execute(stmt)