Basic preupdate hook support at DB level

This commit is contained in:
Philip O'Toole
2025-01-22 22:57:14 -05:00
committed by GitHub
parent 63b2c9c3e3
commit abd78d762b
5 changed files with 832 additions and 42 deletions

View File

@@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.1
// protoc-gen-go v1.36.2
// protoc v3.6.1
// source: message.proto

View File

@@ -1,6 +1,6 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.36.1
// protoc-gen-go v1.36.2
// protoc v3.6.1
// source: command.proto
@@ -188,6 +188,58 @@ func (Command_Type) EnumDescriptor() ([]byte, []int) {
return file_command_proto_rawDescGZIP(), []int{17, 0}
}
type CDCEvent_Operation int32
const (
CDCEvent_UNKNOWN CDCEvent_Operation = 0
CDCEvent_INSERT CDCEvent_Operation = 1
CDCEvent_UPDATE CDCEvent_Operation = 2
CDCEvent_DELETE CDCEvent_Operation = 3
)
// Enum value maps for CDCEvent_Operation.
var (
CDCEvent_Operation_name = map[int32]string{
0: "UNKNOWN",
1: "INSERT",
2: "UPDATE",
3: "DELETE",
}
CDCEvent_Operation_value = map[string]int32{
"UNKNOWN": 0,
"INSERT": 1,
"UPDATE": 2,
"DELETE": 3,
}
)
func (x CDCEvent_Operation) Enum() *CDCEvent_Operation {
p := new(CDCEvent_Operation)
*p = x
return p
}
func (x CDCEvent_Operation) String() string {
return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
}
func (CDCEvent_Operation) Descriptor() protoreflect.EnumDescriptor {
return file_command_proto_enumTypes[3].Descriptor()
}
func (CDCEvent_Operation) Type() protoreflect.EnumType {
return &file_command_proto_enumTypes[3]
}
func (x CDCEvent_Operation) Number() protoreflect.EnumNumber {
return protoreflect.EnumNumber(x)
}
// Deprecated: Use CDCEvent_Operation.Descriptor instead.
func (CDCEvent_Operation) EnumDescriptor() ([]byte, []int) {
return file_command_proto_rawDescGZIP(), []int{20, 0}
}
type Parameter struct {
state protoimpl.MessageState `protogen:"open.v1"`
// Types that are valid to be assigned to Value:
@@ -1392,6 +1444,272 @@ func (x *Command) GetCompressed() bool {
return false
}
type CDCValue struct {
state protoimpl.MessageState `protogen:"open.v1"`
// Types that are valid to be assigned to Value:
//
// *CDCValue_I
// *CDCValue_D
// *CDCValue_B
// *CDCValue_Y
// *CDCValue_S
Value isCDCValue_Value `protobuf_oneof:"value"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CDCValue) Reset() {
*x = CDCValue{}
mi := &file_command_proto_msgTypes[18]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CDCValue) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CDCValue) ProtoMessage() {}
func (x *CDCValue) ProtoReflect() protoreflect.Message {
mi := &file_command_proto_msgTypes[18]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CDCValue.ProtoReflect.Descriptor instead.
func (*CDCValue) Descriptor() ([]byte, []int) {
return file_command_proto_rawDescGZIP(), []int{18}
}
func (x *CDCValue) GetValue() isCDCValue_Value {
if x != nil {
return x.Value
}
return nil
}
func (x *CDCValue) GetI() int64 {
if x != nil {
if x, ok := x.Value.(*CDCValue_I); ok {
return x.I
}
}
return 0
}
func (x *CDCValue) GetD() float64 {
if x != nil {
if x, ok := x.Value.(*CDCValue_D); ok {
return x.D
}
}
return 0
}
func (x *CDCValue) GetB() bool {
if x != nil {
if x, ok := x.Value.(*CDCValue_B); ok {
return x.B
}
}
return false
}
func (x *CDCValue) GetY() []byte {
if x != nil {
if x, ok := x.Value.(*CDCValue_Y); ok {
return x.Y
}
}
return nil
}
func (x *CDCValue) GetS() string {
if x != nil {
if x, ok := x.Value.(*CDCValue_S); ok {
return x.S
}
}
return ""
}
type isCDCValue_Value interface {
isCDCValue_Value()
}
type CDCValue_I struct {
I int64 `protobuf:"zigzag64,1,opt,name=i,proto3,oneof"`
}
type CDCValue_D struct {
D float64 `protobuf:"fixed64,2,opt,name=d,proto3,oneof"`
}
type CDCValue_B struct {
B bool `protobuf:"varint,3,opt,name=b,proto3,oneof"`
}
type CDCValue_Y struct {
Y []byte `protobuf:"bytes,4,opt,name=y,proto3,oneof"`
}
type CDCValue_S struct {
S string `protobuf:"bytes,5,opt,name=s,proto3,oneof"`
}
func (*CDCValue_I) isCDCValue_Value() {}
func (*CDCValue_D) isCDCValue_Value() {}
func (*CDCValue_B) isCDCValue_Value() {}
func (*CDCValue_Y) isCDCValue_Value() {}
func (*CDCValue_S) isCDCValue_Value() {}
type CDCRow struct {
state protoimpl.MessageState `protogen:"open.v1"`
Values []*CDCValue `protobuf:"bytes,1,rep,name=values,proto3" json:"values,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CDCRow) Reset() {
*x = CDCRow{}
mi := &file_command_proto_msgTypes[19]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CDCRow) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CDCRow) ProtoMessage() {}
func (x *CDCRow) ProtoReflect() protoreflect.Message {
mi := &file_command_proto_msgTypes[19]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CDCRow.ProtoReflect.Descriptor instead.
func (*CDCRow) Descriptor() ([]byte, []int) {
return file_command_proto_rawDescGZIP(), []int{19}
}
func (x *CDCRow) GetValues() []*CDCValue {
if x != nil {
return x.Values
}
return nil
}
type CDCEvent struct {
state protoimpl.MessageState `protogen:"open.v1"`
Error string `protobuf:"bytes,1,opt,name=error,proto3" json:"error,omitempty"`
Op CDCEvent_Operation `protobuf:"varint,2,opt,name=op,proto3,enum=command.CDCEvent_Operation" json:"op,omitempty"`
Table string `protobuf:"bytes,3,opt,name=table,proto3" json:"table,omitempty"`
OldRowId int64 `protobuf:"varint,4,opt,name=old_row_id,json=oldRowId,proto3" json:"old_row_id,omitempty"`
NewRowId int64 `protobuf:"varint,5,opt,name=new_row_id,json=newRowId,proto3" json:"new_row_id,omitempty"`
OldRow *CDCRow `protobuf:"bytes,6,opt,name=old_row,json=oldRow,proto3" json:"old_row,omitempty"`
NewRow *CDCRow `protobuf:"bytes,7,opt,name=new_row,json=newRow,proto3" json:"new_row,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
}
func (x *CDCEvent) Reset() {
*x = CDCEvent{}
mi := &file_command_proto_msgTypes[20]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
func (x *CDCEvent) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*CDCEvent) ProtoMessage() {}
func (x *CDCEvent) ProtoReflect() protoreflect.Message {
mi := &file_command_proto_msgTypes[20]
if x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use CDCEvent.ProtoReflect.Descriptor instead.
func (*CDCEvent) Descriptor() ([]byte, []int) {
return file_command_proto_rawDescGZIP(), []int{20}
}
func (x *CDCEvent) GetError() string {
if x != nil {
return x.Error
}
return ""
}
func (x *CDCEvent) GetOp() CDCEvent_Operation {
if x != nil {
return x.Op
}
return CDCEvent_UNKNOWN
}
func (x *CDCEvent) GetTable() string {
if x != nil {
return x.Table
}
return ""
}
func (x *CDCEvent) GetOldRowId() int64 {
if x != nil {
return x.OldRowId
}
return 0
}
func (x *CDCEvent) GetNewRowId() int64 {
if x != nil {
return x.NewRowId
}
return 0
}
func (x *CDCEvent) GetOldRow() *CDCRow {
if x != nil {
return x.OldRow
}
return nil
}
func (x *CDCEvent) GetNewRow() *CDCRow {
if x != nil {
return x.NewRow
}
return nil
}
var File_command_proto protoreflect.FileDescriptor
var file_command_proto_rawDesc = []byte{
@@ -1556,10 +1874,39 @@ var file_command_proto_rawDesc = []byte{
0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x45, 0x58, 0x45, 0x43, 0x55, 0x54, 0x45, 0x5f,
0x51, 0x55, 0x45, 0x52, 0x59, 0x10, 0x06, 0x12, 0x1b, 0x0a, 0x17, 0x43, 0x4f, 0x4d, 0x4d, 0x41,
0x4e, 0x44, 0x5f, 0x54, 0x59, 0x50, 0x45, 0x5f, 0x4c, 0x4f, 0x41, 0x44, 0x5f, 0x43, 0x48, 0x55,
0x4e, 0x4b, 0x10, 0x07, 0x42, 0x2b, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63,
0x6f, 0x6d, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65,
0x2f, 0x76, 0x38, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
0x4e, 0x4b, 0x10, 0x07, 0x22, 0x63, 0x0a, 0x08, 0x43, 0x44, 0x43, 0x56, 0x61, 0x6c, 0x75, 0x65,
0x12, 0x0e, 0x0a, 0x01, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x12, 0x48, 0x00, 0x52, 0x01, 0x69,
0x12, 0x0e, 0x0a, 0x01, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x48, 0x00, 0x52, 0x01, 0x64,
0x12, 0x0e, 0x0a, 0x01, 0x62, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x48, 0x00, 0x52, 0x01, 0x62,
0x12, 0x0e, 0x0a, 0x01, 0x79, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x01, 0x79,
0x12, 0x0e, 0x0a, 0x01, 0x73, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x01, 0x73,
0x42, 0x07, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x22, 0x33, 0x0a, 0x06, 0x43, 0x44, 0x43,
0x52, 0x6f, 0x77, 0x12, 0x29, 0x0a, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x18, 0x01, 0x20,
0x03, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x43, 0x44,
0x43, 0x56, 0x61, 0x6c, 0x75, 0x65, 0x52, 0x06, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x73, 0x22, 0xb1,
0x02, 0x0a, 0x08, 0x43, 0x44, 0x43, 0x45, 0x76, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x65,
0x72, 0x72, 0x6f, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x65, 0x72, 0x72, 0x6f,
0x72, 0x12, 0x2b, 0x0a, 0x02, 0x6f, 0x70, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x1b, 0x2e,
0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x43, 0x44, 0x43, 0x45, 0x76, 0x65, 0x6e, 0x74,
0x2e, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x02, 0x6f, 0x70, 0x12, 0x14,
0x0a, 0x05, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x74,
0x61, 0x62, 0x6c, 0x65, 0x12, 0x1c, 0x0a, 0x0a, 0x6f, 0x6c, 0x64, 0x5f, 0x72, 0x6f, 0x77, 0x5f,
0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x6f, 0x6c, 0x64, 0x52, 0x6f, 0x77,
0x49, 0x64, 0x12, 0x1c, 0x0a, 0x0a, 0x6e, 0x65, 0x77, 0x5f, 0x72, 0x6f, 0x77, 0x5f, 0x69, 0x64,
0x18, 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x6e, 0x65, 0x77, 0x52, 0x6f, 0x77, 0x49, 0x64,
0x12, 0x28, 0x0a, 0x07, 0x6f, 0x6c, 0x64, 0x5f, 0x72, 0x6f, 0x77, 0x18, 0x06, 0x20, 0x01, 0x28,
0x0b, 0x32, 0x0f, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x43, 0x44, 0x43, 0x52,
0x6f, 0x77, 0x52, 0x06, 0x6f, 0x6c, 0x64, 0x52, 0x6f, 0x77, 0x12, 0x28, 0x0a, 0x07, 0x6e, 0x65,
0x77, 0x5f, 0x72, 0x6f, 0x77, 0x18, 0x07, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0f, 0x2e, 0x63, 0x6f,
0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2e, 0x43, 0x44, 0x43, 0x52, 0x6f, 0x77, 0x52, 0x06, 0x6e, 0x65,
0x77, 0x52, 0x6f, 0x77, 0x22, 0x3c, 0x0a, 0x09, 0x4f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f,
0x6e, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0a,
0x0a, 0x06, 0x49, 0x4e, 0x53, 0x45, 0x52, 0x54, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x55, 0x50,
0x44, 0x41, 0x54, 0x45, 0x10, 0x02, 0x12, 0x0a, 0x0a, 0x06, 0x44, 0x45, 0x4c, 0x45, 0x54, 0x45,
0x10, 0x03, 0x42, 0x2b, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d,
0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x72, 0x71, 0x6c, 0x69, 0x74, 0x65, 0x2f, 0x76,
0x38, 0x2f, 0x63, 0x6f, 0x6d, 0x6d, 0x61, 0x6e, 0x64, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
@@ -1574,50 +1921,58 @@ func file_command_proto_rawDescGZIP() []byte {
return file_command_proto_rawDescData
}
var file_command_proto_enumTypes = make([]protoimpl.EnumInfo, 3)
var file_command_proto_msgTypes = make([]protoimpl.MessageInfo, 18)
var file_command_proto_enumTypes = make([]protoimpl.EnumInfo, 4)
var file_command_proto_msgTypes = make([]protoimpl.MessageInfo, 21)
var file_command_proto_goTypes = []any{
(QueryRequest_Level)(0), // 0: command.QueryRequest.Level
(BackupRequest_Format)(0), // 1: command.BackupRequest.Format
(Command_Type)(0), // 2: command.Command.Type
(*Parameter)(nil), // 3: command.Parameter
(*Statement)(nil), // 4: command.Statement
(*Request)(nil), // 5: command.Request
(*QueryRequest)(nil), // 6: command.QueryRequest
(*Values)(nil), // 7: command.Values
(*QueryRows)(nil), // 8: command.QueryRows
(*ExecuteRequest)(nil), // 9: command.ExecuteRequest
(*ExecuteResult)(nil), // 10: command.ExecuteResult
(*ExecuteQueryRequest)(nil), // 11: command.ExecuteQueryRequest
(*ExecuteQueryResponse)(nil), // 12: command.ExecuteQueryResponse
(*BackupRequest)(nil), // 13: command.BackupRequest
(*LoadRequest)(nil), // 14: command.LoadRequest
(*LoadChunkRequest)(nil), // 15: command.LoadChunkRequest
(*JoinRequest)(nil), // 16: command.JoinRequest
(*NotifyRequest)(nil), // 17: command.NotifyRequest
(*RemoveNodeRequest)(nil), // 18: command.RemoveNodeRequest
(*Noop)(nil), // 19: command.Noop
(*Command)(nil), // 20: command.Command
(CDCEvent_Operation)(0), // 3: command.CDCEvent.Operation
(*Parameter)(nil), // 4: command.Parameter
(*Statement)(nil), // 5: command.Statement
(*Request)(nil), // 6: command.Request
(*QueryRequest)(nil), // 7: command.QueryRequest
(*Values)(nil), // 8: command.Values
(*QueryRows)(nil), // 9: command.QueryRows
(*ExecuteRequest)(nil), // 10: command.ExecuteRequest
(*ExecuteResult)(nil), // 11: command.ExecuteResult
(*ExecuteQueryRequest)(nil), // 12: command.ExecuteQueryRequest
(*ExecuteQueryResponse)(nil), // 13: command.ExecuteQueryResponse
(*BackupRequest)(nil), // 14: command.BackupRequest
(*LoadRequest)(nil), // 15: command.LoadRequest
(*LoadChunkRequest)(nil), // 16: command.LoadChunkRequest
(*JoinRequest)(nil), // 17: command.JoinRequest
(*NotifyRequest)(nil), // 18: command.NotifyRequest
(*RemoveNodeRequest)(nil), // 19: command.RemoveNodeRequest
(*Noop)(nil), // 20: command.Noop
(*Command)(nil), // 21: command.Command
(*CDCValue)(nil), // 22: command.CDCValue
(*CDCRow)(nil), // 23: command.CDCRow
(*CDCEvent)(nil), // 24: command.CDCEvent
}
var file_command_proto_depIdxs = []int32{
3, // 0: command.Statement.parameters:type_name -> command.Parameter
4, // 1: command.Request.statements:type_name -> command.Statement
5, // 2: command.QueryRequest.request:type_name -> command.Request
4, // 0: command.Statement.parameters:type_name -> command.Parameter
5, // 1: command.Request.statements:type_name -> command.Statement
6, // 2: command.QueryRequest.request:type_name -> command.Request
0, // 3: command.QueryRequest.level:type_name -> command.QueryRequest.Level
3, // 4: command.Values.parameters:type_name -> command.Parameter
7, // 5: command.QueryRows.values:type_name -> command.Values
5, // 6: command.ExecuteRequest.request:type_name -> command.Request
5, // 7: command.ExecuteQueryRequest.request:type_name -> command.Request
4, // 4: command.Values.parameters:type_name -> command.Parameter
8, // 5: command.QueryRows.values:type_name -> command.Values
6, // 6: command.ExecuteRequest.request:type_name -> command.Request
6, // 7: command.ExecuteQueryRequest.request:type_name -> command.Request
0, // 8: command.ExecuteQueryRequest.level:type_name -> command.QueryRequest.Level
8, // 9: command.ExecuteQueryResponse.q:type_name -> command.QueryRows
10, // 10: command.ExecuteQueryResponse.e:type_name -> command.ExecuteResult
9, // 9: command.ExecuteQueryResponse.q:type_name -> command.QueryRows
11, // 10: command.ExecuteQueryResponse.e:type_name -> command.ExecuteResult
1, // 11: command.BackupRequest.format:type_name -> command.BackupRequest.Format
2, // 12: command.Command.type:type_name -> command.Command.Type
13, // [13:13] is the sub-list for method output_type
13, // [13:13] is the sub-list for method input_type
13, // [13:13] is the sub-list for extension type_name
13, // [13:13] is the sub-list for extension extendee
0, // [0:13] is the sub-list for field type_name
22, // 13: command.CDCRow.values:type_name -> command.CDCValue
3, // 14: command.CDCEvent.op:type_name -> command.CDCEvent.Operation
23, // 15: command.CDCEvent.old_row:type_name -> command.CDCRow
23, // 16: command.CDCEvent.new_row:type_name -> command.CDCRow
17, // [17:17] is the sub-list for method output_type
17, // [17:17] is the sub-list for method input_type
17, // [17:17] is the sub-list for extension type_name
17, // [17:17] is the sub-list for extension extendee
0, // [0:17] is the sub-list for field type_name
}
func init() { file_command_proto_init() }
@@ -1637,13 +1992,20 @@ func file_command_proto_init() {
(*ExecuteQueryResponse_E)(nil),
(*ExecuteQueryResponse_Error)(nil),
}
file_command_proto_msgTypes[18].OneofWrappers = []any{
(*CDCValue_I)(nil),
(*CDCValue_D)(nil),
(*CDCValue_B)(nil),
(*CDCValue_Y)(nil),
(*CDCValue_S)(nil),
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_command_proto_rawDesc,
NumEnums: 3,
NumMessages: 18,
NumEnums: 4,
NumMessages: 21,
NumExtensions: 0,
NumServices: 0,
},

View File

@@ -141,3 +141,33 @@ message Command {
bytes sub_command = 2;
bool compressed = 3;
}
message CDCValue {
oneof value {
sint64 i = 1;
double d = 2;
bool b = 3;
bytes y = 4;
string s = 5;
}
}
message CDCRow {
repeated CDCValue values = 1;
}
message CDCEvent {
enum Operation {
UNKNOWN = 0;
INSERT = 1;
UPDATE = 2;
DELETE = 3;
}
string error = 1;
Operation op = 2;
string table = 3;
int64 old_row_id = 4;
int64 new_row_id = 5;
CDCRow old_row = 6;
CDCRow new_row = 7;
}

140
db/db.go
View File

@@ -239,6 +239,78 @@ func OpenWithDriver(drv *Driver, dbPath string, fkEnabled, wal bool) (retDB *DB,
}, nil
}
// PreUpdateHookCallback is a callback function that is called before a row is modified
// in the database.
type PreUpdateHookCallback func(ev *command.CDCEvent)
// RegisterPreUpdateHook registers a callback that is called before a row is modified
// in the database. If rowIDOnly is true, only the row ID details are passed to the
// callback. If a callback is already registered, it is replaced. If hook is nil, the
// callback is removed.
func (db *DB) RegisterPreUpdateHook(hook PreUpdateHookCallback) error {
var cb func(d sqlite3.SQLitePreUpdateData)
if hook != nil {
cb = func(d sqlite3.SQLitePreUpdateData) {
pb := &command.CDCEvent{
Table: d.TableName,
OldRowId: d.OldRowID,
NewRowId: d.NewRowID,
}
switch d.Op {
case sqlite3.SQLITE_INSERT:
pb.Op = command.CDCEvent_INSERT
case sqlite3.SQLITE_UPDATE:
pb.Op = command.CDCEvent_UPDATE
case sqlite3.SQLITE_DELETE:
pb.Op = command.CDCEvent_DELETE
default:
pb.Error = fmt.Sprintf("unknown preupdate hook operation %d", d.Op)
}
c := d.Count()
oldRow := make([]any, c)
if d.Op != sqlite3.SQLITE_INSERT {
err := d.Old(oldRow...)
if err != nil {
pb.Error = fmt.Sprintf("failed to get old row data: %s", err.Error())
}
pb.OldRow, err = normalizeCDCValues(oldRow)
if err != nil {
pb.Error = fmt.Sprintf("failed to normalize old row data: %s", err.Error())
}
}
newRow := make([]any, c)
if d.Op != sqlite3.SQLITE_DELETE {
err := d.New(newRow...)
if err != nil {
pb.Error = fmt.Sprintf("failed to get new row data: %s", err.Error())
}
pb.NewRow, err = normalizeCDCValues(newRow)
if err != nil {
pb.Error = fmt.Sprintf("failed to normalize new row data: %s", err.Error())
}
}
hook(pb)
}
}
f := func(driverConn interface{}) error {
conn := driverConn.(*sqlite3.SQLiteConn)
conn.RegisterPreUpdateHook(cb)
return nil
}
conn, err := db.rwDB.Conn(context.Background())
if err != nil {
return err
}
defer conn.Close()
if err := conn.Raw(f); err != nil {
return err
}
return nil
}
// LastModified returns the last modified time of the database file, or the WAL file,
// whichever is most recent.
func (db *DB) LastModified() (time.Time, error) {
@@ -1544,6 +1616,74 @@ func normalizeRowParameters(row []interface{}, types []string) ([]*command.Param
return values, nil
}
func normalizeCDCValues(row []any) (*command.CDCRow, error) {
cdcRow := &command.CDCRow{
Values: make([]*command.CDCValue, len(row)),
}
for i, v := range row {
switch val := v.(type) {
case int:
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_I{
I: int64(val)},
}
case int64:
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_I{
I: val,
},
}
case float64:
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_D{
D: val,
},
}
case bool:
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_B{
B: val,
},
}
case string:
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_S{
S: val,
},
}
case []byte:
if true {
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_S{
S: string(val),
},
}
} else {
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_Y{
Y: val,
},
}
}
case time.Time:
rfc3339, err := val.MarshalText()
if err != nil {
return nil, err
}
cdcRow.Values[i] = &command.CDCValue{
Value: &command.CDCValue_S{
S: string(rfc3339),
},
}
case nil:
continue
default:
return nil, fmt.Errorf("unhandled column type: %T %v", val, val)
}
}
return cdcRow, nil
}
// isTextType returns whether the given type has a SQLite text affinity.
// http://www.sqlite.org/datatype3.html
func isTextType(t string) bool {

258
db/db_preupdate_test.go Normal file
View File

@@ -0,0 +1,258 @@
package db
import (
"os"
"sync"
"sync/atomic"
"testing"
command "github.com/rqlite/rqlite/v8/command/proto"
)
// Test_Preupdate_Basic tests the basic functionality of the preupdate hook, ensuring
// it is triggered for inserts, updates, and deletes, and not triggered for selects,
// executes that don't change anything, and when unregistered.
func Test_Preupdate_Basic(t *testing.T) {
path := mustTempPath()
defer os.Remove(path)
db, err := Open(path, false, false)
if err != nil {
t.Fatalf("error opening database")
}
defer db.Close()
mustExecute(db, "CREATE TABLE foo (id INTEGER PRIMARY KEY, name TEXT)")
count := &atomic.Int32{}
hook := func(ev *command.CDCEvent) {
count.Add(1)
}
if err := db.RegisterPreUpdateHook(hook); err != nil {
t.Fatalf("error registering preupdate hook")
}
// A select should not trigger the hook and a basic insert should trigger the hook.
mustQuery(db, "SELECT * FROM foo")
mustExecute(db, "INSERT INTO foo(name) VALUES('fiona')")
if count.Load() != 1 {
t.Fatalf("expected count 1, got %d", count.Load())
}
// An update should trigger the hook, and an update that doesn't change anything
// should not trigger the hook.
mustExecute(db, "UPDATE foo SET name='fiona2' WHERE id=5")
mustExecute(db, "UPDATE foo SET name='fiona2' WHERE id=1")
if count.Load() != 2 {
t.Fatalf("expected count 2, got %d", count.Load())
}
// A delete should trigger the hook.
mustExecute(db, "DELETE FROM foo WHERE id=1")
if count.Load() != 3 {
t.Fatalf("expected count 3, got %d", count.Load())
}
// Insert 5 rows, make sure the hook is triggered 5 times.
for i := 0; i < 5; i++ {
mustExecute(db, "INSERT INTO foo(name) VALUES('fiona')")
}
if count.Load() != 8 {
t.Fatalf("expected count 8, got %d", count.Load())
}
// Delete all rows, make sure the hook is triggered 5 times.
r := mustQuery(db, "SELECT COUNT(*) FROM foo")
if exp, got := int64(5), r[0].Values[0].Parameters[0].GetI(); exp != got {
t.Fatalf("expected count %d, got %d", exp, got)
}
mustExecute(db, "DELETE FROM foo")
if count.Load() != 13 {
t.Fatalf("expected count 13, got %d", count.Load())
}
// Unregister the hook, insert a row, and make sure the hook is not triggered.
if err := db.RegisterPreUpdateHook(nil); err != nil {
t.Fatalf("error unregistering preupdate hook")
}
mustExecute(db, "INSERT INTO foo(name) VALUES('fiona')")
if count.Load() != 13 {
t.Fatalf("expected count 8, got %d", count.Load())
}
}
// Test_Preupdate_Constraint tests that the preupdate hook is not triggered for
// inserts that violate a constraint.
func Test_Preupdate_Constraint(t *testing.T) {
path := mustTempPath()
defer os.Remove(path)
db, err := Open(path, false, false)
if err != nil {
t.Fatalf("error opening database")
}
defer db.Close()
mustExecute(db, "CREATE TABLE foo (id INTEGER PRIMARY KEY, name TEXT UNIQUE)")
count := &atomic.Int32{}
hook := func(ev *command.CDCEvent) {
count.Add(1)
}
if err := db.RegisterPreUpdateHook(hook); err != nil {
t.Fatalf("error registering preupdate hook")
}
// Insert a row, with an explicit ID.
mustExecute(db, "INSERT INTO foo(id, name) VALUES(5, 'fiona')")
if count.Load() != 1 {
t.Fatalf("expected count 1, got %d", count.Load())
}
// Insert a row with the same ID, should not trigger the hook.
r, err := db.ExecuteStringStmt("INSERT INTO foo(id, name) VALUES(5, 'fiona2')")
if err == nil && r[0].GetError() == "" {
t.Fatalf("expected error, got nil")
}
if count.Load() != 1 {
t.Fatalf("expected count 1, got %d", count.Load())
}
// Ensure the hook is not triggered for a INSERT that violates a unique constraint.
r, err = db.ExecuteStringStmt("INSERT INTO foo(id, name) VALUES(6, 'fiona')")
if err == nil && r[0].GetError() == "" {
t.Fatalf("expected error, got nil")
}
if count.Load() != 1 {
t.Fatalf("expected count 1, got %d", count.Load())
}
}
func Test_Preupdate_Data(t *testing.T) {
path := mustTempPath()
defer os.Remove(path)
db, err := Open(path, false, false)
if err != nil {
t.Fatalf("error opening database")
}
defer db.Close()
mustExecute(db, "CREATE TABLE foo (id INTEGER PRIMARY KEY, name TEXT UNIQUE, age float)")
// Insert a row, with an explicit ID.
var wg sync.WaitGroup
hook := func(ev *command.CDCEvent) {
defer wg.Done()
if ev.Table != "foo" {
t.Fatalf("expected table foo, got %s", ev.Table)
}
if ev.Op != command.CDCEvent_INSERT {
t.Fatalf("expected operation insert, got %s", ev.Op)
}
if ev.OldRow != nil {
t.Fatalf("expected no old row")
}
if ev.NewRow == nil {
t.Fatalf("expected new row")
}
if len(ev.NewRow.Values) != 3 {
t.Fatalf("expected 3 values, got %d", len(ev.NewRow.Values))
}
if exp, got := int64(5), ev.NewRow.Values[0].GetI(); exp != got {
t.Fatalf("expected id %d, got %d", exp, got)
}
if exp, got := "fiona", ev.NewRow.Values[1].GetS(); exp != got {
t.Fatalf("expected name %s, got %s", exp, got)
}
if exp, got := 2.4, ev.NewRow.Values[2].GetD(); exp != got {
t.Fatalf("expected age %f, got %f", exp, got)
}
}
if err := db.RegisterPreUpdateHook(hook); err != nil {
t.Fatalf("error registering preupdate hook")
}
wg.Add(1)
mustExecute(db, "INSERT INTO foo(id, name, age) VALUES(5, 'fiona', 2.4)")
wg.Wait()
// Update a row.
hook = func(ev *command.CDCEvent) {
defer wg.Done()
if ev.Table != "foo" {
t.Fatalf("expected table foo, got %s", ev.Table)
}
if ev.Op != command.CDCEvent_UPDATE {
t.Fatalf("expected operation update, got %s", ev.Op)
}
if ev.OldRow == nil {
t.Fatalf("expected old row")
}
if len(ev.OldRow.Values) != 3 {
t.Fatalf("expected 3 values, got %d", len(ev.OldRow.Values))
}
if exp, got := int64(5), ev.OldRow.Values[0].GetI(); exp != got {
t.Fatalf("expected id %d, got %d", exp, got)
}
if exp, got := "fiona", ev.OldRow.Values[1].GetS(); exp != got {
t.Fatalf("expected name %s, got %s", exp, got)
}
if exp, got := 2.4, ev.OldRow.Values[2].GetD(); exp != got {
t.Fatalf("expected age %f, got %f", exp, got)
}
if ev.NewRow == nil {
t.Fatalf("expected new row")
}
if len(ev.NewRow.Values) != 3 {
t.Fatalf("expected 3 values, got %d", len(ev.NewRow.Values))
}
if exp, got := int64(5), ev.NewRow.Values[0].GetI(); exp != got {
t.Fatalf("expected id %d, got %d", exp, got)
}
if exp, got := "fiona2", ev.NewRow.Values[1].GetS(); exp != got {
t.Fatalf("expected name %s, got %s", exp, got)
}
if exp, got := 2.4, ev.NewRow.Values[2].GetD(); exp != got {
t.Fatalf("expected age %f, got %f", exp, got)
}
}
if err := db.RegisterPreUpdateHook(hook); err != nil {
t.Fatalf("error registering preupdate hook")
}
wg.Add(1)
mustExecute(db, "UPDATE foo SET name='fiona2' WHERE id=5")
wg.Wait()
// Delete a row.
hook = func(ev *command.CDCEvent) {
defer wg.Done()
if ev.Table != "foo" {
t.Fatalf("expected table foo, got %s", ev.Table)
}
if ev.Op != command.CDCEvent_DELETE {
t.Fatalf("expected operation delete, got %s", ev.Op)
}
if ev.OldRow == nil {
t.Fatalf("expected old row")
}
if len(ev.OldRow.Values) != 3 {
t.Fatalf("expected 3 values, got %d", len(ev.OldRow.Values))
}
if exp, got := int64(5), ev.OldRow.Values[0].GetI(); exp != got {
t.Fatalf("expected id %d, got %d", exp, got)
}
if exp, got := "fiona2", ev.OldRow.Values[1].GetS(); exp != got {
t.Fatalf("expected name %s, got %s", exp, got)
}
if exp, got := 2.4, ev.OldRow.Values[2].GetD(); exp != got {
t.Fatalf("expected age %f, got %f", exp, got)
}
if ev.NewRow != nil {
t.Fatalf("expected no new row")
}
}
if err := db.RegisterPreUpdateHook(hook); err != nil {
t.Fatalf("error registering preupdate hook")
}
wg.Add(1)
mustExecute(db, "DELETE FROM foo WHERE id=5")
wg.Wait()
}