Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,53 @@ All notable changes to dbmazz will be documented here.

## [Unreleased]

## [2.4.0] - 2026-05-16

### Changed

- **Sinks now dispatch on `DataType` (source-agnostic), not on
`pg_type_id`.** PostgreSQL `pg_type_id` is now an optional refinement
carried only by Postgres sources (`Option<u32>` on `SourceColumn`);
non-PG sources (MySQL today) populate `None`. The previous design used
a magic-zero `pg_type_id == 0` sentinel to detect non-PG sources,
which coupled every sink to PG OID semantics and silently broke
type-aware code paths for MySQL.
- Internal: `connectors::sinks::postgres::types::mysql_data_type_to_pg`
renamed to `data_type_to_pg` (it was never MySQL-specific; the name
was a relic of when it was first added).

### Fixed

- **MySQL → Snowflake** MERGE projection now produces correct casts for
every column type. Previously, the Snowflake sink dispatched on
`pg_type_id` and treated `0` (MySQL marker) as an unknown type,
defaulting every column to `VARCHAR` and silently breaking MERGE
fidelity. Now the sink dispatches primarily on `DataType` and falls
back to the OID only when the `DataType` is too coarse (PG arrays,
money) to disambiguate.
- **PostgreSQL sink** `Value::Timestamp` now serializes as an ISO-8601
string (`YYYY-MM-DD HH:MM:SS.ffffff`) instead of a raw int64. Parity
with StarRocks/Snowflake sinks. Fixes a pre-existing bug that surfaced
when MySQL TIMESTAMP began landing as `Value::Timestamp(i64)` — the
raw int landed in the raw table's JSONB cell and the downstream MERGE
could not cast it.
- `pg_type_to_data_type` (PG→`DataType` boundary) now maps `1082`
(`date`), `1083` (`time`), and `1266` (`timetz`) to `DataType::Date`
and `DataType::Time` respectively, instead of collapsing them to
`DataType::String`. Sinks that dispatch on `DataType` (now the
primary axis) can produce accurate DDL without consulting the OID.

### Migration

- **`_dbmazz._schema_tracking` schema** auto-migrates from v1 to v2 on
first daemon start with v2.4.0+: adds nullable `data_type JSONB`
column, drops the `NOT NULL` on `pg_type_id`, and backfills
`data_type` for any pre-existing rows. The migration is idempotent;
re-running on v2 is a no-op. Old binaries remain forward-compatible
with the v2 schema (they will not write the new `data_type` column,
but reads tolerate NULL `data_type` with a fallback to
`pg_type_to_data_type(pg_type_id)`).

