Files
zeekay 86788566d1 datastore: rebrand the Rust client and speak the Datastore wire protocol
Cut from the upstream v0.13.3 release tag (not HEAD) so this is a pure
rename with no behaviour change. v0.13.3 is what our consumers already
resolve to: insights/rust's Cargo.lock pins clickhouse 0.13.3 and its
manifest already declares `datastore` at 0.13.x.

The load-bearing change is the HTTP auth header rename. The Datastore
server reads only X-Datastore-User / X-Datastore-Key; the upstream names
return `401 Code: 194 Authentication failed`. Every un-renamed HTTP
client is therefore unable to authenticate at all — this is a
correctness fix, not branding.

- crate `clickhouse` -> `datastore`, `clickhouse-derive` -> `datastore-derive`;
  versions preserved exactly (0.13.3 / 0.2.0).
- derive macro emits `datastore::Row` in lockstep, so downstream
  `#[derive(Row)]` keeps resolving.
- User-Agent reports `datastore-rs/{version}`.
- test-harness env vars CLICKHOUSE_* -> DATASTORE_*; compose uses
  ghcr.io/hanzoai/datastore with /etc/datastore-server config paths.
- doc links point at docs.hanzo.ai/datastore.
- `publish = false`: the `datastore` name on crates.io belongs to an
  unrelated crate, so consume this by git tag, never by version range.

CHANGELOG is truncated to a fork notice rather than find-replaced —
rewriting upstream release history would manufacture changes Datastore
never shipped. References to upstream issues keep their numbers.
2026-07-26 11:30:32 -07:00

92 lines
2.8 KiB
Rust

use datastore::{error::Result, test, Client, Row};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Row)]
struct SomeRow {
no: u32,
}
async fn make_create(client: &Client) -> Result<()> {
client.query("CREATE TABLE test").execute().await
}
async fn make_select(client: &Client) -> Result<Vec<SomeRow>> {
client
.query("SELECT ?fields FROM `who cares`")
.fetch_all::<SomeRow>()
.await
}
async fn make_insert(client: &Client, data: &[SomeRow]) -> Result<()> {
let mut insert = client.insert("who cares")?;
for row in data {
insert.write(row).await?;
}
insert.end().await
}
#[cfg(feature = "watch")]
async fn make_watch(client: &Client) -> Result<(u64, SomeRow)> {
client
.watch("SELECT max(no) no FROM test")
.fetch_one::<SomeRow>()
.await
}
#[cfg(feature = "watch")]
async fn make_watch_only_events(client: &Client) -> Result<u64> {
client
.watch("SELECT max(no) no FROM test")
.only_events()
.fetch_one()
.await
}
#[tokio::main]
async fn main() {
let mock = test::Mock::new();
let client = Client::default().with_url(mock.url());
let list = vec![SomeRow { no: 1 }, SomeRow { no: 2 }];
// How to test DDL.
let recording = mock.add(test::handlers::record_ddl());
make_create(&client).await.unwrap();
assert!(recording.query().await.contains("CREATE TABLE"));
// How to test SELECT.
mock.add(test::handlers::provide(list.clone()));
let rows = make_select(&client).await.unwrap();
assert_eq!(rows, list);
// How to test failures.
mock.add(test::handlers::failure(test::status::FORBIDDEN));
let reason = make_select(&client).await;
assert_eq!(format!("{reason:?}"), r#"Err(BadResponse("Forbidden"))"#);
// How to test INSERT.
let recording = mock.add(test::handlers::record());
make_insert(&client, &list).await.unwrap();
let rows: Vec<SomeRow> = recording.collect().await;
assert_eq!(rows, list);
// How to test WATCH.
#[cfg(feature = "watch")]
{
// Check `CREATE LIVE VIEW` (for `watch(query)` case only).
let recording = mock.add(test::handlers::record_ddl());
mock.add(test::handlers::watch(list.into_iter().map(|row| (42, row))));
let (version, row) = make_watch(&client).await.unwrap();
assert!(recording.query().await.contains("CREATE LIVE VIEW"));
assert_eq!(version, 42);
assert_eq!(row, SomeRow { no: 1 });
// `EVENTS`.
let recording = mock.add(test::handlers::record_ddl());
mock.add(test::handlers::watch_only_events(3..5));
let version = make_watch_only_events(&client).await.unwrap();
assert!(recording.query().await.contains("CREATE LIVE VIEW"));
assert_eq!(version, 3);
}
}