use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
pub use pg_walstream::{ChangeEvent, EventType, Lsn, ReplicaIdentity, RowData};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum DestinationType {
MySQL,
SqlServer,
SQLite,
}
impl std::fmt::Display for DestinationType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DestinationType::MySQL => write!(f, "mysql"),
DestinationType::SqlServer => write!(f, "sqlserver"),
DestinationType::SQLite => write!(f, "sqlite"),
}
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Transaction {
pub transaction_id: u32,
pub commit_timestamp: DateTime<Utc>,
pub commit_lsn: Option<Lsn>,
pub events: Vec<ChangeEvent>,
#[serde(default = "default_is_final_batch")]
pub is_final_batch: bool,
}
fn default_is_final_batch() -> bool {
true
}
impl Transaction {
pub fn new(transaction_id: u32, commit_timestamp: DateTime<Utc>) -> Self {
Self {
transaction_id,
commit_timestamp,
commit_lsn: None,
events: Vec::new(),
is_final_batch: true, }
}
pub fn add_event(&mut self, event: ChangeEvent) {
self.events.push(event);
}
pub fn set_commit_lsn(&mut self, lsn: Lsn) {
self.commit_lsn = Some(lsn);
}
pub fn event_count(&self) -> usize {
self.events.len()
}
pub fn is_empty(&self) -> bool {
self.events.is_empty()
}
pub fn set_final_batch(&mut self, is_final: bool) {
self.is_final_batch = is_final;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransactionContext {
None,
Normal(u32), Streaming(u32), }
#[derive(Debug, Clone)]
pub struct InFlightNormalTransaction {
pub transaction_id: u32,
pub event_count: usize,
}