risingwave_connector/parser/debezium/
schema_change.rs1use risingwave_common::catalog::ColumnCatalog;
16use risingwave_pb::ddl_service::table_schema_change::TableChangeType as PbTableChangeType;
17use risingwave_pb::ddl_service::{
18 SchemaChangeEnvelope as PbSchemaChangeEnvelope, TableSchemaChange as PbTableSchemaChange,
19};
20
21#[derive(Debug)]
22pub struct SchemaChangeEnvelope {
23 pub table_changes: Vec<TableSchemaChange>,
24}
25
26#[derive(Debug, Clone, Copy, PartialEq)]
27pub(crate) enum TableChangeType {
28 Unspecified,
29 Alter,
30 Create,
31 Drop,
32}
33
34impl TableChangeType {
35 #[allow(dead_code)]
36 pub fn from_proto(value: PbTableChangeType) -> Self {
37 match value {
38 PbTableChangeType::Alter => TableChangeType::Alter,
39 PbTableChangeType::Create => TableChangeType::Create,
40 PbTableChangeType::Drop => TableChangeType::Drop,
41 PbTableChangeType::Unspecified => TableChangeType::Unspecified,
42 }
43 }
44
45 pub fn to_proto(self) -> PbTableChangeType {
46 match self {
47 TableChangeType::Alter => PbTableChangeType::Alter,
48 TableChangeType::Create => PbTableChangeType::Create,
49 TableChangeType::Drop => PbTableChangeType::Drop,
50 TableChangeType::Unspecified => PbTableChangeType::Unspecified,
51 }
52 }
53}
54
55impl From<&str> for TableChangeType {
56 fn from(value: &str) -> Self {
57 match value {
58 "ALTER" => TableChangeType::Alter,
59 "CREATE" => TableChangeType::Create,
60 "DROP" => TableChangeType::Drop,
61 _ => TableChangeType::Unspecified,
62 }
63 }
64}
65
66#[derive(Debug)]
67pub struct TableSchemaChange {
68 pub(crate) cdc_table_id: String,
69 pub(crate) columns: Vec<ColumnCatalog>,
70 pub(crate) change_type: TableChangeType,
71 pub(crate) upstream_ddl: String,
72}
73
74impl SchemaChangeEnvelope {
75 pub fn is_empty(&self) -> bool {
76 self.table_changes.is_empty()
77 }
78
79 pub fn to_protobuf(&self) -> PbSchemaChangeEnvelope {
80 let table_changes = self
81 .table_changes
82 .iter()
83 .map(|table_change| {
84 let columns = table_change
85 .columns
86 .iter()
87 .map(|column| column.to_protobuf())
88 .collect();
89 PbTableSchemaChange {
90 change_type: table_change.change_type.to_proto() as _,
91 cdc_table_id: table_change.cdc_table_id.clone(),
92 columns,
93 upstream_ddl: table_change.upstream_ddl.clone(),
94 }
95 })
96 .collect();
97
98 PbSchemaChangeEnvelope { table_changes }
99 }
100
101 pub fn table_ids(&self) -> Vec<String> {
102 self.table_changes
103 .iter()
104 .map(|table_change| table_change.cdc_table_id.clone())
105 .collect()
106 }
107}