1- from typing import TYPE_CHECKING , Union
1+ from __future__ import annotations
2+
3+ from typing import TYPE_CHECKING , Self , Union
24
35from graphene import Boolean , Field , InputObjectType , Mutation , String
4- from graphql import GraphQLResolveInfo
56
6- from infrahub import config , lock
7+ from infrahub import lock
78from infrahub .core import registry
8- from infrahub .core .branch import Branch
99from infrahub .core .constants import RESTRICTED_NAMESPACES
1010from infrahub .core .manager import NodeManager
1111from infrahub .core .schema import DropdownChoice , GenericSchema , NodeSchema
1212from infrahub .database import InfrahubDatabase , retry_db_transaction
13+ from infrahub .events import EventMeta
14+ from infrahub .events .schema_action import SchemaUpdatedEvent
1315from infrahub .exceptions import ValidationError
14- from infrahub .log import get_logger
15- from infrahub .message_bus import Meta , messages
16- from infrahub .services import services
16+ from infrahub .log import get_log_data , get_logger
1717from infrahub .worker import WORKER_IDENTITY
1818
1919from ..types import DropdownFields
2020
2121if TYPE_CHECKING :
22+ from graphql import GraphQLResolveInfo
23+
24+ from infrahub .core .branch import Branch
25+ from infrahub .services import InfrahubServices
26+
2227 from ..initialization import GraphqlContext
2328
2429log = get_logger ()
@@ -56,7 +61,7 @@ async def mutate(
5661 root : dict , # pylint: disable=unused-argument
5762 info : GraphQLResolveInfo ,
5863 data : SchemaDropdownAddInput ,
59- ):
64+ ) -> Self :
6065 context : GraphqlContext = info .context
6166
6267 kind = context .db .schema .get (name = str (data .kind ), branch = context .branch .name )
@@ -73,7 +78,13 @@ async def mutate(
7378 )
7479 attrib .choices .append (choice )
7580
76- await update_registry (kind = kind , branch = context .branch , db = context .db )
81+ await update_registry (
82+ kind = kind ,
83+ branch = context .branch ,
84+ db = context .db ,
85+ account_id = context .active_account_session .account_id ,
86+ service = context .active_service ,
87+ )
7788
7889 kind = context .db .schema .get (name = str (data .kind ), branch = context .branch .name )
7990 attrib = kind .get_attribute (attribute )
@@ -130,7 +141,13 @@ async def mutate(
130141 raise ValidationError (f"Unable to remove the last dropdown on { kind .kind } in attribute { attribute } " )
131142 attrib .choices = [entry for entry in attrib .choices if dropdown != entry .name ]
132143
133- await update_registry (kind = kind , branch = context .branch , db = context .db )
144+ await update_registry (
145+ kind = kind ,
146+ branch = context .branch ,
147+ db = context .db ,
148+ account_id = context .active_account_session .account_id ,
149+ service = context .active_service ,
150+ )
134151
135152 return {"ok" : True }
136153
@@ -165,7 +182,13 @@ async def mutate(
165182 )
166183 attrib .enum .append (enum )
167184
168- await update_registry (kind = kind , branch = context .branch , db = context .db )
185+ await update_registry (
186+ kind = kind ,
187+ branch = context .branch ,
188+ db = context .db ,
189+ account_id = context .active_account_session .account_id ,
190+ service = context .active_service ,
191+ )
169192
170193 return {"ok" : True }
171194
@@ -206,7 +229,13 @@ async def mutate(
206229 raise ValidationError (f"Unable to remove the last enum on { kind .kind } in attribute { attribute } " )
207230 attrib .enum = [entry for entry in attrib .enum if entry != enum ]
208231
209- await update_registry (kind = kind , branch = branch , db = db )
232+ await update_registry (
233+ kind = kind ,
234+ branch = branch ,
235+ db = db ,
236+ account_id = context .active_account_session .account_id ,
237+ service = context .active_service ,
238+ )
210239
211240 return {"ok" : True }
212241
@@ -237,7 +266,9 @@ def validate_kind(kind: Union[GenericSchema, NodeSchema], attribute: str) -> Non
237266 raise ValidationError (f"Attribute { attribute } on { kind .kind } is inherited and must be changed on the generic" )
238267
239268
240- async def update_registry (kind : NodeSchema , branch : Branch , db : InfrahubDatabase ) -> None :
269+ async def update_registry (
270+ kind : NodeSchema , db : InfrahubDatabase , branch : Branch , account_id : str , service : InfrahubServices
271+ ) -> None :
241272 async with lock .registry .global_schema_lock ():
242273 branch_schema = registry .schema .get_schema_branch (name = branch .name )
243274
@@ -256,13 +287,20 @@ async def update_registry(kind: NodeSchema, branch: Branch, db: InfrahubDatabase
256287 schema = tmp_schema , db = dbt , branch = branch .name , limit = diff .all , update_db = True
257288 )
258289 branch .update_schema_hash ()
259- log .info ("Schema has been updated" , branch = branch .name , hash = branch .schema_hash .main )
290+ log .info ("Schema has been updated" , branch = branch .name , hash = branch .active_schema_hash .main )
260291 await branch .save (db = dbt )
261292
262- if config .SETTINGS .broker .enable :
263- message = messages .EventSchemaUpdate (
264- branch = branch .name ,
265- meta = Meta (initiator_id = WORKER_IDENTITY ),
266- )
267- await services .send (message )
268- await services .service .component .refresh_schema_hash (branches = [branch .name ])
293+ log_data = get_log_data ()
294+ request_id = log_data .get ("request_id" , "" )
295+ event = SchemaUpdatedEvent (
296+ branch = branch .name ,
297+ schema_hash = branch .active_schema_hash .main ,
298+ meta = EventMeta (
299+ initiator_id = WORKER_IDENTITY ,
300+ request_id = request_id ,
301+ account_id = account_id ,
302+ ),
303+ )
304+ await service .event .send (event = event )
305+
306+ await service .component .refresh_schema_hash (branches = [branch .name ])
0 commit comments