reinhardt-conf 0.3.2

Configuration management framework for Reinhardt - Django-inspired settings with encryption and secrets management
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
//! Database backend for dynamic settings
//!
//! This backend provides runtime configuration storage using SQL databases via sqlx.
//!
//! ## Features
//!
//! This module is only available when the `dynamic-database` feature is enabled.
//!
//! ## Database Schema
//!
//! The backend expects a table with the following structure:
//!
//! ```sql
//! CREATE TABLE settings (
//!     key VARCHAR(255) PRIMARY KEY,
//!     value TEXT NOT NULL,
//!     expire_date TIMESTAMP
//! );
//! CREATE INDEX idx_settings_expire_date ON settings(expire_date);
//! ```
//!
//! ## Example
//!
//! ```rust,no_run
//! # #[cfg(feature = "dynamic-database")]
//! # async fn example() -> Result<(), String> {
//! use reinhardt_conf::settings::backends::DatabaseBackend;
//! use serde_json::json;
//!
//! // Create backend
//! let backend = DatabaseBackend::new("postgres://user:pass@localhost/db").await?;
//!
//! // Create table
//! backend.create_table().await?;
//!
//! // Set a value with TTL
//! let value = json!({"debug": true, "port": 8080});
//! backend.set("app_config", &value, Some(3600)).await?;
//!
//! // Get the value
//! let retrieved = backend.get("app_config").await?;
//! assert!(retrieved.is_some());
//!
//! // Delete the value
//! backend.delete("app_config").await?;
//! # Ok(())
//! # }
//! ```

#[cfg(feature = "dynamic-database")]
use chrono::{DateTime, Duration, Utc};
#[cfg(feature = "dynamic-database")]
use reinhardt_query::prelude::{
	MySqlQueryBuilder, PostgresQueryBuilder, QueryStatementBuilder, SqliteQueryBuilder,
};
#[cfg(feature = "dynamic-database")]
use sqlx::{AnyPool, Row};
#[cfg(feature = "dynamic-database")]
use std::sync::Arc;

#[cfg(feature = "dynamic-database")]
use crate::settings::dynamic::{DynamicBackend, DynamicError, DynamicResult};
#[cfg(feature = "dynamic-database")]
use async_trait::async_trait;

use crate::settings::database_config::validate_database_url_scheme;

/// Database backend for runtime configuration changes
///
/// This backend allows dynamic settings to be stored in and retrieved from SQL databases,
/// enabling runtime configuration changes without application restarts.
///
/// Supports PostgreSQL, MySQL, and SQLite through sqlx.
///
/// ## Example
///
/// ```rust,no_run
/// # #[cfg(feature = "dynamic-database")]
/// # async fn example() -> Result<(), String> {
/// use reinhardt_conf::settings::backends::DatabaseBackend;
/// use serde_json::json;
///
/// let backend = DatabaseBackend::new("postgres://localhost/settings").await?;
/// backend.create_table().await?;
///
/// // Store configuration
/// backend.set("feature_flags", &json!({"new_ui": true}), None).await?;
///
/// // Retrieve configuration
/// let config = backend.get("feature_flags").await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone)]
pub struct DatabaseBackend {
	#[cfg(feature = "dynamic-database")]
	pool: Arc<AnyPool>,
	#[cfg(feature = "dynamic-database")]
	database_url: String,
	#[cfg(not(feature = "dynamic-database"))]
	_phantom: std::marker::PhantomData<()>,
}

impl DatabaseBackend {
	/// Create a new database backend
	///
	/// Initializes a connection pool to the specified database URL.
	///
	/// ## Arguments
	///
	/// * `connection_url` - Database connection URL
	///   - PostgreSQL: `postgres://user:pass@localhost/dbname`
	///   - MySQL: `mysql://user:pass@localhost/dbname`
	///   - SQLite: `sqlite://path/to/db.sqlite`
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	///
	/// // PostgreSQL
	/// let backend = DatabaseBackend::new("postgres://user:pass@localhost/db").await?;
	///
	/// // SQLite
	/// let backend = DatabaseBackend::new("sqlite://settings.db").await?;
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn new(connection_url: &str) -> Result<Self, String> {
		validate_database_url_scheme(connection_url)?;

		let pool = AnyPool::connect(connection_url)
			.await
			.map_err(|e| format!("Database connection error: {}", e))?;

		Ok(Self {
			pool: Arc::new(pool),
			database_url: connection_url.to_string(),
		})
	}

