elif-orm 0.7.1

Production-ready ORM with migrations, database services, connection pooling, and query builder
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
//! Database seeding system with environment controls

use super::Factory;
use crate::error::{OrmError, OrmResult};
use crate::model::Model;

/// Environment types for seeding control
#[derive(Debug, Clone, PartialEq)]
pub enum Environment {
    Development,
    Testing,
    Staging,
    Production,
    Custom(String),
}

impl Environment {
    /// Parse environment from string
    pub fn from_str(env: &str) -> Self {
        match env.to_lowercase().as_str() {
            "development" | "dev" => Environment::Development,
            "testing" | "test" => Environment::Testing,
            "staging" | "stage" => Environment::Staging,
            "production" | "prod" => Environment::Production,
            custom => Environment::Custom(custom.to_string()),
        }
    }

    /// Get environment name as string
    pub fn as_str(&self) -> &str {
        match self {
            Environment::Development => "development",
            Environment::Testing => "testing",
            Environment::Staging => "staging",
            Environment::Production => "production",
            Environment::Custom(name) => name,
        }
    }

    /// Check if this is a safe environment for seeding
    pub fn is_safe_for_seeding(&self) -> bool {
        match self {
            Environment::Development | Environment::Testing => true,
            Environment::Staging => true,     // Usually safe
            Environment::Production => false, // Requires explicit opt-in
            Environment::Custom(_) => false,  // Requires explicit opt-in
        }
    }
}

/// Seeder trait for implementing database seeders
#[async_trait::async_trait]
pub trait Seeder: Send + Sync {
    /// Get the seeder name for logging and tracking
    fn name(&self) -> &str;

    /// Get environments where this seeder should run
    fn environments(&self) -> Vec<Environment> {
        vec![Environment::Development, Environment::Testing]
    }

    /// Check if this seeder should run in the given environment
    fn should_run(&self, env: &Environment) -> bool {
        self.environments().contains(env)
    }

    /// Run the seeder
    async fn run(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()>;

    /// Optional: Clean up data created by this seeder
    async fn rollback(&self, _pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        // Default implementation does nothing
        Ok(())
    }

    /// Get seeder priority (lower numbers run first)
    fn priority(&self) -> i32 {
        100
    }

    /// Check dependencies (other seeders that must run first)
    fn dependencies(&self) -> Vec<String> {
        vec![]
    }
}

/// Factory-based seeder for easy model creation
pub struct FactorySeeder<T: Model, F: Factory<T>> {
    name: String,
    factory: F,
    count: usize,
    environments: Vec<Environment>,
    priority: i32,
    dependencies: Vec<String>,
    _phantom: std::marker::PhantomData<T>,
}

impl<T: Model, F: Factory<T>> FactorySeeder<T, F> {
    pub fn new(name: impl Into<String>, factory: F, count: usize) -> Self {
        Self {
            name: name.into(),
            factory,
            count,
            environments: vec![Environment::Development, Environment::Testing],
            priority: 100,
            dependencies: vec![],
            _phantom: std::marker::PhantomData,
        }
    }

    pub fn environments(mut self, envs: Vec<Environment>) -> Self {
        self.environments = envs;
        self
    }

    pub fn with_priority(mut self, priority: i32) -> Self {
        self.priority = priority;
        self
    }

    pub fn depends_on(mut self, dependencies: Vec<String>) -> Self {
        self.dependencies = dependencies;
        self
    }
}

#[async_trait::async_trait]
impl<T: Model + Send + Sync, F: Factory<T> + Send + Sync> Seeder for FactorySeeder<T, F> {
    fn name(&self) -> &str {
        &self.name
    }

    fn environments(&self) -> Vec<Environment> {
        self.environments.clone()
    }

    fn priority(&self) -> i32 {
        self.priority
    }

    fn dependencies(&self) -> Vec<String> {
        self.dependencies.clone()
    }

    async fn run(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        tracing::info!(
            "Running seeder: {} (creating {} records)",
            self.name,
            self.count
        );

        let models = self.factory.create_many(pool, self.count).await?;

        tracing::info!(
            "Seeder {} completed: created {} records",
            self.name,
            models.len()
        );
        Ok(())
    }

    async fn rollback(&self, _pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        tracing::info!("Rolling back seeder: {}", self.name);

        // TODO: Implement rollback logic
        // This would require tracking created records or using truncation

        tracing::warn!("Rollback not yet implemented for factory seeders");
        Ok(())
    }
}

/// Seeder manager for running multiple seeders
#[derive(Default)]
pub struct SeederManager {
    seeders: Vec<Box<dyn Seeder>>,
}

impl SeederManager {
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a seeder to the manager
    pub fn add<S: Seeder + 'static>(mut self, seeder: S) -> Self {
        self.seeders.push(Box::new(seeder));
        self
    }

