substreams-database-change 4.0.0

Substreams database change containg helpers
Documentation
// @generated
// This file is @generated by prost-build.
/// DatabaseChanges represents a collection of table changes that should be applied to a database.
/// This is the top-level message that Substreams modules output when using the database sink.
/// All changes within a DatabaseChanges message are typically applied within a single transaction.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct DatabaseChanges {
    /// table_changes is an ordered list of changes to apply to database tables.
    /// The changes are applied in the order they appear in this list.
    #[prost(message, repeated, tag = "1")]
    pub table_changes: ::prost::alloc::vec::Vec<TableChange>,
}
/// TableChange represents a single change operation (CREATE, UPDATE, DELETE, or UPSERT) on a database table row.
/// Each TableChange must specify the table name, primary key, operation type, and the fields to modify.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct TableChange {
    /// table is the name of the database table to modify.
    /// This should match the actual table name in your database schema.
    #[prost(string, tag = "1")]
    pub table: ::prost::alloc::string::String,
    /// ordinal provides ordering information for this change within the block.
    /// Changes with lower ordinal values are applied before changes with higher values.
    /// This ensures deterministic ordering when multiple changes affect the same or related data.
    #[prost(uint64, tag = "3")]
    pub ordinal: u64,
    /// operation specifies what type of change to apply to the table row.
    #[prost(enumeration = "table_change::Operation", tag = "4")]
    pub operation: i32,
    /// fields contains the column values to set or update.
    /// For CREATE and UPSERT operations, these are the values for the new/updated row.
    /// For UPDATE operations, these are the columns to modify.
    /// For DELETE operations, fields may be empty or contain old values for logging/auditing.
    #[prost(message, repeated, tag = "5")]
    pub fields: ::prost::alloc::vec::Vec<Field>,
    /// primary_key identifies the row to operate on. Use either a single primary key string
    /// or a composite primary key for tables with multi-column primary keys.
    #[prost(oneof = "table_change::PrimaryKey", tags = "2, 6")]
    pub primary_key: ::core::option::Option<table_change::PrimaryKey>,
}
/// Nested message and enum types in `TableChange`.
pub mod table_change {
    /// Operation defines the type of database operation to perform on the row.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
    #[repr(i32)]
    pub enum Operation {
        /// OPERATION_UNSPECIFIED should not be used in practice.
        /// This exists as the protobuf default to ensure consumers explicitly set the operation type.
        Unspecified = 0,
        /// OPERATION_CREATE inserts a new row into the table.
        /// Will fail if a row with the same primary key already exists.
        Create = 1,
        /// OPERATION_UPDATE modifies an existing row in the table.
        /// Will fail if no row with the specified primary key exists.
        Update = 2,
        /// OPERATION_DELETE removes a row from the table.
        /// Will fail if no row with the specified primary key exists.
        Delete = 3,
        /// OPERATION_UPSERT inserts a new row or updates an existing row.
        /// If a row with the primary key exists, it updates; otherwise, it creates.
        ///
        /// The upsert might not be supported by all drivers the sink supports,
        /// refer to <https://github.com/streamingfast/substreams-sink-sql> for
        /// which drivers support it.
        ///
        /// At time of writing, the Postgres driver supports was the only one supporting
        /// it.
        Upsert = 4,
    }
    impl Operation {
        /// String value of the enum field names used in the ProtoBuf definition.
        ///
        /// The values are not transformed in any way and thus are considered stable
        /// (if the ProtoBuf definition does not change) and safe for programmatic use.
        pub fn as_str_name(&self) -> &'static str {
            match self {
                Operation::Unspecified => "OPERATION_UNSPECIFIED",
                Operation::Create => "OPERATION_CREATE",
                Operation::Update => "OPERATION_UPDATE",
                Operation::Delete => "OPERATION_DELETE",
                Operation::Upsert => "OPERATION_UPSERT",
            }
        }
        /// Creates an enum from field names used in the ProtoBuf definition.
        pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
            match value {
                "OPERATION_UNSPECIFIED" => Some(Self::Unspecified),
                "OPERATION_CREATE" => Some(Self::Create),
                "OPERATION_UPDATE" => Some(Self::Update),
                "OPERATION_DELETE" => Some(Self::Delete),
                "OPERATION_UPSERT" => Some(Self::Upsert),
                _ => None,
            }
        }
    }
    /// primary_key identifies the row to operate on. Use either a single primary key string
    /// or a composite primary key for tables with multi-column primary keys.
    #[allow(clippy::derive_partial_eq_without_eq)]
    #[derive(Clone, PartialEq, ::prost::Oneof)]
    pub enum PrimaryKey {
        /// pk is used for tables with a single-column primary key.
        /// The value should be the primary key value as a string.
        #[prost(string, tag = "2")]
        Pk(::prost::alloc::string::String),
        /// composite_pk is used for tables with multi-column primary keys.
        /// Provides a map of column names to their values.
        #[prost(message, tag = "6")]
        CompositePk(super::CompositePrimaryKey),
    }
}
/// CompositePrimaryKey represents a primary key composed of multiple columns.
/// Use this when your table has a multi-column primary key instead of a single primary key column.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct CompositePrimaryKey {
    /// keys is a map from column name to column value for each part of the composite primary key.
    /// All values are represented as strings and will be converted to the appropriate type by the sink.
    /// Example: {"user_id": "123", "post_id": "456"} for a table with composite primary key (user_id, post_id)
    #[prost(map = "string, string", tag = "1")]
    pub keys:
        ::std::collections::HashMap<::prost::alloc::string::String, ::prost::alloc::string::String>,
}
/// Field represents a single column value in a table row.
/// Contains the column name, value, and an update operation mode for UPSERT operations.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Field {
    /// name is the column name in the database table.
    /// Must match the actual column name in your database schema.
    #[prost(string, tag = "1")]
    pub name: ::prost::alloc::string::String,
    /// value is the value to set for this column.
    /// All values are represented as strings and will be converted to the appropriate database type.
    /// For numeric types, use string representation (e.g., "123", "45.67").
    /// For NULL values, leave this field empty or unset.
    #[prost(string, tag = "2")]
    pub value: ::prost::alloc::string::String,
    /// update_op specifies how to merge this field's value during UPSERT operations.
    /// Defaults to UPDATE_OP_SET if not specified.
    /// Ignored for CREATE, UPDATE, and DELETE operations.
    #[prost(enumeration = "field::UpdateOp", tag = "4")]
    pub update_op: i32,
}
/// Nested message and enum types in `Field`.
pub mod field {
    /// UpdateOp defines how the field value should be combined with existing data during UPSERT operations.
    /// Only applies when TableChange.operation is OPERATION_UPSERT.
    /// Allows for advanced merge behaviors beyond simple replacement.
    #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]
    #[repr(i32)]
    pub enum UpdateOp {
        /// UPDATE_OP_UNSPECIFIED should not be used in practice.
        /// This exists as the protobuf default to ensure consumers explicitly set the update operation type.
        Unspecified = 0,
        /// UPDATE_OP_SET replaces the column value with the provided value (default behavior).
        /// SQL equivalent: column = value
        Set = 1,
        /// UPDATE_OP_ADD adds the provided value to the existing column value.
        /// Useful for counters and numeric accumulation.
        /// SQL equivalent: column = COALESCE(column, 0) + value
        Add = 2,
        /// UPDATE_OP_MAX sets the column to the maximum of existing value and provided value.
        /// Useful for tracking high-water marks or maximum observed values.
        /// SQL equivalent: column = GREATEST(COALESCE(column, value), value)
        Max = 3,
        /// UPDATE_OP_MIN sets the column to the minimum of existing value and provided value.
        /// Useful for tracking low-water marks or minimum observed values.
        /// SQL equivalent: column = LEAST(COALESCE(column, value), value)
        Min = 4,
        /// UPDATE_OP_SET_IF_NULL sets the column to the provided value only if it's currently NULL.
        /// Preserves the first non-NULL value and prevents subsequent overwrites.
        /// SQL equivalent: column = COALESCE(column, value)
        SetIfNull = 5,
    }
    impl UpdateOp {
        /// String value of the enum field names used in the ProtoBuf definition.
        ///
        /// The values are not transformed in any way and thus are considered stable
        /// (if the ProtoBuf definition does not change) and safe for programmatic use.
        pub fn as_str_name(&self) -> &'static str {
            match self {
                UpdateOp::Unspecified => "UPDATE_OP_UNSPECIFIED",
                UpdateOp::Set => "UPDATE_OP_SET",
                UpdateOp::Add => "UPDATE_OP_ADD",
                UpdateOp::Max => "UPDATE_OP_MAX",
                UpdateOp::Min => "UPDATE_OP_MIN",
                UpdateOp::SetIfNull => "UPDATE_OP_SET_IF_NULL",
            }
        }
        /// Creates an enum from field names used in the ProtoBuf definition.
        pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {
            match value {
                "UPDATE_OP_UNSPECIFIED" => Some(Self::Unspecified),
                "UPDATE_OP_SET" => Some(Self::Set),
                "UPDATE_OP_ADD" => Some(Self::Add),
                "UPDATE_OP_MAX" => Some(Self::Max),
                "UPDATE_OP_MIN" => Some(Self::Min),
                "UPDATE_OP_SET_IF_NULL" => Some(Self::SetIfNull),
                _ => None,
            }
        }
    }
}
// @@protoc_insertion_point(module)