	#[cfg(not(feature = "dynamic-database"))]
	pub async fn new(_connection_url: &str) -> Result<Self, String> {
		Err("Database backend not enabled. Enable the 'dynamic-database' feature.".to_string())
	}

	/// Create a new backend from an existing connection pool
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use sqlx::AnyPool;
	/// use std::sync::Arc;
	///
	/// let pool = AnyPool::connect("sqlite::memory:").await.map_err(|e| e.to_string())?;
	/// let backend = DatabaseBackend::from_pool(Arc::new(pool), "sqlite::memory:");
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub fn from_pool(pool: Arc<AnyPool>, database_url: &str) -> Self {
		Self {
			pool,
			database_url: database_url.to_string(),
		}
	}

	/// Detect database backend type from URL
	///
	/// Returns the database backend type based on the connection URL format.
	#[cfg(feature = "dynamic-database")]
	fn detect_backend(&self) -> &'static str {
		if self.database_url.starts_with("postgres://")
			|| self.database_url.starts_with("postgresql://")
		{
			"postgres"
		} else if self.database_url.starts_with("mysql://") {
			"mysql"
		} else {
			"sqlite"
		}
	}

	/// Build SQL string for the current database backend
	///
	/// Uses reinhardt-query to generate database-specific SQL syntax for queries.
	#[cfg(feature = "dynamic-database")]
	fn build_sql<T>(&self, statement: T) -> String
	where
		T: QueryStatementBuilder,
	{
		match self.detect_backend() {
			"postgres" => statement.to_string(PostgresQueryBuilder),
			"mysql" => statement.to_string(MySqlQueryBuilder),
			_ => statement.to_string(SqliteQueryBuilder),
		}
	}

	/// Build table SQL string for the current database backend
	///
	/// Uses reinhardt-query to generate database-specific SQL syntax for DDL operations.
	#[cfg(feature = "dynamic-database")]
	fn build_table_sql<T>(&self, statement: T) -> String
	where
		T: QueryStatementBuilder,
	{
		match self.detect_backend() {
			"postgres" => statement.to_string(PostgresQueryBuilder),
			"mysql" => statement.to_string(MySqlQueryBuilder),
			_ => statement.to_string(SqliteQueryBuilder),
		}
	}

	/// Build index SQL string for the current database backend
	///
	/// Uses reinhardt-query to generate database-specific SQL syntax for index creation.
	#[cfg(feature = "dynamic-database")]
	fn build_index_sql(
		&self,
		statement: &reinhardt_query::prelude::CreateIndexStatement,
	) -> String {
		match self.detect_backend() {
			"postgres" => statement.to_string(PostgresQueryBuilder),
			"mysql" => statement.to_string(MySqlQueryBuilder),
			_ => statement.to_string(SqliteQueryBuilder),
		}
	}

	/// Create the settings table if it doesn't exist
	///
	/// Creates the required database table for settings storage.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn create_table(&self) -> Result<(), String> {
		use reinhardt_query::prelude::{Alias, ColumnDef, Query};

		// Build CREATE TABLE statement using reinhardt-query
		let stmt = Query::create_table()
			.table(Alias::new("settings"))
			.if_not_exists()
			.col(
				ColumnDef::new(Alias::new("key"))
					.string_len(255)
					.not_null(true)
					.primary_key(true),
			)
			.col(ColumnDef::new(Alias::new("value")).text().not_null(true))
			.col(ColumnDef::new(Alias::new("expire_date")).text())
			.to_owned();

		let sql = self.build_table_sql(stmt);

		sqlx::query(&sql)
			.execute(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to create table: {}", e))?;

		// Create index on expire_date for efficient cleanup using reinhardt-query
		let index_stmt = Query::create_index()
			.if_not_exists()
			.name("idx_settings_expire_date")
			.table(Alias::new("settings"))
			.col(Alias::new("expire_date"))
			.to_owned();

		let index_sql = self.build_index_sql(&index_stmt);

		sqlx::query(&index_sql)
			.execute(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to create index: {}", e))?;

		Ok(())
	}

	/// Get a setting value by key
	///
	/// Returns `None` if the key doesn't exist or has expired.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use serde_json::json;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// backend.set("key", &json!("value"), None).await?;
	/// let value = backend.get("key").await?;
	/// assert_eq!(value, Some(json!("value")));
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn get(&self, key: &str) -> Result<Option<serde_json::Value>, String> {
		use reinhardt_query::prelude::{Alias, Expr, ExprTrait, Query};

		// Build SELECT query using reinhardt-query
		let stmt = Query::select()
			.columns([Alias::new("value"), Alias::new("expire_date")])
			.from(Alias::new("settings"))
			.and_where(Expr::col(Alias::new("key")).eq(key))
			.to_owned();

		let sql = self.build_sql(stmt);

		let row = sqlx::query(&sql)
			.fetch_optional(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to get setting: {}", e))?;

		match row {
			Some(row) => {
				// Check if setting has expired
				// Use index-based access for MySQL compatibility
				// MySQL TEXT columns may be returned as BLOB, so handle both String and Vec<u8>
				let expire_date_str: Option<String> = {
					if let Ok(s) = row.try_get::<String, _>(1) {
						Some(s)
					} else if let Ok(bytes) = row.try_get::<Vec<u8>, _>(1) {
						String::from_utf8(bytes).ok()
					} else {
						None
					}
				};

				if let Some(expire_date_str) = expire_date_str
					&& let Ok(expire_date) = DateTime::parse_from_rfc3339(&expire_date_str)
					&& expire_date.with_timezone(&Utc) < Utc::now()
				{
					// Setting expired, delete it
					let _ = self.delete(key).await;
					return Ok(None);
				}

				// Use index-based access for MySQL compatibility (value is first column, index 0)
				// MySQL TEXT columns may be returned as BLOB, so try Vec<u8> first
				let value: String = if let Ok(s) = row.try_get::<String, _>(0) {
					s
				} else if let Ok(bytes) = row.try_get::<Vec<u8>, _>(0) {
					String::from_utf8(bytes)
						.map_err(|e| format!("Invalid UTF-8 in value column: {}", e))?
				} else {
					return Err("Missing value column".to_string());
				};

				let data: serde_json::Value = serde_json::from_str(&value)
					.map_err(|e| format!("Deserialization error: {}", e))?;

				Ok(Some(data))
			}
			None => Ok(None),
		}
	}

	/// Set a setting value with optional TTL
	///
	/// ## Arguments
	///
	/// * `key` - Setting key
	/// * `value` - Setting value (must be JSON-serializable)
	/// * `ttl` - Optional time-to-live in seconds
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use serde_json::json;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// // Set with 1 hour TTL
	/// backend.set("temp_config", &json!({"enabled": true}), Some(3600)).await?;
	///
	/// // Set without TTL (permanent)
	/// backend.set("permanent_config", &json!({"version": "1.0"}), None).await?;
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn set(
		&self,
		key: &str,
		value: &serde_json::Value,
		ttl: Option<u64>,
	) -> Result<(), String> {
		let value_str =
			serde_json::to_string(value).map_err(|e| format!("Serialization error: {}", e))?;

		let expire_date_str = ttl.map(|seconds| {
			let expire_date = Utc::now() + Duration::seconds(seconds as i64);
			expire_date.to_rfc3339()
		});

		// Delete existing key first for simplicity (works across all databases)
		let _ = self.delete(key).await;

		use reinhardt_query::prelude::{Alias, IntoValue, Query};

		// Build INSERT query in a block scope so the non-Send SeaQuery
		// statement is dropped before the await point.
		let sql = {
			let mut stmt = Query::insert()
				.into_table(Alias::new("settings"))
				.columns([
					Alias::new("key"),
					Alias::new("value"),
					Alias::new("expire_date"),
				])
				.to_owned();

			// Add values based on whether expire_date is set
			if let Some(expire_str) = &expire_date_str {
				stmt.values_panic(vec![
					key.into_value(),
					value_str.into_value(),
					expire_str.clone().into_value(),
				]);
			} else {
				stmt.values_panic(vec![
					key.into_value(),
					value_str.into_value(),
					Option::<String>::None.into_value(),
				]);
			}

			self.build_sql(stmt)
		};

		sqlx::query(&sql)
			.execute(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to set setting: {}", e))?;

		Ok(())
	}

	/// Delete a setting by key
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use serde_json::json;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// backend.set("key", &json!("value"), None).await?;
	/// backend.delete("key").await?;
	/// assert!(!backend.exists("key").await?);
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn delete(&self, key: &str) -> Result<(), String> {
		use reinhardt_query::prelude::{Alias, Expr, ExprTrait, Query};

		// Build DELETE query using reinhardt-query
		let stmt = Query::delete()
			.from_table(Alias::new("settings"))
			.and_where(Expr::col(Alias::new("key")).eq(key))
			.to_owned();

		let sql = self.build_sql(stmt);

		sqlx::query(&sql)
			.execute(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to delete setting: {}", e))?;

		Ok(())
	}

	/// Check if a setting exists and is not expired
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use serde_json::json;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// assert!(!backend.exists("nonexistent").await?);
	///
	/// backend.set("key", &json!("value"), None).await?;
	/// assert!(backend.exists("key").await?);
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn exists(&self, key: &str) -> Result<bool, String> {
		use reinhardt_query::prelude::{Alias, Cond, Expr, ExprTrait, Query};

		let now = Utc::now().to_rfc3339();

		// Build SELECT query using reinhardt-query
		// WHERE key = ? AND (expire_date IS NULL OR expire_date > ?)
		let stmt = Query::select()
			.expr(Expr::value(1))
			.from(Alias::new("settings"))
			.and_where(Expr::col(Alias::new("key")).eq(key))
			.and_where(
				Cond::any()
					.add(Expr::col(Alias::new("expire_date")).is_null())
					.add(Expr::col(Alias::new("expire_date")).gt(Expr::value(&now))),
			)
			.to_owned();

		let sql = self.build_sql(stmt);

		let row = sqlx::query(&sql)
			.fetch_optional(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to check setting existence: {}", e))?;

		Ok(row.is_some())
	}

	/// Clean up expired settings
	///
	/// Deletes all settings that have passed their expiration time.
	/// This should be called periodically to prevent database bloat.
	///
	/// Returns the number of deleted settings.
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// let deleted_count = backend.cleanup_expired().await?;
	/// println!("Deleted {} expired settings", deleted_count);
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn cleanup_expired(&self) -> Result<u64, String> {
		use reinhardt_query::prelude::{Alias, Expr, ExprTrait, Query};

		let now = Utc::now().to_rfc3339();

		// Build DELETE query using reinhardt-query
		let stmt = Query::delete()
			.from_table(Alias::new("settings"))
			.and_where(Expr::col(Alias::new("expire_date")).lt(Expr::value(&now)))
			.to_owned();

		let sql = self.build_sql(stmt);

		let rows_affected = sqlx::query(&sql)
			.execute(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to cleanup settings: {}", e))?
			.rows_affected();

		Ok(rows_affected)
	}

	/// Get all non-expired setting keys
	///
	/// ## Example
	///
	/// ```rust,no_run
	/// # #[cfg(feature = "dynamic-database")]
	/// # async fn example() -> Result<(), String> {
	/// use reinhardt_conf::settings::backends::DatabaseBackend;
	/// use serde_json::json;
	///
	/// let backend = DatabaseBackend::new("sqlite::memory:").await?;
	/// backend.create_table().await?;
	///
	/// backend.set("key1", &json!("value1"), None).await?;
	/// backend.set("key2", &json!("value2"), None).await?;
	///
	/// let keys = backend.keys().await?;
	/// assert!(keys.contains(&"key1".to_string()));
	/// assert!(keys.contains(&"key2".to_string()));
	/// # Ok(())
	/// # }
	/// ```
	#[cfg(feature = "dynamic-database")]
	pub async fn keys(&self) -> Result<Vec<String>, String> {
		use reinhardt_query::prelude::{Alias, Cond, Expr, ExprTrait, Query};

		let now = Utc::now().to_rfc3339();

		// Build SELECT query using reinhardt-query
		// WHERE expire_date IS NULL OR expire_date > ?
		let stmt = Query::select()
			.column(Alias::new("key"))
			.from(Alias::new("settings"))
			.and_where(
				Cond::any()
					.add(Expr::col(Alias::new("expire_date")).is_null())
					.add(Expr::col(Alias::new("expire_date")).gt(Expr::value(&now))),
			)
			.to_owned();

		let sql = self.build_sql(stmt);

		let rows = sqlx::query(&sql)
			.fetch_all(self.pool.as_ref())
			.await
			.map_err(|e| format!("Failed to fetch keys: {}", e))?;

		// Use index-based access for MySQL compatibility (key is first column, index 0)
		// MySQL TEXT columns may be returned as BLOB, so handle both String and Vec<u8>
		let keys: Result<Vec<String>, String> = rows
			.iter()
			.map(|row| {
				if let Ok(s) = row.try_get::<String, _>(0) {
					Ok(s)
				} else if let Ok(bytes) = row.try_get::<Vec<u8>, _>(0) {
					String::from_utf8(bytes)
						.map_err(|e| format!("Invalid UTF-8 in key column: {}", e))
				} else {
					Err("Missing key column".to_string())
				}
			})
			.collect();

		keys
	}
}

/// Convert String errors to DynamicError
#[cfg(feature = "dynamic-database")]
impl From<String> for DynamicError {
	fn from(error: String) -> Self {
		DynamicError::Backend(error)
	}
}

/// DynamicBackend trait implementation for DatabaseBackend
#[cfg(feature = "dynamic-database")]
#[async_trait]
impl DynamicBackend for DatabaseBackend {
	async fn get(&self, key: &str) -> DynamicResult<Option<serde_json::Value>> {
		self.get(key).await.map_err(DynamicError::from)
	}

	async fn set(
		&self,
		key: &str,
		value: &serde_json::Value,
		ttl: Option<u64>,
	) -> DynamicResult<()> {
		self.set(key, value, ttl).await.map_err(DynamicError::from)
	}

	async fn delete(&self, key: &str) -> DynamicResult<()> {
		self.delete(key).await.map_err(DynamicError::from)
	}

	async fn exists(&self, key: &str) -> DynamicResult<bool> {
		self.exists(key).await.map_err(DynamicError::from)
	}

	async fn keys(&self) -> DynamicResult<Vec<String>> {
		self.keys().await.map_err(DynamicError::from)
	}
}

#[cfg(all(test, not(feature = "dynamic-database")))]
mod tests_no_feature {
	use super::*;

	#[tokio::test]
	async fn test_database_backend_disabled() {
		let result = DatabaseBackend::new("sqlite::memory:?mode=rwc&cache=shared").await;
		assert!(result.is_err());
		assert!(result.unwrap_err().contains("Database backend not enabled"));
	}
}

#[cfg(all(test, feature = "dynamic-database"))]
mod tests {
	use super::*;
	use serde_json::json;
	use std::sync::Once;

	static INIT_DRIVERS: Once = Once::new();

	fn init_drivers() {
		INIT_DRIVERS.call_once(|| {
			sqlx::any::install_default_drivers();
		});
	}

	async fn create_test_backend() -> DatabaseBackend {
		init_drivers();
		// Use in-memory SQLite with shared cache mode
		// This allows multiple connections from the pool to share the same in-memory database
		let db_url = "sqlite::memory:?mode=rwc&cache=shared";

		// Create backend with minimal connection pool for tests
		use sqlx::any::AnyPoolOptions;
		let pool = AnyPoolOptions::new()
			.min_connections(1)
			.max_connections(1)
			.connect(db_url)
			.await
			.expect("Failed to connect to test database");

		let backend = DatabaseBackend::from_pool(Arc::new(pool), db_url);
		backend
			.create_table()
			.await
			.expect("Failed to create table");
		backend
	}

	#[tokio::test]
	async fn test_set_and_get_setting() {
		let backend = create_test_backend().await;
		let key = "test_setting_1";
		let value = json!({
			"debug": true,
			"port": 8080,
		});

		// Set setting
		backend
			.set(key, &value, Some(3600))
			.await
			.expect("Failed to set setting");

		// Get setting
		let retrieved = backend.get(key).await.expect("Failed to get setting");

		assert_eq!(retrieved, Some(value));
	}

	#[tokio::test]
	async fn test_setting_exists() {
		let backend = create_test_backend().await;
		let key = "test_setting_2";
		let value = json!({"test": "data"});

		// Setting should not exist initially
		let exists = backend
			.exists(key)
			.await
			.expect("Failed to check existence");
		assert!(!exists);

		// Set setting
		backend
			.set(key, &value, Some(3600))
			.await
			.expect("Failed to set setting");

		// Setting should now exist
		let exists = backend
			.exists(key)
			.await
			.expect("Failed to check existence");
		assert!(exists);
	}

	#[tokio::test]
	async fn test_delete_setting() {
		let backend = create_test_backend().await;
		let key = "test_setting_3";
		let value = json!({"test": "data"});

		// Set setting
		backend
			.set(key, &value, Some(3600))
			.await
			.expect("Failed to set setting");

		// Verify setting exists
		assert!(
			backend
				.exists(key)
				.await
				.expect("Failed to check existence")
		);

		// Delete setting
		backend.delete(key).await.expect("Failed to delete setting");

		// Verify setting no longer exists
		assert!(
			!backend
				.exists(key)
				.await
				.expect("Failed to check existence")
		);
	}

	#[tokio::test]
	async fn test_expired_setting() {
		let backend = create_test_backend().await;
		let key = "test_setting_4";
		let value = json!({"test": "data"});

		// Set setting with 0 second TTL (immediately expired)
		backend
			.set(key, &value, Some(0))
			.await
			.expect("Failed to set setting");

		// Try to get expired setting
		let retrieved = backend.get(key).await.expect("Failed to get setting");

		assert_eq!(retrieved, None);
	}

	#[tokio::test]
	async fn test_cleanup_expired() {
		let backend = create_test_backend().await;

		// Set some expired settings
		for i in 0..5 {
			let key = format!("expired_{}", i);
			backend
				.set(&key, &json!({ "test": i }), Some(0))
				.await
				.expect("Failed to set setting");
		}

		// Set some active settings
		for i in 0..3 {
			let key = format!("active_{}", i);
			backend
				.set(&key, &json!({ "test": i }), Some(3600))
				.await
				.expect("Failed to set setting");
		}

		// Clean up expired settings
		let deleted = backend.cleanup_expired().await.expect("Failed to cleanup");

		assert_eq!(deleted, 5);

		// Verify active settings still exist
		for i in 0..3 {
			let key = format!("active_{}", i);
			assert!(
				backend
					.exists(&key)
					.await
					.expect("Failed to check existence")
			);
		}
	}

	#[tokio::test]
	async fn test_setting_without_ttl() {
		let backend = create_test_backend().await;
		let key = "permanent_setting";
		let value = json!({"permanent": true});

		// Set setting without TTL
		backend
			.set(key, &value, None)
			.await
			.expect("Failed to set setting");

		// Get setting
		let retrieved = backend.get(key).await.expect("Failed to get setting");
		assert_eq!(retrieved, Some(value));

		// Verify it exists
		assert!(
			backend
				.exists(key)
				.await
				.expect("Failed to check existence")
		);
	}

	#[tokio::test]
	async fn test_overwrite_existing_setting() {
		let backend = create_test_backend().await;
		let key = "overwrite_test";

		// Set initial value
		backend
			.set(key, &json!({"value": 1}), None)
			.await
			.expect("Failed to set setting");

		// Overwrite with new value
		backend
			.set(key, &json!({"value": 2}), None)
			.await
			.expect("Failed to set setting");

		// Get updated value
		let retrieved = backend.get(key).await.expect("Failed to get setting");
		assert_eq!(retrieved, Some(json!({"value": 2})));
	}

	#[tokio::test]
	async fn test_keys_retrieval() {
		let backend = create_test_backend().await;

		// Set multiple settings
		backend
			.set("key1", &json!("value1"), None)
			.await
			.expect("Failed to set key1");
		backend
			.set("key2", &json!("value2"), None)
			.await
			.expect("Failed to set key2");
		backend
			.set("key3", &json!("value3"), None)
			.await
			.expect("Failed to set key3");

		// Get all keys
		let keys = backend.keys().await.expect("Failed to get keys");

		// Verify all keys are present
		assert_eq!(keys.len(), 3);
		assert!(keys.contains(&"key1".to_string()));
		assert!(keys.contains(&"key2".to_string()));
		assert!(keys.contains(&"key3".to_string()));
	}

	#[tokio::test]
	async fn test_keys_excludes_expired() {
		let backend = create_test_backend().await;

		// Set active settings
		backend
			.set("active1", &json!("value1"), Some(3600))
			.await
			.expect("Failed to set active1");
		backend
			.set("active2", &json!("value2"), None)
			.await
			.expect("Failed to set active2");

		// Set expired settings
		backend
			.set("expired1", &json!("value3"), Some(0))
			.await
			.expect("Failed to set expired1");
		backend
			.set("expired2", &json!("value4"), Some(0))
			.await
			.expect("Failed to set expired2");

		// Get keys (should only include active settings)
		let keys = backend.keys().await.expect("Failed to get keys");

		assert_eq!(keys.len(), 2);
		assert!(keys.contains(&"active1".to_string()));
		assert!(keys.contains(&"active2".to_string()));
		assert!(!keys.contains(&"expired1".to_string()));
		assert!(!keys.contains(&"expired2".to_string()));
	}
}