reinhardt-db 0.1.2

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
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
//! Zero-downtime migration support
//!
//! This module provides strategies for performing database migrations with minimal or no downtime,
//! inspired by patterns from Braintree, GitHub, and Stripe's migration practices.
//!
//! # Example
//!
//! ```rust
//! use reinhardt_db::migrations::zero_downtime::{ZeroDowntimeMigration, Strategy};
//! use reinhardt_db::migrations::Migration;
//!
//! // Create a migration with zero-downtime strategy
//! let migration = Migration::new("0001_add_column", "myapp");
//! let zd_migration = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
//!
//! // Get the phases
//! let phases = zd_migration.get_phases().unwrap();
//! assert!(phases.len() > 0);
//! ```

use super::{Migration, Operation, Result};

/// Zero-downtime migration strategy
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::zero_downtime::Strategy;
///
/// let strategy = Strategy::ExpandContractPattern;
/// assert_eq!(strategy.name(), "Expand-Contract Pattern");
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Strategy {
	/// Expand-Contract Pattern: Add new schema, dual-write, migrate data, remove old schema
	ExpandContractPattern,
	/// Blue-Green Deployment: Run two versions simultaneously
	BlueGreenDeployment,
	/// Rolling Deployment: Gradual rollout with backward compatibility
	RollingDeployment,
	/// Shadow Mode: Test new schema alongside old one
	ShadowMode,
}

impl Strategy {
	/// Get strategy name
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::Strategy;
	///
	/// let strategy = Strategy::ExpandContractPattern;
	/// assert_eq!(strategy.name(), "Expand-Contract Pattern");
	/// ```
	pub fn name(&self) -> &str {
		match self {
			Strategy::ExpandContractPattern => "Expand-Contract Pattern",
			Strategy::BlueGreenDeployment => "Blue-Green Deployment",
			Strategy::RollingDeployment => "Rolling Deployment",
			Strategy::ShadowMode => "Shadow Mode",
		}
	}

	/// Get strategy description
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::Strategy;
	///
	/// let strategy = Strategy::ExpandContractPattern;
	/// let desc = strategy.description();
	/// assert!(desc.contains("Expand"));
	/// ```
	pub fn description(&self) -> &str {
		match self {
			Strategy::ExpandContractPattern => {
				"Expand schema first (add columns/tables), then contract (remove old schema)"
			}
			Strategy::BlueGreenDeployment => {
				"Deploy new version alongside old, switch traffic when ready"
			}
			Strategy::RollingDeployment => {
				"Gradually deploy to servers while maintaining backward compatibility"
			}
			Strategy::ShadowMode => "Test new schema in shadow mode before switching over",
		}
	}
}

/// Migration phase for zero-downtime deployment
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::zero_downtime::MigrationPhase;
/// use reinhardt_db::migrations::Migration;
///
/// let migration = Migration::new("0001_initial", "myapp");
/// let phase = MigrationPhase::new(1, "Expand", migration);
/// assert_eq!(phase.phase_number, 1);
/// ```
#[derive(Debug, Clone)]
pub struct MigrationPhase {
	/// Phase number (1-based)
	pub phase_number: usize,
	/// Phase description
	pub description: String,
	/// Migration for this phase
	pub migration: Migration,
	/// Whether this phase requires deployment
	pub requires_deployment: bool,
}

impl MigrationPhase {
	/// Create a new migration phase
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::MigrationPhase;
	/// use reinhardt_db::migrations::Migration;
	///
	/// let migration = Migration::new("0001_expand", "myapp");
	/// let phase = MigrationPhase::new(1, "Expand schema", migration);
	/// ```
	pub fn new(phase_number: usize, description: impl Into<String>, migration: Migration) -> Self {
		Self {
			phase_number,
			description: description.into(),
			migration,
			requires_deployment: false,
		}
	}

