Name SQL cluster configuration explicitly
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
`driver.rs` 集中定义驱动类型,供 SQL 和对象存储配置模块使用。
|
||||
|
||||
`sql.rs` 定义 `SqlCluster`,描述 SQL 主库和可选读副本。`catalog.rs` 的 `Resource` 统一表示 SQL 和对象存储资源。
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
lib["lib.rs"] -->|解析 SQL 配置| sql["sql.rs"]
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{Cluster, ConfigError, DeploymentConfig, Driver, ObjectStorage, validate_name};
|
||||
use crate::{ConfigError, DeploymentConfig, Driver, ObjectStorage, SqlCluster, validate_name};
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(tag = "kind", rename_all = "snake_case")]
|
||||
pub enum Resource {
|
||||
Sql(Cluster),
|
||||
Sql(SqlCluster),
|
||||
ObjectStore(ObjectStorage),
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum ResourceRef<'a> {
|
||||
Sql(&'a Cluster),
|
||||
Sql(&'a SqlCluster),
|
||||
ObjectStore(&'a ObjectStorage),
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,9 @@ pub use catalog::{Resource, ResourceRef};
|
||||
pub use driver::*;
|
||||
pub use format::{ConfigFormat, FormatError, ImportFormat};
|
||||
pub use object_storage::ObjectStorage;
|
||||
pub use sql::{Address, Cluster, ResolvedSql, Tls};
|
||||
/// Compatibility name for the SQL cluster configuration.
|
||||
pub use sql::SqlCluster as Cluster;
|
||||
pub use sql::{Address, ResolvedSql, SqlCluster, Tls};
|
||||
|
||||
pub const DOCUMENTS: &str = "documents";
|
||||
|
||||
@@ -26,7 +28,8 @@ pub struct DeploymentConfig {
|
||||
pub config_version: String,
|
||||
/// Legacy shorthand for sql.documents.
|
||||
pub cluster: Option<String>,
|
||||
pub clusters: BTreeMap<String, Cluster>,
|
||||
/// Named SQL targets that can be bound to entries in `sql`.
|
||||
pub clusters: BTreeMap<String, SqlCluster>,
|
||||
/// SQL role to named cluster.
|
||||
pub sql: BTreeMap<String, String>,
|
||||
pub object_storage: BTreeMap<String, ObjectStorage>,
|
||||
@@ -137,7 +140,7 @@ impl DeploymentConfig {
|
||||
if !targets.is_empty() {
|
||||
return Err(ConfigError::ClusterRequired);
|
||||
}
|
||||
let local = Cluster {
|
||||
let local = SqlCluster {
|
||||
driver: SqlDriver::Sqlite,
|
||||
primary: self.sqlite_path.clone(),
|
||||
replicas: vec![],
|
||||
|
||||
@@ -42,9 +42,10 @@ impl Address {
|
||||
}
|
||||
}
|
||||
|
||||
/// A SQL target with a primary connection and optional read replicas.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Cluster {
|
||||
pub struct SqlCluster {
|
||||
pub driver: SqlDriver,
|
||||
pub primary: Address,
|
||||
#[serde(default)]
|
||||
@@ -72,7 +73,7 @@ pub struct ResolvedSql {
|
||||
pub max_connections: u32,
|
||||
}
|
||||
|
||||
impl Cluster {
|
||||
impl SqlCluster {
|
||||
pub(crate) fn resolve(
|
||||
&self,
|
||||
deployment: &str,
|
||||
@@ -104,7 +105,7 @@ impl Cluster {
|
||||
}
|
||||
|
||||
fn resolve_address(
|
||||
cluster: &Cluster,
|
||||
cluster: &SqlCluster,
|
||||
address: &Address,
|
||||
read_only: bool,
|
||||
) -> Result<Address, ConfigError> {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use clusters::{Address, Cluster, ConfigError, DeploymentConfig, SqlDriver, Tls};
|
||||
use clusters::{Address, ConfigError, DeploymentConfig, SqlCluster, SqlDriver, Tls};
|
||||
|
||||
#[test]
|
||||
fn address_debug_formats_the_inner_string() {
|
||||
@@ -14,7 +14,7 @@ fn named_cluster_requires_matching_addresses() {
|
||||
cluster: Some("regional".into()),
|
||||
clusters: BTreeMap::from([(
|
||||
"regional".into(),
|
||||
Cluster {
|
||||
SqlCluster {
|
||||
driver: SqlDriver::Postgres,
|
||||
primary: "postgres://user:secret@primary/bedrock".into(),
|
||||
replicas: vec!["postgresql://reader:secret@replica/bedrock".into()],
|
||||
|
||||
@@ -7,7 +7,9 @@ pub mod postgres;
|
||||
pub mod sqlite;
|
||||
|
||||
pub use clusters::SqlDriver as Dialect;
|
||||
pub use clusters::{Address, Cluster, ConfigError, DeploymentConfig, SqlDriver as Driver, Tls};
|
||||
pub use clusters::{
|
||||
Address, Cluster, ConfigError, DeploymentConfig, SqlCluster, SqlDriver as Driver, Tls,
|
||||
};
|
||||
pub use common::Error as StorageError;
|
||||
pub use common::{Document, Error, Mutation, Result, Revision, Snapshot};
|
||||
pub use connection::{
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use std::{collections::BTreeMap, env, time::Duration};
|
||||
|
||||
use db::{
|
||||
Cluster, Connection, DeploymentConfig, Driver, Mutation, ReadPreference, StorageError, Tls,
|
||||
Connection, DeploymentConfig, Driver, Mutation, ReadPreference, SqlCluster, StorageError, Tls,
|
||||
connect,
|
||||
};
|
||||
use serde_json::json;
|
||||
@@ -20,7 +20,7 @@ fn sql_config(driver: Driver, url: &str, deployment: &str) -> DeploymentConfig {
|
||||
cluster: Some("test".into()),
|
||||
clusters: BTreeMap::from([(
|
||||
"test".into(),
|
||||
Cluster {
|
||||
SqlCluster {
|
||||
driver,
|
||||
primary: url.into(),
|
||||
replicas: vec![],
|
||||
|
||||
Reference in New Issue
Block a user