Coverage for /builds/BuildGrid/buildgrid/buildgrid/server/sql/alembic/versions/e83194af8292_add_a_table_for_request_metadata.py: 76.47%

17 statements  

« prev     ^ index     » next       coverage.py v7.4.1, created at 2024-10-04 17:48 +0000

1# Copyright (C) 2024 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"""Add a table for request metadata 

16 

17Revision ID: e83194af8292 

18Revises: 316ad77858af 

19Create Date: 2024-06-14 12:49:52.934372 

20 

21""" 

22 

23import sqlalchemy as sa 

24from alembic import op 

25 

26# revision identifiers, used by Alembic. 

27revision = "e83194af8292" 

28down_revision = "316ad77858af" 

29branch_labels = None 

30depends_on = None 

31 

32 

33def upgrade() -> None: 

34 op.create_table( 

35 "request_metadata", 

36 sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), 

37 sa.Column("tool_name", sa.String(), nullable=True), 

38 sa.Column("tool_version", sa.String(), nullable=True), 

39 sa.Column("invocation_id", sa.String(), nullable=True), 

40 sa.Column("correlated_invocations_id", sa.String(), nullable=True), 

41 sa.Column("action_mnemonic", sa.String(), nullable=True), 

42 sa.Column("target_id", sa.String(), nullable=True), 

43 sa.Column("configuration_id", sa.String(), nullable=True), 

44 sa.PrimaryKeyConstraint("id"), 

45 sa.UniqueConstraint( 

46 "tool_name", 

47 "tool_version", 

48 "invocation_id", 

49 "correlated_invocations_id", 

50 "action_mnemonic", 

51 "target_id", 

52 "configuration_id", 

53 name="unique_metadata_constraint", 

54 ), 

55 ) 

56 

57 # We need to use `batch_alter_table` for SQLite, where `ALTER TABLE` isn't available 

58 with op.batch_alter_table("operations") as batch_op: 

59 batch_op.add_column(sa.Column("request_metadata_id", sa.Integer(), nullable=True)) 

60 batch_op.create_foreign_key( 

61 "fk_operation_request_metadata", "request_metadata", ["request_metadata_id"], ["id"] 

62 ) 

63 

64 

65def downgrade() -> None: 

66 with op.batch_alter_table("operations") as batch_op: 

67 batch_op.drop_constraint("fk_operation_request_metadata", type_="foreignkey") 

68 batch_op.drop_column("request_metadata_id") 

69 

70 op.drop_table("request_metadata")