Evolving a Schema
What is safe to change, what is not, and reserved.
Every rule here follows from chapter 2: the field number is the only identity on the wire.
What is safe
Because names are not transmitted, and unknown fields are kept:
| Change | Safe |
|---|---|
| Add a field with a new number | yes |
| Rename a field | yes |
| Add a method | yes |
Add an enum value |
yes |
| Reorder declarations in the file | yes |
Renaming a field
Two schemas, same numbers, different names:
syntax = "proto3";
package ev1;
option go_package = "xz/grpc/ev1pb";
message User {
string name = 1;
int32 age = 2;
}syntax = "proto3";
package ev2;
option go_package = "xz/grpc/ev2pb";
message User {
string full_name = 1;
int32 age = 2;
string email = 3;
}Marshal with the first, read with the second:
package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"xz/grpc/ev1pb"
"xz/grpc/ev2pb"
)
func main() {
wire, _ := proto.Marshal(&ev1pb.User{Name: "ada", Age: 36})
var renamed ev2pb.User
proto.Unmarshal(wire, &renamed)
fmt.Printf("renamed field: %q age %d email %q\n",
renamed.GetFullName(), renamed.GetAge(), renamed.GetEmail())
}name became full_name and the value arrived. The added email is simply absent.
What is not
These compile, deploy, and lose data:
| Change | Breaks |
|---|---|
| Change a field’s number | the field, silently |
| Change a field’s type | the field, silently |
| Reuse a retired number | old and new readers disagree |
| Remove a method still called | Unimplemented at runtime |
Change a package name |
the service’s wire name |
Swap two numbers and read the old bytes:
syntax = "proto3";
package ev3;
option go_package = "xz/grpc/ev3pb";
message User {
string name = 2;
int32 age = 1;
}package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"xz/grpc/ev1pb"
"xz/grpc/ev3pb"
)
func main() {
wire, _ := proto.Marshal(&ev1pb.User{Name: "ada", Age: 36})
var swapped ev3pb.User
err := proto.Unmarshal(wire, &swapped)
fmt.Println("renumbered unmarshal err:", err)
fmt.Printf("renumbered: name %q age %d\n", swapped.GetName(), swapped.GetAge())
}Changing a type
int32 to string on the same number, which looks harmless in a diff:
syntax = "proto3";
package ev4;
option go_package = "xz/grpc/ev4pb";
message User {
string name = 1;
string age = 2;
}package main
import (
"fmt"
"google.golang.org/protobuf/proto"
"xz/grpc/ev1pb"
"xz/grpc/ev4pb"
)
func main() {
wire, _ := proto.Marshal(&ev1pb.User{Name: "ada", Age: 36})
var changed ev4pb.User
err := proto.Unmarshal(wire, &changed)
fmt.Println("int32 -> string err:", err)
fmt.Printf("name %q age %q\n", changed.GetName(), changed.GetAge())
}name survived, age is gone, and the call succeeded. This is the failure mode to fear: a
partially decoded message that no code path treats as an error.
reserved
Deleting a field frees its number for reuse, which is the trap. reserved keeps it taken:
syntax = "proto3";
package res;
option go_package = "xz/grpc/respb";
message User {
string name = 1;
reserved 2;
reserved "age";
string email = 3;
}Reuse it anyway and the compiler stops you:
trueDeprecating a method
Removing an rpc breaks callers you cannot see. Mark it instead:
service Users {
rpc GetUser(GetUserRequest) returns (User) {
option deprecated = true;
}
rpc BatchGetUsers(BatchGetUsersRequest) returns (BatchGetUsersResponse);
}The generated Go carries a // Deprecated: comment, so editors and staticcheck flag callers.
Versioning a package
When a change cannot be made compatibly, the package name carries the version:
package users.v1;
package users.v2;Both can be registered on one server, since the service’s full name includes the package. Old
clients keep calling users.v1.
buf lint and buf breaking
buf lint checks the conventions, including the versioned package name from above:
buf lint proto 2>&1 | wc -l | xargs echo findings
buf lint proto 2>&1 | head -1 | cut -d: -f1-3
buf lint proto 2>&1 | head -1 | cut -d: -f4- | fold -s -w 42buf breaking compares against a previous version and enforces this whole chapter mechanically:
trueThat is the same change that returned a nil error and dropped data at runtime. Here it fails a
build instead.