data-modelling-core 2.4.0

Core SDK library for model operations across platforms
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//! Database configuration file support
//!
//! Handles parsing of `.data-model.toml` configuration files and
//! environment variable overrides.

use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

use super::{DatabaseError, DatabaseResult};

/// Default database filename for DuckDB
pub const DEFAULT_DUCKDB_FILENAME: &str = ".data-model.duckdb";

/// Default configuration filename
pub const CONFIG_FILENAME: &str = ".data-model.toml";

/// Environment variable for database backend
pub const ENV_DB_BACKEND: &str = "DATA_MODEL_DB_BACKEND";

/// Environment variable for DuckDB path
pub const ENV_DUCKDB_PATH: &str = "DATA_MODEL_DUCKDB_PATH";

/// Environment variable for PostgreSQL connection string
pub const ENV_POSTGRES_URL: &str = "DATA_MODEL_POSTGRES_URL";

/// Environment variable for PostgreSQL pool size
pub const ENV_POSTGRES_POOL_SIZE: &str = "DATA_MODEL_POSTGRES_POOL_SIZE";

/// Database backend type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DatabaseBackendType {
    /// DuckDB embedded database (default)
    #[default]
    DuckDB,
    /// PostgreSQL database
    Postgres,
}

impl std::str::FromStr for DatabaseBackendType {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "duckdb" => Ok(DatabaseBackendType::DuckDB),
            "postgres" | "postgresql" => Ok(DatabaseBackendType::Postgres),
            _ => Err(format!(
                "Unknown database backend: {}. Use 'duckdb' or 'postgres'.",
                s
            )),
        }
    }
}

impl std::fmt::Display for DatabaseBackendType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DatabaseBackendType::DuckDB => write!(f, "duckdb"),
            DatabaseBackendType::Postgres => write!(f, "postgres"),
        }
    }
}

/// Database configuration section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DatabaseSection {
    /// Database backend type
    #[serde(default)]
    pub backend: DatabaseBackendType,

    /// Path to DuckDB database file (relative to workspace)
    #[serde(default = "default_duckdb_path")]
    pub path: String,
}

fn default_duckdb_path() -> String {
    DEFAULT_DUCKDB_FILENAME.to_string()
}

impl Default for DatabaseSection {
    fn default() -> Self {
        Self {
            backend: DatabaseBackendType::default(),
            path: default_duckdb_path(),
        }
    }
}

/// PostgreSQL configuration section
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct PostgresSection {
    /// Connection string (e.g., "postgresql://user:pass@localhost/db")
    #[serde(default)]
    pub connection_string: Option<String>,

    /// Connection pool size
    #[serde(default = "default_pool_size")]
    pub pool_size: usize,
}

fn default_pool_size() -> usize {
    5
}

/// Sync configuration section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncSection {
    /// Enable automatic sync on file changes
    #[serde(default)]
    pub auto_sync: bool,

    /// Enable file watching
    #[serde(default)]
    pub watch: bool,
}

impl Default for SyncSection {
    fn default() -> Self {
        Self {
            auto_sync: true,
            watch: false,
        }
    }
}

/// Git integration configuration section
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GitSection {
    /// Enable Git hooks
    #[serde(default = "default_hooks_enabled")]
    pub hooks_enabled: bool,
}

fn default_hooks_enabled() -> bool {
    true
}

impl Default for GitSection {
    fn default() -> Self {
        Self {
            hooks_enabled: default_hooks_enabled(),
        }
    }
}

/// Main configuration structure
///
/// Represents the `.data-model.toml` configuration file format.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct DatabaseConfig {
    /// Database configuration
    #[serde(default)]
    pub database: DatabaseSection,

    /// PostgreSQL-specific configuration
    #[serde(default)]
    pub postgres: PostgresSection,

    /// Sync configuration
    #[serde(default)]
    pub sync: SyncSection,

    /// Git integration configuration
    #[serde(default)]
    pub git: GitSection,
}

