PartialWrite applies incremental updates to the schema of a specific tenant based on the provided request.
(ctx context.Context, request *v1.SchemaPartialWriteRequest)
| 90 | |
| 91 | // PartialWrite applies incremental updates to the schema of a specific tenant based on the provided request. |
| 92 | func (r *SchemaServer) PartialWrite(ctx context.Context, request *v1.SchemaPartialWriteRequest) (*v1.SchemaPartialWriteResponse, error) { |
| 93 | // Start a new tracing span for monitoring and observability. |
| 94 | ctx, span := internal.Tracer.Start(ctx, "schemas.partial-write") |
| 95 | defer span.End() // Ensure the span is closed at the end of the function. |
| 96 | |
| 97 | // Retrieve or default the schema version from the request. |
| 98 | version := request.GetMetadata().GetSchemaVersion() |
| 99 | if version == "" { // If not provided, fetch the latest version. |
| 100 | ver, err := r.sr.HeadVersion(ctx, request.GetTenantId()) |
| 101 | if err != nil { |
| 102 | return nil, status.Error(GetStatus(err), err.Error()) // Return version error |
| 103 | } |
| 104 | version = ver |
| 105 | } |
| 106 | |
| 107 | // Fetch the current schema definition as a string. |
| 108 | definitions, err := r.sr.ReadSchemaString(ctx, request.GetTenantId(), version) |
| 109 | if err != nil { |
| 110 | span.RecordError(err) // Log and record the error. |
| 111 | return nil, status.Error(GetStatus(err), err.Error()) |
| 112 | } |
| 113 | |
| 114 | // Parse the schema definitions into a structured format. |
| 115 | p := parser.NewParser(strings.Join(definitions, "\n")) |
| 116 | schema, err := p.Parse() |
| 117 | if err != nil { |
| 118 | span.RecordError(err) // Log and record the error. |
| 119 | return nil, status.Error(GetStatus(err), err.Error()) |
| 120 | } |
| 121 | |
| 122 | // Iterate through each partial update in the request and apply changes. |
| 123 | for entityName, partials := range request.GetPartials() { |
| 124 | for _, write := range partials.GetWrite() { // Handle new schema statements. |
| 125 | pr := parser.NewParser(write) |
| 126 | stmt, err := pr.ParsePartial(entityName) |
| 127 | if err != nil { |
| 128 | span.RecordError(err) |
| 129 | return nil, status.Error(GetStatus(err), err.Error()) |
| 130 | } |
| 131 | err = schema.AddStatement(entityName, stmt) |
| 132 | if err != nil { |
| 133 | span.RecordError(err) |
| 134 | return nil, status.Error(GetStatus(err), err.Error()) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | for _, update := range partials.GetUpdate() { // Handle schema updates. |
| 139 | pr := parser.NewParser(update) |
| 140 | stmt, err := pr.ParsePartial(entityName) |
| 141 | if err != nil { |
| 142 | span.RecordError(err) |
| 143 | return nil, status.Error(GetStatus(err), err.Error()) |
| 144 | } |
| 145 | err = schema.UpdateStatement(entityName, stmt) |
| 146 | if err != nil { |
| 147 | span.RecordError(err) |
| 148 | return nil, status.Error(GetStatus(err), err.Error()) |
| 149 | } |
nothing calls this directly
no test coverage detected