Skip to content
Sign inGet all access
Chapter 7·14 minFree

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:

proto/ev1/ev1.proto
syntax = "proto3";
package ev1;
option go_package = "xz/grpc/ev1pb";
message User {
  string name = 1;
  int32 age = 2;
}
proto/ev2/ev2.proto
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())
}
Outputrenamed field: "ada" age 36 email ""

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:

proto/ev3/ev3.proto
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())
}
Outputrenumbered unmarshal err: <nil>renumbered: name "" age 0

Changing a type

int32 to string on the same number, which looks harmless in a diff:

proto/ev4/ev4.proto
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())
}
Outputint32 -> string err: <nil>name "ada" age ""

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:

proto/res/res.proto
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:

terminal
true
Output$ buf generate protoproto/res/res.proto:7:15:use of reserved field number `2`

Deprecating a method

Removing an rpc breaks callers you cannot see. Mark it instead:

shown
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:

shown
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 42
Outputfindings 5proto/ev1/ev1.proto:2:1Package name "ev1" should be suffixed with a correctly formed version, such as "ev1.v1".

buf breaking compares against a previous version and enforces this whole chapter mechanically:

terminal
true
Output$ buf breaking proto --against .git#branch=mainproto/res/res.proto:5:3:Field "1" with name "name"on message "User" changed type from "string" to"int32".

That is the same change that returned a nil error and dropped data at runtime. Here it fails a build instead.