impl DatabaseConfig {
    /// Create a new default configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Create a DuckDB configuration
    pub fn duckdb(path: impl Into<String>) -> Self {
        Self {
            database: DatabaseSection {
                backend: DatabaseBackendType::DuckDB,
                path: path.into(),
            },
            ..Default::default()
        }
    }

    /// Create a PostgreSQL configuration
    pub fn postgres(connection_string: impl Into<String>) -> Self {
        Self {
            database: DatabaseSection {
                backend: DatabaseBackendType::Postgres,
                path: String::new(),
            },
            postgres: PostgresSection {
                connection_string: Some(connection_string.into()),
                pool_size: default_pool_size(),
            },
            ..Default::default()
        }
    }

    /// Load configuration from a workspace directory
    ///
    /// Looks for `.data-model.toml` in the workspace directory.
    /// Falls back to defaults if not found.
    pub fn load(workspace_path: &Path) -> DatabaseResult<Self> {
        let config_path = workspace_path.join(CONFIG_FILENAME);

        let mut config = if config_path.exists() {
            let content = std::fs::read_to_string(&config_path)
                .map_err(|e| DatabaseError::IoError(format!("Failed to read config: {}", e)))?;

            Self::parse(&content)?
        } else {
            Self::default()
        };

        // Apply environment variable overrides
        config.apply_env_overrides();

        Ok(config)
    }

    /// Parse configuration from TOML string
    pub fn parse(content: &str) -> DatabaseResult<Self> {
        toml::from_str(content)
            .map_err(|e| DatabaseError::ConfigError(format!("Failed to parse config: {}", e)))
    }

    /// Save configuration to a workspace directory
    pub fn save(&self, workspace_path: &Path) -> DatabaseResult<()> {
        let config_path = workspace_path.join(CONFIG_FILENAME);
        let content = self.to_toml()?;

        std::fs::write(&config_path, content)
            .map_err(|e| DatabaseError::IoError(format!("Failed to write config: {}", e)))?;

        Ok(())
    }

    /// Convert configuration to TOML string
    pub fn to_toml(&self) -> DatabaseResult<String> {
        toml::to_string_pretty(self).map_err(|e| {
            DatabaseError::SerializationError(format!("Failed to serialize config: {}", e))
        })
    }

    /// Apply environment variable overrides
    pub fn apply_env_overrides(&mut self) {
        // Backend type
        if let Ok(backend) = std::env::var(ENV_DB_BACKEND)
            && let Ok(backend_type) = backend.parse()
        {
            self.database.backend = backend_type;
        }

        // DuckDB path
        if let Ok(path) = std::env::var(ENV_DUCKDB_PATH) {
            self.database.path = path;
        }

        // PostgreSQL connection string
        if let Ok(url) = std::env::var(ENV_POSTGRES_URL) {
            self.postgres.connection_string = Some(url);
        }

        // PostgreSQL pool size
        if let Ok(size) = std::env::var(ENV_POSTGRES_POOL_SIZE)
            && let Ok(size) = size.parse()
        {
            self.postgres.pool_size = size;
        }
    }

    /// Get the DuckDB database path for a workspace
    pub fn get_duckdb_path(&self, workspace_path: &Path) -> PathBuf {
        if self.database.path.is_empty() {
            workspace_path.join(DEFAULT_DUCKDB_FILENAME)
        } else if Path::new(&self.database.path).is_absolute() {
            PathBuf::from(&self.database.path)
        } else {
            workspace_path.join(&self.database.path)
        }
    }

    /// Get the PostgreSQL connection string
    pub fn get_postgres_connection_string(&self) -> Option<&str> {
        self.postgres.connection_string.as_deref()
    }

    /// Check if configuration exists in a workspace
    pub fn exists(workspace_path: &Path) -> bool {
        workspace_path.join(CONFIG_FILENAME).exists()
    }

    /// Check if database is initialized (config file exists)
    pub fn is_initialized(workspace_path: &Path) -> bool {
        let config_path = workspace_path.join(CONFIG_FILENAME);
        if !config_path.exists() {
            return false;
        }

        // Also check if the database file/connection is valid
        if let Ok(config) = Self::load(workspace_path) {
            match config.database.backend {
                DatabaseBackendType::DuckDB => config.get_duckdb_path(workspace_path).exists(),
                DatabaseBackendType::Postgres => config.postgres.connection_string.is_some(),
            }
        } else {
            false
        }
    }
}

