reinhardt-db 0.1.1

Django-style database layer for Reinhardt framework
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
//! PostgreSQL-specific migration operations
//!
//! This module provides PostgreSQL-specific operations inspired by Django's
//! `django/contrib/postgres/operations.py`. These operations allow you to:
//!
//! - Create and manage PostgreSQL extensions
//! - Use PostgreSQL-specific index types (GIN, GiST, BRIN, etc.)
//! - Work with PostgreSQL functions and triggers
//!
//! # Example
//!
//! ```rust
//! use reinhardt_db::migrations::operations::postgres::{CreateExtension, CreateCollation};
//!
//! // Create the hstore extension
//! let ext = CreateExtension::new("hstore");
//!
//! // Create a custom collation
//! let collation = CreateCollation::new("german", "de_DE");
//! ```

use crate::backends::schema::BaseDatabaseSchemaEditor;
use crate::migrations::ProjectState;
use pg_escape::quote_literal;
use serde::{Deserialize, Serialize};

/// Create a PostgreSQL extension
///
/// PostgreSQL extensions add additional functionality to the database.
/// Common extensions include hstore, postgis, pg_trgm, and many others.
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::operations::postgres::CreateExtension;
/// use reinhardt_db::migrations::ProjectState;
///
/// let mut state = ProjectState::new();
/// let ext = CreateExtension::new("hstore");
///
/// // Extensions don't modify project state
/// ext.state_forwards("myapp", &mut state);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateExtension {
	/// The name.
	pub name: String,
	/// The schema.
	pub schema: Option<String>,
	/// The version.
	pub version: Option<String>,
}

impl CreateExtension {
	/// Create a new CreateExtension operation
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::operations::postgres::CreateExtension;
	///
	/// let ext = CreateExtension::new("hstore");
	/// assert_eq!(ext.name, "hstore");
	/// assert!(ext.schema.is_none());
	/// ```
	pub fn new(name: impl Into<String>) -> Self {
		Self {
			name: name.into(),
			schema: None,
			version: None,
		}
	}

	/// Set the schema where the extension should be created
	pub fn with_schema(mut self, schema: impl Into<String>) -> Self {
		self.schema = Some(schema.into());
		self
	}

	/// Set a specific version of the extension
	pub fn with_version(mut self, version: impl Into<String>) -> Self {
		self.version = Some(version.into());
		self
	}

	/// Apply to project state (extensions don't modify state)
	pub fn state_forwards(&self, _app_label: &str, _state: &mut ProjectState) {
		// Extensions are database-level and don't affect the application schema
	}

	/// Generate SQL using schema editor
	///
	/// # Example
	///
	/// ```rust,no_run
	/// use reinhardt_db::migrations::operations::postgres::CreateExtension;
	/// use reinhardt_db::backends::schema::factory::{SchemaEditorFactory, DatabaseType};
	///
	/// let ext = CreateExtension::new("hstore").with_schema("public");
	/// let factory = SchemaEditorFactory::new();
	/// let editor = factory.create_for_database(DatabaseType::PostgreSQL);
	///
	/// let sql = ext.database_forwards(editor.as_ref());
	/// assert_eq!(sql.len(), 1);
	/// assert!(sql[0].contains("CREATE EXTENSION"));
	/// assert!(sql[0].contains("hstore"));
	/// ```
	pub fn database_forwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		let mut parts = vec!["CREATE EXTENSION IF NOT EXISTS".to_string()];
		// Always use double quotes for PostgreSQL identifier safety
		parts.push(format!("\"{}\"", self.name));

		if let Some(ref schema) = self.schema {
			parts.push("SCHEMA".to_string());
			parts.push(format!("\"{}\"", schema));
		}

		if let Some(ref version) = self.version {
			parts.push("VERSION".to_string());
			parts.push(quote_literal(version));
		}

		vec![format!("{};", parts.join(" "))]
	}

	/// Generate reverse SQL
	///
	/// # Example
	///
	/// ```rust,no_run
	/// use reinhardt_db::migrations::operations::postgres::CreateExtension;
	/// use reinhardt_db::backends::schema::factory::{SchemaEditorFactory, DatabaseType};
	///
	/// let ext = CreateExtension::new("hstore");
	/// let factory = SchemaEditorFactory::new();
	/// let editor = factory.create_for_database(DatabaseType::PostgreSQL);
	///
	/// let sql = ext.database_backwards(editor.as_ref());
	/// assert_eq!(sql.len(), 1);
	/// assert!(sql[0].contains("DROP EXTENSION"));
	/// ```
	pub fn database_backwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		vec![format!("DROP EXTENSION IF EXISTS \"{}\";", self.name)]
	}
}

