supabase_management_rs/
postgres_configs.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents the configuration settings for a Postgres database.
4///
5/// ```
6/// # use supabase_management_rs::PostgresConfig;
7///
8/// let mut postgres_config = PostgresConfig::default();
9/// postgres_config.max_parallel_workers = Some(128);
10///
11/// // You can now update your instance's Postgres configuration using Client::set_postgres_config
12/// ```
13#[derive(Debug, Clone, Deserialize, Serialize, Default)]
14pub struct PostgresConfig {
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub effective_cache_size: Option<String>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub logical_decoding_work_mem: Option<String>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub maintenance_work_mem: Option<String>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    pub track_activity_query_size: Option<String>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub max_connections: Option<i32>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    pub max_locks_per_transaction: Option<i32>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    pub max_parallel_maintenance_workers: Option<i32>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub max_parallel_workers: Option<i32>,
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub max_parallel_workers_per_gather: Option<i32>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub max_replication_slots: Option<i32>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub max_slot_wal_keep_size: Option<String>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub max_standby_archive_delay: Option<String>,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub max_standby_streaming_delay: Option<String>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub max_wal_size: Option<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub max_wal_senders: Option<i32>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub max_worker_processes: Option<i32>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub shared_buffers: Option<String>,
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub statement_timeout: Option<String>,
51    #[serde(skip_serializing_if = "Option::is_none")]
52    pub track_commit_timestamp: Option<bool>,
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub wal_keep_size: Option<String>,
55    #[serde(skip_serializing_if = "Option::is_none")]
56    pub wal_sender_timeout: Option<String>,
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub work_mem: Option<String>,
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub session_replication_role: Option<SessionReplicationRole>,
61}
62
63#[derive(Debug, Clone, Deserialize, Serialize)]
64#[serde(rename_all = "lowercase")]
65pub enum SessionReplicationRole {
66    Origin,
67    Replica,
68    Local,
69}
70
71impl crate::Client {
72    /// Get the Postgres configs for a project
73    pub async fn get_postgres_config(
74        &self,
75        project_id: &str,
76    ) -> Result<PostgresConfig, crate::Error> {
77        self.get(format!("projects/{project_id}/config/database/postgres"))
78            .await
79    }
80
81    pub async fn set_postgres_config(
82        &self,
83        project_id: &str,
84        config: &PostgresConfig,
85    ) -> Result<PostgresConfig, crate::Error> {
86        self.put(
87            format!("projects/{project_id}/config/database/postgres"),
88            Some(config),
89        )
90        .await
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::PostgresConfig;
97
98    #[test]
99    fn deserializes_pg_config() {
100        let json_data = r#"
101        {
102            "effective_cache_size": "4GB",
103            "logical_decoding_work_mem": "64MB",
104            "maintenance_work_mem": "256MB",
105            "track_activity_query_size": "2048",
106            "max_connections": 100,
107            "max_locks_per_transaction": 128,
108            "max_parallel_maintenance_workers": 2,
109            "max_parallel_workers": 4,
110            "max_parallel_workers_per_gather": 2,
111            "max_replication_slots": 10,
112            "max_slot_wal_keep_size": "1GB",
113            "max_standby_archive_delay": "30s",
114            "max_standby_streaming_delay": "30s",
115            "max_wal_size": "2GB",
116            "max_wal_senders": 5,
117            "max_worker_processes": 8,
118            "shared_buffers": "2GB",
119            "statement_timeout": "60s",
120            "track_commit_timestamp": true,
121            "wal_keep_size": "512MB",
122            "wal_sender_timeout": "60s",
123            "work_mem": "4MB",
124            "session_replication_role": "replica"
125        }
126        "#;
127
128        let _config: PostgresConfig =
129            serde_json::from_str(json_data).expect("JSON was not well-formatted");
130    }
131}