    /// Add a factory seeder
    pub fn add_factory<T, F>(self, name: impl Into<String>, factory: F, count: usize) -> Self
    where
        T: Model + Send + Sync + 'static,
        F: Factory<T> + Send + Sync + 'static,
    {
        let seeder = FactorySeeder::new(name, factory, count);
        self.add(seeder)
    }

    /// Run all seeders for the given environment
    pub async fn run_for_environment(
        &self,
        pool: &sqlx::Pool<sqlx::Postgres>,
        env: &Environment,
    ) -> OrmResult<()> {
        // Filter seeders for this environment
        let mut applicable_seeders: Vec<&Box<dyn Seeder>> = self
            .seeders
            .iter()
            .filter(|seeder| seeder.should_run(env))
            .collect();

        // Sort by priority (lower numbers first)
        applicable_seeders.sort_by_key(|seeder| seeder.priority());

        tracing::info!(
            "Running {} seeders for environment: {}",
            applicable_seeders.len(),
            env.as_str()
        );

        // Check environment safety
        if !env.is_safe_for_seeding() {
            return Err(OrmError::Validation(format!(
                "Environment '{}' is not safe for automatic seeding. Use explicit opt-in.",
                env.as_str()
            )));
        }

        // Resolve dependencies with topological sorting
        let ordered_seeders = self.resolve_dependencies(applicable_seeders)?;

        for seeder in ordered_seeders {
            tracing::info!("Running seeder: {}", seeder.name());
            seeder.run(pool).await?;
        }

        tracing::info!("All seeders completed successfully");
        Ok(())
    }

    /// Run seeders in development environment
    pub async fn run_development(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        self.run_for_environment(pool, &Environment::Development)
            .await
    }

    /// Run seeders in testing environment
    pub async fn run_testing(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        self.run_for_environment(pool, &Environment::Testing).await
    }

    /// Force run seeders in production (use with caution)
    pub async fn run_production_force(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        let applicable_seeders: Vec<&Box<dyn Seeder>> = self
            .seeders
            .iter()
            .filter(|seeder| seeder.environments().contains(&Environment::Production))
            .collect();

        // Resolve dependencies even in production for safety
        let ordered_seeders = self.resolve_dependencies(applicable_seeders)?;

        tracing::warn!(
            "Force running {} seeders in PRODUCTION environment with dependency resolution",
            ordered_seeders.len()
        );

        for seeder in ordered_seeders {
            tracing::warn!("Running production seeder: {}", seeder.name());
            seeder.run(pool).await?;
        }

        Ok(())
    }

    /// Resolve seeder dependencies using topological sorting
    fn resolve_dependencies<'a>(
        &self,
        seeders: Vec<&'a Box<dyn Seeder>>,
    ) -> OrmResult<Vec<&'a Box<dyn Seeder>>> {
        use std::collections::{HashMap, HashSet, VecDeque};

        // Build a map of seeder name to seeder reference
        let mut seeder_map: HashMap<String, &'a Box<dyn Seeder>> = HashMap::new();
        let mut dependencies: HashMap<String, Vec<String>> = HashMap::new();
        let mut in_degree: HashMap<String, usize> = HashMap::new();

        // Initialize maps
        for seeder in &seeders {
            let name = seeder.name().to_string();
            seeder_map.insert(name.clone(), seeder);
            dependencies.insert(name.clone(), seeder.dependencies());
            in_degree.insert(name.clone(), 0);
        }

        // Build reverse dependency graph for efficient lookups (O(V+E) optimization)
        let mut rev_deps: HashMap<String, Vec<String>> = HashMap::new();
        for (seeder_name, deps) in &dependencies {
            for dep in deps {
                // Validate that dependency exists
                if !seeder_map.contains_key(dep) {
                    return Err(OrmError::Validation(format!(
                        "Seeder '{}' depends on '{}', but '{}' was not found",
                        seeder_name, dep, dep
                    )));
                }
                
                // Build reverse dependency graph: dep -> [list of seeders that depend on dep]
                rev_deps.entry(dep.clone()).or_default().push(seeder_name.clone());
                
                // Increment in-degree for the dependent seeder
                if let Some(degree) = in_degree.get_mut(seeder_name) {
                    *degree += 1;
                }
            }
        }

        // Kahn's algorithm for topological sorting
        let mut queue: VecDeque<String> = VecDeque::new();
        let mut result = Vec::new();
        let mut processed = HashSet::new();

        // Start with seeders that have no dependencies (in-degree = 0)
        for (name, &degree) in &in_degree {
            if degree == 0 {
                queue.push_back(name.clone());
            }
        }

        // Process queue (optimized O(V+E) implementation)
        while let Some(current) = queue.pop_front() {
            processed.insert(current.clone());
            
            // Add current seeder to result
            if let Some(seeder) = seeder_map.get(&current) {
                result.push(*seeder);
            }

            // Update in-degrees for seeders that depend on the current seeder
            if let Some(dependents) = rev_deps.get(&current) {
                for dependent in dependents {
                    if let Some(degree) = in_degree.get_mut(dependent) {
                        *degree -= 1;
                        if *degree == 0 {
                            queue.push_back(dependent.clone());
                        }
                    }
                }
            }
        }

        // Check for circular dependencies
        if result.len() != seeders.len() {
            let unprocessed: Vec<String> = seeders
                .iter()
                .map(|s| s.name().to_string())
                .filter(|name| !processed.contains(name))
                .collect();
            
            return Err(OrmError::Validation(format!(
                "Circular dependency detected in seeders: {}",
                unprocessed.join(", ")
            )));
        }

        // Secondary sort by priority for seeders at the same dependency level
        result.sort_by_key(|seeder| seeder.priority());

        Ok(result)
    }

    /// Get current environment from environment variable
    pub fn current_environment() -> Environment {
        std::env::var("ELIF_ENV")
            .or_else(|_| std::env::var("ENV"))
            .or_else(|_| std::env::var("ENVIRONMENT"))
            .map(|env| Environment::from_str(&env))
            .unwrap_or(Environment::Development)
    }

    /// Run seeders for current environment
    pub async fn run(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        let env = Self::current_environment();
        self.run_for_environment(pool, &env).await
    }
}