/// Drop a PostgreSQL extension
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::operations::postgres::DropExtension;
///
/// let drop = DropExtension::new("hstore");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DropExtension {
	/// The name.
	pub name: String,
}

impl DropExtension {
	/// Create a new DropExtension operation
	pub fn new(name: impl Into<String>) -> Self {
		Self { name: name.into() }
	}

	/// Apply to project state (extensions don't modify state)
	pub fn state_forwards(&self, _app_label: &str, _state: &mut ProjectState) {}

	/// Generate SQL
	///
	/// # Example
	///
	/// ```rust,no_run
	/// use reinhardt_db::migrations::operations::postgres::DropExtension;
	/// use reinhardt_db::backends::schema::factory::{SchemaEditorFactory, DatabaseType};
	///
	/// let drop = DropExtension::new("hstore");
	/// let factory = SchemaEditorFactory::new();
	/// let editor = factory.create_for_database(DatabaseType::PostgreSQL);
	///
	/// let sql = drop.database_forwards(editor.as_ref());
	/// assert_eq!(sql.len(), 1);
	/// assert!(sql[0].contains("DROP EXTENSION"));
	/// ```
	pub fn database_forwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		vec![format!("DROP EXTENSION IF EXISTS \"{}\";", self.name)]
	}

	/// Generate reverse SQL (recreate extension)
	pub fn database_backwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		vec![format!("CREATE EXTENSION IF NOT EXISTS \"{}\";", self.name)]
	}
}

/// Create a PostgreSQL collation
///
/// Collations define how text is sorted and compared in the database.
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::operations::postgres::CreateCollation;
///
/// let collation = CreateCollation::new("german", "de_DE");
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateCollation {
	/// The name.
	pub name: String,
	/// The locale.
	pub locale: String,
	/// The provider.
	pub provider: Option<String>,
}

impl CreateCollation {
	/// Create a new collation
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::operations::postgres::CreateCollation;
	///
	/// let collation = CreateCollation::new("german", "de_DE");
	/// assert_eq!(collation.name, "german");
	/// assert_eq!(collation.locale, "de_DE");
	/// ```
	pub fn new(name: impl Into<String>, locale: impl Into<String>) -> Self {
		Self {
			name: name.into(),
			locale: locale.into(),
			provider: None,
		}
	}

	/// Set the collation provider (icu or libc)
	pub fn with_provider(mut self, provider: impl Into<String>) -> Self {
		self.provider = Some(provider.into());
		self
	}

	/// Apply to project state
	pub fn state_forwards(&self, _app_label: &str, _state: &mut ProjectState) {}

	/// Generate SQL
	///
	/// # Example
	///
	/// ```rust,no_run
	/// use reinhardt_db::migrations::operations::postgres::CreateCollation;
	/// use reinhardt_db::backends::schema::factory::{SchemaEditorFactory, DatabaseType};
	///
	/// let collation = CreateCollation::new("german", "de_DE");
	/// let factory = SchemaEditorFactory::new();
	/// let editor = factory.create_for_database(DatabaseType::PostgreSQL);
	///
	/// let sql = collation.database_forwards(editor.as_ref());
	/// assert_eq!(sql.len(), 1);
	/// assert!(sql[0].contains("CREATE COLLATION"));
	/// assert!(sql[0].contains("german"));
	/// ```
	pub fn database_forwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		// Always use double quotes for PostgreSQL identifier safety
		let mut sql = format!(
			"CREATE COLLATION IF NOT EXISTS \"{}\" (LOCALE = {}",
			self.name,
			quote_literal(&self.locale)
		);

		if let Some(ref provider) = self.provider {
			sql.push_str(&format!(", PROVIDER = {}", quote_literal(provider)));
		}

		sql.push_str(");");
		vec![sql]
	}

	/// Generate reverse SQL
	pub fn database_backwards(&self, _schema_editor: &dyn BaseDatabaseSchemaEditor) -> Vec<String> {
		vec![format!("DROP COLLATION IF EXISTS \"{}\";", self.name)]
	}
}

/// Commonly used PostgreSQL extensions
pub mod extensions {
	use super::CreateExtension;

	/// Create the hstore extension for key-value storage
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::operations::postgres::extensions::hstore;
	///
	/// let ext = hstore();
	/// assert_eq!(ext.name, "hstore");
	/// ```
	pub fn hstore() -> CreateExtension {
		CreateExtension::new("hstore")
	}

	/// Create the pg_trgm extension for trigram matching
	pub fn pg_trgm() -> CreateExtension {
		CreateExtension::new("pg_trgm")
	}

	/// Create the uuid-ossp extension for UUID generation
	pub fn uuid_ossp() -> CreateExtension {
		CreateExtension::new("uuid-ossp")
	}