/// Generate a sample configuration file content
pub fn sample_config() -> &'static str {
    r#"# Data Model SDK Configuration
# This file configures the database backend for the data modelling SDK.

[database]
# Database backend: "duckdb" (default) or "postgres"
backend = "duckdb"

# Path to DuckDB database file (relative to workspace, or absolute)
path = ".data-model.duckdb"

# PostgreSQL configuration (used when backend = "postgres")
[database.postgres]
# connection_string = "postgresql://user:password@localhost:5432/datamodel"
pool_size = 5

[sync]
# Enable automatic sync when files change
auto_sync = true

# Enable file watching (requires --watch flag)
watch = false

[git]
# Enable Git hooks for automatic database rebuild
hooks_enabled = true
"#
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[test]
    fn test_default_config() {
        let config = DatabaseConfig::new();
        assert_eq!(config.database.backend, DatabaseBackendType::DuckDB);
        assert_eq!(config.database.path, DEFAULT_DUCKDB_FILENAME);
        assert!(config.sync.auto_sync);
        assert!(config.git.hooks_enabled);
    }

    #[test]
    fn test_parse_config() {
        let toml = r#"
[database]
backend = "duckdb"
path = "custom.duckdb"

[sync]
auto_sync = false

[git]
hooks_enabled = false
"#;
        let config = DatabaseConfig::parse(toml).unwrap();
        assert_eq!(config.database.backend, DatabaseBackendType::DuckDB);
        assert_eq!(config.database.path, "custom.duckdb");
        assert!(!config.sync.auto_sync);
        assert!(!config.git.hooks_enabled);
    }

    #[test]
    fn test_parse_postgres_config() {
        let toml = r#"
[database]
backend = "postgres"

[postgres]
connection_string = "postgresql://localhost/test"
pool_size = 10
"#;
        let config = DatabaseConfig::parse(toml).unwrap();
        assert_eq!(config.database.backend, DatabaseBackendType::Postgres);
        assert_eq!(
            config.postgres.connection_string,
            Some("postgresql://localhost/test".to_string())
        );
        assert_eq!(config.postgres.pool_size, 10);
    }

    #[test]
    fn test_to_toml() {
        let config = DatabaseConfig::duckdb("test.duckdb");
        let toml = config.to_toml().unwrap();
        assert!(toml.contains("duckdb"));
        assert!(toml.contains("test.duckdb"));
    }

    #[test]
    fn test_save_and_load() {
        let dir = tempdir().unwrap();
        let config = DatabaseConfig::duckdb("my-db.duckdb");

        config.save(dir.path()).unwrap();
        assert!(dir.path().join(CONFIG_FILENAME).exists());

        let loaded = DatabaseConfig::load(dir.path()).unwrap();
        assert_eq!(loaded.database.path, "my-db.duckdb");
    }

    #[test]
    fn test_get_duckdb_path() {
        let config = DatabaseConfig::duckdb("relative.duckdb");
        let workspace = Path::new("/workspace");
        assert_eq!(
            config.get_duckdb_path(workspace),
            PathBuf::from("/workspace/relative.duckdb")
        );
    }

    #[test]
    fn test_backend_type_from_str() {
        assert_eq!(
            "duckdb".parse::<DatabaseBackendType>().unwrap(),
            DatabaseBackendType::DuckDB
        );
        assert_eq!(
            "postgres".parse::<DatabaseBackendType>().unwrap(),
            DatabaseBackendType::Postgres
        );
        assert_eq!(
            "postgresql".parse::<DatabaseBackendType>().unwrap(),
            DatabaseBackendType::Postgres
        );
        assert!("invalid".parse::<DatabaseBackendType>().is_err());
    }

    #[test]
    fn test_sample_config_is_valid() {
        let sample = sample_config();
        // Should parse without error
        let result = DatabaseConfig::parse(sample);
        assert!(result.is_ok(), "Sample config should be valid TOML");
    }
}