/// Custom seeder implementation for complex seeding logic
pub struct CustomSeeder {
    name: String,
    environments: Vec<Environment>,
    priority: i32,
    dependencies: Vec<String>,
    run_fn: Box<
        dyn Fn(
                &sqlx::Pool<sqlx::Postgres>,
            )
                -> std::pin::Pin<Box<dyn std::future::Future<Output = OrmResult<()>> + Send>>
            + Send
            + Sync,
    >,
}

impl CustomSeeder {
    pub fn new<F, Fut>(name: impl Into<String>, run_fn: F) -> Self
    where
        F: Fn(&sqlx::Pool<sqlx::Postgres>) -> Fut + Send + Sync + 'static,
        Fut: std::future::Future<Output = OrmResult<()>> + Send + 'static,
    {
        Self {
            name: name.into(),
            environments: vec![Environment::Development, Environment::Testing],
            priority: 100,
            dependencies: vec![],
            run_fn: Box::new(move |pool| Box::pin(run_fn(pool))),
        }
    }

    pub fn environments(mut self, envs: Vec<Environment>) -> Self {
        self.environments = envs;
        self
    }

    pub fn with_priority(mut self, priority: i32) -> Self {
        self.priority = priority;
        self
    }

    pub fn depends_on(mut self, dependencies: Vec<String>) -> Self {
        self.dependencies = dependencies;
        self
    }
}

#[async_trait::async_trait]
impl Seeder for CustomSeeder {
    fn name(&self) -> &str {
        &self.name
    }

    fn environments(&self) -> Vec<Environment> {
        self.environments.clone()
    }

    fn priority(&self) -> i32 {
        self.priority
    }

    fn dependencies(&self) -> Vec<String> {
        self.dependencies.clone()
    }

    async fn run(&self, pool: &sqlx::Pool<sqlx::Postgres>) -> OrmResult<()> {
        (self.run_fn)(pool).await
    }
}

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

    #[test]
    fn test_environment_parsing() {
        assert_eq!(
            Environment::from_str("development"),
            Environment::Development
        );
        assert_eq!(Environment::from_str("dev"), Environment::Development);
        assert_eq!(Environment::from_str("testing"), Environment::Testing);
        assert_eq!(Environment::from_str("test"), Environment::Testing);
        assert_eq!(Environment::from_str("production"), Environment::Production);
        assert_eq!(Environment::from_str("prod"), Environment::Production);
        assert_eq!(
            Environment::from_str("custom"),
            Environment::Custom("custom".to_string())
        );
    }

    #[test]
    fn test_environment_safety() {
        assert!(Environment::Development.is_safe_for_seeding());
        assert!(Environment::Testing.is_safe_for_seeding());
        assert!(Environment::Staging.is_safe_for_seeding());
        assert!(!Environment::Production.is_safe_for_seeding());
        assert!(!Environment::Custom("custom".to_string()).is_safe_for_seeding());
    }

    #[test]
    fn test_seeder_manager_creation() {
        let manager = SeederManager::new();
        assert_eq!(manager.seeders.len(), 0);
    }

    #[test]
    fn test_current_environment() {
        // This will use the default since we're not setting env vars in tests
        let env = SeederManager::current_environment();
        assert_eq!(env, Environment::Development);
    }

    #[tokio::test]
    async fn test_custom_seeder() {
        let seeder = CustomSeeder::new("test_seeder", |_pool| async { Ok(()) });

        assert_eq!(seeder.name(), "test_seeder");
        assert_eq!(Seeder::priority(&seeder), 100);
        assert!(seeder.should_run(&Environment::Development));
    }
}