	/// Create the postgis extension for geographic data
	pub fn postgis() -> CreateExtension {
		CreateExtension::new("postgis")
	}

	/// Create the btree_gin extension for B-tree GIN indexes
	pub fn btree_gin() -> CreateExtension {
		CreateExtension::new("btree_gin")
	}

	/// Create the btree_gist extension for B-tree GiST indexes
	pub fn btree_gist() -> CreateExtension {
		CreateExtension::new("btree_gist")
	}

	/// Create the citext extension for case-insensitive text
	pub fn citext() -> CreateExtension {
		CreateExtension::new("citext")
	}

	/// Create the unaccent extension for removing accents
	pub fn unaccent() -> CreateExtension {
		CreateExtension::new("unaccent")
	}
}

// MigrationOperation trait implementation for Django-style naming
use crate::migrations::operation_trait::MigrationOperation;

impl MigrationOperation for CreateExtension {
	fn migration_name_fragment(&self) -> Option<String> {
		Some(format!("create_extension_{}", self.name.to_lowercase()))
	}

	fn describe(&self) -> String {
		format!("Create PostgreSQL extension {}", self.name)
	}
}

impl MigrationOperation for DropExtension {
	fn migration_name_fragment(&self) -> Option<String> {
		Some(format!("drop_extension_{}", self.name.to_lowercase()))
	}

	fn describe(&self) -> String {
		format!("Drop PostgreSQL extension {}", self.name)
	}
}

impl MigrationOperation for CreateCollation {
	fn migration_name_fragment(&self) -> Option<String> {
		Some(format!("create_collation_{}", self.name.to_lowercase()))
	}

	fn describe(&self) -> String {
		format!("Create collation {}", self.name)
	}
}

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

	#[test]
	fn test_create_extension_basic() {
		let ext = CreateExtension::new("hstore");
		assert_eq!(ext.name, "hstore");
		assert!(ext.schema.is_none());
		assert!(ext.version.is_none());
	}

	#[test]
	fn test_create_extension_with_schema() {
		let ext = CreateExtension::new("hstore").with_schema("public");
		assert_eq!(ext.name, "hstore");
		assert_eq!(ext.schema, Some("public".to_string()));
	}

	#[test]
	fn test_create_extension_with_version() {
		let ext = CreateExtension::new("postgis").with_version("3.0.0");
		assert_eq!(ext.version, Some("3.0.0".to_string()));
	}

	#[cfg(feature = "postgres")]
	#[test]
	fn test_create_extension_database_forwards() {
		use crate::backends::schema::test_utils::MockSchemaEditor;

		let ext = CreateExtension::new("hstore");
		let editor = MockSchemaEditor::new();

		let sql = ext.database_forwards(&editor);
		assert_eq!(sql.len(), 1);
		assert!(sql[0].contains("CREATE EXTENSION IF NOT EXISTS"));
		assert!(sql[0].contains("\"hstore\""));
	}

	#[cfg(feature = "postgres")]
	#[test]
	fn test_create_extension_with_schema_sql() {
		use crate::backends::schema::test_utils::MockSchemaEditor;

		let ext = CreateExtension::new("hstore").with_schema("public");
		let editor = MockSchemaEditor::new();

		let sql = ext.database_forwards(&editor);
		assert!(sql[0].contains("SCHEMA"));
		assert!(sql[0].contains("\"public\""));
	}

	#[cfg(feature = "postgres")]
	#[test]
	fn test_drop_extension() {
		use crate::backends::schema::test_utils::MockSchemaEditor;

		let drop = DropExtension::new("hstore");
		let editor = MockSchemaEditor::new();

		let sql = drop.database_forwards(&editor);
		assert_eq!(sql.len(), 1);
		assert!(sql[0].contains("DROP EXTENSION IF EXISTS"));
		assert!(sql[0].contains("\"hstore\""));
	}

	#[cfg(feature = "postgres")]
	#[test]
	fn test_create_collation() {
		use crate::backends::schema::test_utils::MockSchemaEditor;

		let collation = CreateCollation::new("german", "de_DE");
		let editor = MockSchemaEditor::new();

		let sql = collation.database_forwards(&editor);
		assert_eq!(sql.len(), 1);
		assert!(sql[0].contains("CREATE COLLATION IF NOT EXISTS"));
		assert!(sql[0].contains("\"german\""));
		assert!(sql[0].contains("de_DE"));
	}

	#[test]
	fn test_extension_helpers() {
		let hstore = extensions::hstore();
		assert_eq!(hstore.name, "hstore");

		let pg_trgm = extensions::pg_trgm();
		assert_eq!(pg_trgm.name, "pg_trgm");

		let postgis = extensions::postgis();
		assert_eq!(postgis.name, "postgis");
	}
}