@@ -20,12 +20,12 @@ use crate::{bail, etl_error};
2020/// event trigger. Messages with this prefix contain JSON-encoded schema information.
2121pub const DDL_MESSAGE_PREFIX : & str = "supabase_etl_ddl" ;
2222
23- /// Represents a DDL schema change message emitted by Postgres event trigger.
23+ /// Represents a schema change message emitted by Postgres event trigger.
2424///
2525/// This message is emitted when ALTER TABLE commands are executed on tables
2626/// that are part of a publication.
2727#[ derive( Debug , Clone , Deserialize ) ]
28- pub struct DdlSchemaChangeMessage {
28+ pub struct SchemaChangeMessage {
2929 /// The DDL command that triggered this message (e.g., "ALTER TABLE").
3030 pub event : String ,
3131 /// The schema name of the affected table.
@@ -35,13 +35,49 @@ pub struct DdlSchemaChangeMessage {
3535 /// The OID of the affected table.
3636 pub table_id : i64 ,
3737 /// The columns of the table after the schema change.
38- pub columns : Vec < DdlColumnSchema > ,
38+ pub columns : Vec < ColumnSchemaMessage > ,
3939}
4040
41- /// Represents a column schema in a DDL schema change message.
41+ impl SchemaChangeMessage {
42+
43+ /// Converts a [`SchemaChangeMessage`] to a [`TableSchema`] with a specific snapshot ID.
44+ ///
45+ /// This is used to update the stored table schema when a DDL change is detected.
46+ /// The snapshot_id should be the start_lsn of the DDL message.
47+ pub fn into_table_schema (
48+ self ,
49+ snapshot_id : SnapshotId ,
50+ ) -> TableSchema {
51+ let table_name = TableName :: new ( self . schema_name , self . table_name ) ;
52+ let column_schemas = self
53+ . columns
54+ . into_iter ( )
55+ . map ( |column| {
56+ let typ = convert_type_oid_to_type ( column. type_oid ) ;
57+ ColumnSchema :: new (
58+ column. name ,
59+ typ,
60+ column. type_modifier ,
61+ column. ordinal_position ,
62+ column. primary_key_ordinal_position ,
63+ column. nullable ,
64+ )
65+ } )
66+ . collect ( ) ;
67+
68+ TableSchema :: with_snapshot_id (
69+ TableId :: new ( self . table_id as u32 ) ,
70+ table_name,
71+ column_schemas,
72+ snapshot_id,
73+ )
74+ }
75+ }
76+
77+ /// Represents a column schema in a schema change message.
4278#[ allow( dead_code) ]
4379#[ derive( Debug , Clone , Deserialize ) ]
44- pub struct DdlColumnSchema {
80+ pub struct ColumnSchemaMessage {
4581 /// The name of the column.
4682 pub name : String ,
4783 /// The OID of the column's data type.
@@ -316,45 +352,12 @@ pub fn convert_tuple_to_row<'a>(
316352/// Parses a DDL schema change message from its JSON content.
317353///
318354/// Returns the parsed message if successful, or an error if the JSON is malformed.
319- pub fn parse_ddl_schema_change_message ( content : & str ) -> EtlResult < DdlSchemaChangeMessage > {
355+ pub fn parse_schema_change_message ( content : & str ) -> EtlResult < SchemaChangeMessage > {
320356 serde_json:: from_str ( content) . map_err ( |e| {
321357 etl_error ! (
322358 ErrorKind :: ConversionError ,
323- "Failed to parse DDL schema change message" ,
324- format!( "Invalid JSON in DDL message: {}" , e)
359+ "Failed to parse schema change message" ,
360+ format!( "Invalid JSON in schema change message: {}" , e)
325361 )
326362 } )
327363}
328-
329- /// Converts a [`DdlSchemaChangeMessage`] to a [`TableSchema`] with a specific snapshot ID.
330- ///
331- /// This is used to update the stored table schema when a DDL change is detected.
332- /// The snapshot_id should be the start_lsn of the DDL message.
333- pub fn ddl_message_to_table_schema (
334- message : & DdlSchemaChangeMessage ,
335- snapshot_id : SnapshotId ,
336- ) -> TableSchema {
337- let table_name = TableName :: new ( message. schema_name . clone ( ) , message. table_name . clone ( ) ) ;
338- let column_schemas = message
339- . columns
340- . iter ( )
341- . map ( |col| {
342- let typ = convert_type_oid_to_type ( col. type_oid ) ;
343- ColumnSchema :: new (
344- col. name . clone ( ) ,
345- typ,
346- col. type_modifier ,
347- col. ordinal_position ,
348- col. primary_key_ordinal_position ,
349- col. nullable ,
350- )
351- } )
352- . collect ( ) ;
353-
354- TableSchema :: with_snapshot_id (
355- TableId :: new ( message. table_id as u32 ) ,
356- table_name,
357- column_schemas,
358- snapshot_id,
359- )
360- }
0 commit comments