	/// Set whether this phase requires code deployment
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::MigrationPhase;
	/// use reinhardt_db::migrations::Migration;
	///
	/// let migration = Migration::new("0001_expand", "myapp");
	/// let phase = MigrationPhase::new(1, "Expand", migration)
	///     .requires_deployment(true);
	/// assert!(phase.requires_deployment);
	/// ```
	pub fn requires_deployment(mut self, requires: bool) -> Self {
		self.requires_deployment = requires;
		self
	}
}

/// Zero-downtime migration
///
/// Wraps a migration with a strategy for zero-downtime deployment.
///
/// # Example
///
/// ```rust
/// use reinhardt_db::migrations::zero_downtime::{ZeroDowntimeMigration, Strategy};
/// use reinhardt_db::migrations::Migration;
///
/// let migration = Migration::new("0001_add_field", "myapp");
/// let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
/// ```
pub struct ZeroDowntimeMigration {
	migration: Migration,
	strategy: Strategy,
}

impl ZeroDowntimeMigration {
	/// Create a new zero-downtime migration
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::{ZeroDowntimeMigration, Strategy};
	/// use reinhardt_db::migrations::Migration;
	///
	/// let migration = Migration::new("0001_rename_column", "myapp");
	/// let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
	/// ```
	pub fn new(migration: Migration, strategy: Strategy) -> Self {
		Self {
			migration,
			strategy,
		}
	}

	/// Get the migration phases
	///
	/// # Example
	///
	/// ```rust
	/// use reinhardt_db::migrations::zero_downtime::{ZeroDowntimeMigration, Strategy};
	/// use reinhardt_db::migrations::Migration;
	///
	/// let migration = Migration::new("0001_add_column", "myapp");
	/// let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
	/// let phases = zd.get_phases().unwrap();
	/// ```
	pub fn get_phases(&self) -> Result<Vec<MigrationPhase>> {
		match self.strategy {
			Strategy::ExpandContractPattern => self.expand_contract_phases(),
			Strategy::BlueGreenDeployment => self.blue_green_phases(),
			Strategy::RollingDeployment => self.rolling_phases(),
			Strategy::ShadowMode => self.shadow_mode_phases(),
		}
	}