## [2.3.0] - 2026-05-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dbmazz"
version = "2.3.0"
version = "2.4.0"
edition = "2021"
license = "Elastic-2.0"
# Minimum Supported Rust Version.
Expand Down
10 changes: 10 additions & 0 deletions docs/contributing-connectors.md
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,16 @@ impl Sink for MyNewSink {

### Step 4: Implement Type Mappings

> **Important — dispatch on `DataType`, never on `pg_type_id`.**
> `SourceColumn::pg_type_id: Option<u32>` is a Postgres-source-only
> refinement metadata field. Sink type mappers MUST take `&DataType` as
> the primary input. `pg_type_id` is consulted only as a refinement
> when the source is Postgres AND `DataType` is too coarse to express
> the precision the target type system needs (e.g., PG arrays collapse
> to `DataType::String` and require the OID to disambiguate). For all
> other cases, ignore `pg_type_id` — it will be `None` for non-PG
> sources (MySQL today, and any future source).

In `types.rs`, map `core::DataType` to sink-specific types:

```rust
Expand Down
14 changes: 7 additions & 7 deletions src/connectors/sinks/postgres/merge_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,13 @@ mod tests {
name: "id".to_string(),
data_type: DataType::Int32,
nullable: false,
pg_type_id: 23, // integer
pg_type_id: Some(23), // integer
},
SourceColumn {
name: "name".to_string(),
data_type: DataType::String,
nullable: true,
pg_type_id: 25, // text
pg_type_id: Some(25), // text
},
SourceColumn {
name: "amount".to_string(),
Expand All @@ -221,7 +221,7 @@ mod tests {
scale: 9,
},
nullable: true,
pg_type_id: 1700, // numeric
pg_type_id: Some(1700), // numeric
},
],
primary_keys: vec!["id".to_string()],
Expand Down Expand Up @@ -300,19 +300,19 @@ mod tests {
name: "order_id".to_string(),
data_type: DataType::Int32,
nullable: false,
pg_type_id: 23,
pg_type_id: Some(23),
},
SourceColumn {
name: "item_id".to_string(),
data_type: DataType::Int32,
nullable: false,
pg_type_id: 23,
pg_type_id: Some(23),
},
SourceColumn {
name: "quantity".to_string(),
data_type: DataType::Int32,
nullable: true,
pg_type_id: 23,
pg_type_id: Some(23),
},
],
primary_keys: vec!["order_id".to_string(), "item_id".to_string()],
Expand Down Expand Up @@ -378,7 +378,7 @@ mod tests {
name: "description".to_string(),
data_type: DataType::String,
nullable: true,
pg_type_id: 25, // text
pg_type_id: Some(25), // text
});

let sql = generate_merge_range(
Expand Down
44 changes: 39 additions & 5 deletions src/connectors/sinks/postgres/raw_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use tracing::info;
use super::schema_tracking::{self, SchemaState};
use super::types;
use crate::connectors::sinks::schema_evolution::compute_schema_evolution_plan;
use crate::core::record::{CdcRecord, ColumnValue, Value};
use crate::core::record::{CdcRecord, ColumnValue, DataType, Value};
use crate::core::traits::SourceTableSchema;

/// Metadata schema name
Expand Down Expand Up @@ -62,9 +62,11 @@ pub async fn write_batch_to_raw(
// is complete (Risk #6). A COPY failure rolls back the DDL too.
for (table, diff) in &pending_diffs {
for added in &diff.added {
let col_type = match added.pg_type_id {
Some(oid) => types::pg_oid_to_target_type(oid),
None => types::mysql_data_type_to_pg(&added.data_type),
// DataType-primary dispatch; OID is a PG-source refinement
// and only adds info when DataType collapses to String.
let col_type = match (&added.data_type, added.pg_type_id) {
(DataType::String | DataType::Text, Some(oid)) => types::pg_oid_to_target_type(oid),
_ => types::data_type_to_pg(&added.data_type),
};
let sql = format!(
r#"ALTER TABLE "{}"."{}" ADD COLUMN IF NOT EXISTS "{}" {}"#,
Expand Down Expand Up @@ -342,7 +344,20 @@ fn value_to_json(value: &Value) -> serde_json::Value {
// Parse the JSON string to embed it as a JSON value (not a string)
serde_json::from_str(s).unwrap_or_else(|_| serde_json::json!(s))
}
Value::Timestamp(ts) => serde_json::json!(*ts),
Value::Timestamp(ts) => {
// Emit ISO-8601 string so the downstream MERGE can cast to
// PG timestamp directly. Mirrors the StarRocks/Snowflake
// sinks. Without this, MySQL TIMESTAMP→PG was unparseable
// because the raw int landed in a JSONB cell and MERGE
// could not cast it to timestamp.
let secs = ts / 1_000_000;
let nanos = ((ts % 1_000_000) * 1000) as u32;
if let Some(dt) = chrono::DateTime::from_timestamp(secs, nanos) {
serde_json::json!(dt.format("%Y-%m-%d %H:%M:%S%.6f").to_string())
} else {
serde_json::Value::Null
}
}
Value::Decimal(s) => serde_json::json!(s),
Value::Uuid(s) => serde_json::json!(s),
Value::Unchanged => serde_json::Value::Null, // should not reach here
Expand Down Expand Up @@ -380,6 +395,25 @@ mod tests {
assert!(parsed["deleted_at"].is_null());
}

#[test]
fn test_value_to_json_timestamp_iso8601() {
// 2024-01-15 10:50:00 UTC in microseconds since epoch
let ts: i64 = 1_705_315_800_000_000;
let json = value_to_json(&Value::Timestamp(ts));
let s = json.as_str().expect("timestamp must serialize to string");
assert_eq!(s, "2024-01-15 10:50:00.000000");
}

#[test]
fn test_value_to_json_timestamp_with_microseconds() {
// Timestamp with fractional microseconds — must round-trip
// through the format string with %.6f precision.
let ts: i64 = 1_705_315_800_123_456;
let json = value_to_json(&Value::Timestamp(ts));
let s = json.as_str().unwrap();
assert!(s.ends_with(".123456"), "expected microseconds in {s:?}");
}

#[test]
fn test_columns_to_json_skip_unchanged() {
let cols = vec![
Expand Down
Loading