	/// Generate phases for Expand-Contract pattern
	fn expand_contract_phases(&self) -> Result<Vec<MigrationPhase>> {
		let mut phases = Vec::new();

		// Expand - Add new schema elements
		let mut expand_migration = Migration::new(
			format!("{}_expand", self.migration.name),
			self.migration.app_label.clone(),
		);
		expand_migration.operations = self.extract_expand_operations();
		expand_migration.dependencies = self.migration.dependencies.clone();

		phases.push(
			MigrationPhase::new(1, "Expand: Add new schema elements", expand_migration)
				.requires_deployment(false),
		);

		// Dual-write - Application writes to both old and new schema
		phases.push(
			MigrationPhase::new(
				2,
				"Deploy code with dual-write support",
				Migration::new(
					format!("{}_dual_write", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		// Migrate data from old to new schema
		let mut migrate_migration = Migration::new(
			format!("{}_migrate_data", self.migration.name),
			self.migration.app_label.clone(),
		);
		migrate_migration.operations = self.extract_data_migration_operations();

		phases.push(
			MigrationPhase::new(3, "Migrate data to new schema", migrate_migration)
				.requires_deployment(false),
		);

		// Switch reads to new schema
		phases.push(
			MigrationPhase::new(
				4,
				"Deploy code reading from new schema",
				Migration::new(
					format!("{}_switch_reads", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		// Contract - Remove old schema elements
		let mut contract_migration = Migration::new(
			format!("{}_contract", self.migration.name),
			self.migration.app_label.clone(),
		);
		contract_migration.operations = self.extract_contract_operations();

		phases.push(
			MigrationPhase::new(
				5,
				"Contract: Remove old schema elements",
				contract_migration,
			)
			.requires_deployment(false),
		);

		Ok(phases)
	}

	/// Generate phases for Blue-Green deployment
	fn blue_green_phases(&self) -> Result<Vec<MigrationPhase>> {
		let mut phases = Vec::new();

		// Setup green environment
		let mut green_migration = Migration::new(
			format!("{}_green", self.migration.name),
			self.migration.app_label.clone(),
		);
		green_migration.operations = self.migration.operations.clone();

		phases.push(
			MigrationPhase::new(
				1,
				"Setup green environment with new schema",
				green_migration,
			)
			.requires_deployment(false),
		);

		// Deploy application to green
		phases.push(
			MigrationPhase::new(
				2,
				"Deploy application to green environment",
				Migration::new(
					format!("{}_deploy_green", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		// Switch traffic to green
		phases.push(
			MigrationPhase::new(
				3,
				"Switch traffic to green environment",
				Migration::new(
					format!("{}_switch", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(false),
		);

		Ok(phases)
	}

	/// Generate phases for Rolling deployment
	fn rolling_phases(&self) -> Result<Vec<MigrationPhase>> {
		let mut phases = Vec::new();

		// Deploy backward-compatible schema changes
		let mut compat_migration = Migration::new(
			format!("{}_compatible", self.migration.name),
			self.migration.app_label.clone(),
		);
		compat_migration.operations = self.migration.operations.clone();

		phases.push(
			MigrationPhase::new(1, "Deploy backward-compatible changes", compat_migration)
				.requires_deployment(false),
		);

		// Rolling application deployment
		phases.push(
			MigrationPhase::new(
				2,
				"Deploy application updates gradually",
				Migration::new(
					format!("{}_rolling", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		Ok(phases)
	}

	/// Generate phases for Shadow mode
	fn shadow_mode_phases(&self) -> Result<Vec<MigrationPhase>> {
		let mut phases = Vec::new();

		// Create shadow schema
		let mut shadow_migration = Migration::new(
			format!("{}_shadow", self.migration.name),
			self.migration.app_label.clone(),
		);
		shadow_migration.operations = self.migration.operations.clone();

		phases.push(
			MigrationPhase::new(1, "Create shadow schema for testing", shadow_migration)
				.requires_deployment(false),
		);

		// Deploy with shadow writes
		phases.push(
			MigrationPhase::new(
				2,
				"Deploy code with shadow write support",
				Migration::new(
					format!("{}_shadow_writes", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		// Validate shadow data
		phases.push(
			MigrationPhase::new(
				3,
				"Validate shadow data matches production",
				Migration::new(
					format!("{}_validate", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(false),
		);

		// Switch to new schema
		phases.push(
			MigrationPhase::new(
				4,
				"Switch to new schema",
				Migration::new(
					format!("{}_switch", self.migration.name),
					self.migration.app_label.clone(),
				),
			)
			.requires_deployment(true),
		);

		Ok(phases)
	}

	/// Extract operations that expand the schema (add new elements)
	fn extract_expand_operations(&self) -> Vec<Operation> {
		self.migration
			.operations
			.iter()
			.filter(|op| {
				matches!(
					op,
					Operation::CreateTable { .. } | Operation::AddColumn { .. }
				)
			})
			.cloned()
			.collect()
	}

	/// Extract operations that contract the schema (remove old elements)
	fn extract_contract_operations(&self) -> Vec<Operation> {
		self.migration
			.operations
			.iter()
			.filter(|op| {
				matches!(
					op,
					Operation::DropTable { .. } | Operation::DropColumn { .. }
				)
			})
			.cloned()
			.collect()
	}

	/// Extract data migration operations
	fn extract_data_migration_operations(&self) -> Vec<Operation> {
		self.migration
			.operations
			.iter()
			.filter(|op| matches!(op, Operation::RunSQL { .. }))
			.cloned()
			.collect()
	}
}

#[cfg(test)]
mod tests {
	use super::*;
	use crate::migrations::{ColumnDefinition, FieldType};

	#[test]
	fn test_strategy_name() {
		assert_eq!(
			Strategy::ExpandContractPattern.name(),
			"Expand-Contract Pattern"
		);
		assert_eq!(
			Strategy::BlueGreenDeployment.name(),
			"Blue-Green Deployment"
		);
	}

	#[test]
	fn test_strategy_description() {
		let desc = Strategy::ExpandContractPattern.description();
		assert!(desc.contains("Expand"));
		assert!(desc.contains("contract"));
	}

	#[test]
	fn test_migration_phase_creation() {
		let migration = Migration::new("0001_test", "myapp");
		let phase = MigrationPhase::new(1, "Test phase", migration);

		assert_eq!(phase.phase_number, 1);
		assert_eq!(phase.description, "Test phase");
		assert!(!phase.requires_deployment);
	}

	#[test]
	fn test_phase_requires_deployment() {
		let migration = Migration::new("0001_test", "myapp");
		let phase = MigrationPhase::new(1, "Deploy", migration).requires_deployment(true);

		assert!(phase.requires_deployment);
	}

	#[test]
	fn test_zero_downtime_expand_contract() {
		let migration =
			Migration::new("0001_add_column", "myapp").add_operation(Operation::AddColumn {
				table: "users".to_string(),
				column: ColumnDefinition::new("new_field", FieldType::VarChar(100)),
				mysql_options: None,
			});

		let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
		let phases = zd.get_phases().unwrap();

		assert_eq!(phases.len(), 5);
		assert_eq!(phases[0].description, "Expand: Add new schema elements");
	}

	#[test]
	fn test_zero_downtime_blue_green() {
		let migration = Migration::new("0001_schema_change", "myapp");
		let zd = ZeroDowntimeMigration::new(migration, Strategy::BlueGreenDeployment);
		let phases = zd.get_phases().unwrap();

		assert_eq!(phases.len(), 3);
		assert!(phases[0].description.contains("green environment"));
	}

	#[test]
	fn test_zero_downtime_rolling() {
		let migration = Migration::new("0001_update", "myapp");
		let zd = ZeroDowntimeMigration::new(migration, Strategy::RollingDeployment);
		let phases = zd.get_phases().unwrap();

		assert_eq!(phases.len(), 2);
		assert!(phases[0].description.contains("backward-compatible"));
	}

	#[test]
	fn test_zero_downtime_shadow() {
		let migration = Migration::new("0001_test_new_schema", "myapp");
		let zd = ZeroDowntimeMigration::new(migration, Strategy::ShadowMode);
		let phases = zd.get_phases().unwrap();

		assert_eq!(phases.len(), 4);
		assert!(phases[0].description.contains("shadow"));
	}

	#[test]
	fn test_extract_expand_operations() {
		let migration = Migration::new("0001_mixed", "myapp")
			.add_operation(Operation::CreateTable {
				name: "new_table".to_string(),
				columns: vec![],
				constraints: vec![],
				without_rowid: None,
				partition: None,
				interleave_in_parent: None,
			})
			.add_operation(Operation::AddColumn {
				table: "users".to_string(),
				column: ColumnDefinition::new("field", FieldType::VarChar(100)),
				mysql_options: None,
			})
			.add_operation(Operation::DropColumn {
				table: "users".to_string(),
				column: "old_field".to_string(),
			});

		let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
		let expand_ops = zd.extract_expand_operations();

		assert_eq!(expand_ops.len(), 2);
	}

	#[test]
	fn test_extract_contract_operations() {
		let migration = Migration::new("0001_mixed", "myapp")
			.add_operation(Operation::AddColumn {
				table: "users".to_string(),
				column: ColumnDefinition::new("field", FieldType::VarChar(100)),
				mysql_options: None,
			})
			.add_operation(Operation::DropColumn {
				table: "users".to_string(),
				column: "old_field".to_string(),
			})
			.add_operation(Operation::DropTable {
				name: "old_table".to_string(),
			});

		let zd = ZeroDowntimeMigration::new(migration, Strategy::ExpandContractPattern);
		let contract_ops = zd.extract_contract_operations();

		assert_eq!(contract_ops.len(), 2);
	}
}