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
//! Migration numbering system
//!
//! Provides app-specific sequential numbering for migrations (0001, 0002, 0003, ...).
use once_cell::sync::Lazy;
use std::collections::HashMap;
use std::path::Path;
use std::sync::{Arc, RwLock};
/// Global cache for migration numbering
///
/// Key format: "{migrations_dir}:{app_label}"
/// Value: highest migration number for that app
static NUMBERING_CACHE: Lazy<Arc<RwLock<HashMap<String, u32>>>> =
Lazy::new(|| Arc::new(RwLock::new(HashMap::new())));
/// Migration numbering system
pub struct MigrationNumbering;
impl MigrationNumbering {
/// Get next migration number for an app (cached version)
///
/// Uses a global cache to avoid repeated filesystem scans.
/// Thread-safe using RwLock.
///
/// # Arguments
///
/// * `migrations_dir` - Path to the migrations directory (e.g., `migrations/`)
/// * `app_label` - App label (e.g., `"myapp"`)
///
/// # Returns
///
/// Next migration number as 4-digit zero-padded string (e.g., `"0001"`, `"0002"`)
///
/// # Examples
///
/// ```rust,no_run
/// use reinhardt_db::migrations::MigrationNumbering;
/// use std::path::Path;
///
/// let next_num = MigrationNumbering::next_number_cached(
/// Path::new("migrations"),
/// "myapp"
/// );
/// assert_eq!(next_num, "0001"); // First migration
/// ```
pub fn next_number_cached(migrations_dir: &Path, app_label: &str) -> String {
let cache_key = format!("{}:{}", migrations_dir.display(), app_label);
// Acquire write lock once for the entire read-modify-write operation
let mut cache = NUMBERING_CACHE.write().unwrap_or_else(|e| e.into_inner());
if let Some(cached_num) = cache.get_mut(&cache_key) {
let next = *cached_num + 1;
*cached_num = next;
Self::format_number(next)
} else {
// Cache miss - scan filesystem
let highest = Self::get_highest_number(migrations_dir, app_label);
// Store highest + 1 so the next cache hit returns the correct next number
cache.insert(cache_key, highest + 1);
Self::format_number(highest + 1)
}
}
/// Get next migration number for an app (non-cached version)
///
/// Scans existing migration files in the app's migrations directory
/// and returns the next sequential number (4-digit zero-padded).
///
/// # Arguments
///
/// * `migrations_dir` - Path to the migrations directory (e.g., `migrations/`)
/// * `app_label` - App label (e.g., `"myapp"`)
///
/// # Returns
///
/// Next migration number as 4-digit zero-padded string (e.g., `"0001"`, `"0002"`)
///
/// # Examples
///
/// ```rust,no_run
/// use reinhardt_db::migrations::MigrationNumbering;
/// use std::path::Path;
///
/// let next_num = MigrationNumbering::next_number(
/// Path::new("migrations"),
/// "myapp"
/// );
/// assert_eq!(next_num, "0001"); // First migration
/// ```
pub fn next_number(migrations_dir: &Path, app_label: &str) -> String {
let highest = Self::get_highest_number(migrations_dir, app_label);
Self::format_number(highest + 1)
}
/// Invalidate the global cache
///
/// Call this when migrations are manually deleted or modified outside
/// of the normal flow to ensure cache consistency.
///
/// # Examples
///
/// ```rust,no_run
/// use reinhardt_db::migrations::MigrationNumbering;
///
/// // After manually deleting migrations
/// MigrationNumbering::invalidate_cache();
/// ```
pub fn invalidate_cache() {
NUMBERING_CACHE
.write()
.unwrap_or_else(|e| e.into_inner())
.clear();
}
/// Format a migration number as a zero-padded string
///
/// Pads to at least 4 digits, but preserves larger numbers as-is.
fn format_number(num: u32) -> String {
if num <= 9999 {
format!("{:04}", num)
} else {
format!("{}", num)
}
}
/// Get highest existing migration number for an app
///
/// Scans migration files matching the pattern `NNNN_*.rs` and returns
/// the highest number found, or 0 if no migrations exist.
///
/// # File Name Pattern
///
/// Expects migration files in format: `{app_label}/NNNN_*.rs`
/// - `NNNN`: 4-digit zero-padded number
/// - `*`: migration name (e.g., `initial`, `add_user_email`)
///
/// # Examples
///
/// ```rust,no_run
/// # use reinhardt_db::migrations::MigrationNumbering;
/// # use std::path::Path;
/// // Given files:
/// // migrations/myapp/0001_initial.rs
/// // migrations/myapp/0002_add_field.rs
/// // migrations/myapp/0003_remove_field.rs
///
/// let highest = MigrationNumbering::get_highest_number(
/// Path::new("migrations"),
/// "myapp"
/// );
/// assert_eq!(highest, 3);
/// ```
pub fn get_highest_number(migrations_dir: &Path, app_label: &str) -> u32 {
let app_migrations_dir = migrations_dir.join(app_label);
// If directory doesn't exist, this is the first migration
if !app_migrations_dir.exists() {
return 0;
}
let mut highest = 0;
// Scan for migration files matching NNNN_*.rs
if let Ok(entries) = std::fs::read_dir(&app_migrations_dir) {
for entry in entries.flatten() {
let path = entry.path();
// Only process .rs files
if path.extension().and_then(|s| s.to_str()) != Some("rs") {
continue;
}
// Extract filename
if let Some(filename) = path.file_stem().and_then(|s| s.to_str()) {
// Parse all leading digits dynamically (supports 4+ digit prefixes, #1334)
let prefix: String = filename
.chars()
.take_while(|c| c.is_ascii_digit())
.collect();
if !prefix.is_empty()
&& let Ok(num) = prefix.parse::<u32>()
{
highest = highest.max(num);
}
}
}
}
highest
}
/// Get all app migration numbers
///
/// Scans the migrations directory and returns a map of app labels
/// to their highest migration numbers.
///
/// # Examples
///
/// ```rust,no_run
/// # use reinhardt_db::migrations::MigrationNumbering;
/// # use std::path::Path;
/// // Given structure:
/// // migrations/
/// // myapp/0001_initial.rs
/// // myapp/0002_add_field.rs
/// // other_app/0001_initial.rs
///
/// let numbers = MigrationNumbering::get_all_numbers(Path::new("migrations"));
/// assert_eq!(numbers.get("myapp"), Some(&2));
/// assert_eq!(numbers.get("other_app"), Some(&1));
/// ```
pub fn get_all_numbers(migrations_dir: &Path) -> HashMap<String, u32> {
let mut result = HashMap::new();
// If directory doesn't exist, return empty map
if !migrations_dir.exists() {
return result;
}
// Scan for app directories
if let Ok(entries) = std::fs::read_dir(migrations_dir) {
for entry in entries.flatten() {
let path = entry.path();
// Only process directories
if !path.is_dir() {
continue;
}
// Extract app label from directory name
if let Some(app_label) = path.file_name().and_then(|s| s.to_str()) {
let highest = Self::get_highest_number(migrations_dir, app_label);
if highest > 0 {
result.insert(app_label.to_string(), highest);
}
}
}
}
result
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
use std::fs;
#[rstest]
#[test]
fn test_next_number_first_migration() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
// Act
let next = MigrationNumbering::next_number(&migrations_dir, "myapp");
// Assert
assert_eq!(next, "0001");
}
#[rstest]
#[test]
fn test_next_number_existing_migrations() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
let app_dir = migrations_dir.join("myapp");
fs::create_dir_all(&app_dir).unwrap();
fs::write(app_dir.join("0001_initial.rs"), "").unwrap();
fs::write(app_dir.join("0002_add_field.rs"), "").unwrap();
fs::write(app_dir.join("0003_remove_field.rs"), "").unwrap();
// Act
let next = MigrationNumbering::next_number(&migrations_dir, "myapp");
// Assert
assert_eq!(next, "0004");
}
#[rstest]
#[test]
fn test_get_highest_number_no_migrations() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
// Act
let highest = MigrationNumbering::get_highest_number(&migrations_dir, "myapp");
// Assert
assert_eq!(highest, 0);
}
#[rstest]
#[test]
fn test_get_highest_number_with_migrations() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
let app_dir = migrations_dir.join("myapp");
fs::create_dir_all(&app_dir).unwrap();
fs::write(app_dir.join("0001_initial.rs"), "").unwrap();
fs::write(app_dir.join("0005_add_field.rs"), "").unwrap();
fs::write(app_dir.join("0003_remove_field.rs"), "").unwrap();
// Act
let highest = MigrationNumbering::get_highest_number(&migrations_dir, "myapp");
// Assert
assert_eq!(highest, 5);
}
#[rstest]
#[test]
fn test_get_highest_number_ignores_non_migration_files() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
let app_dir = migrations_dir.join("myapp");
fs::create_dir_all(&app_dir).unwrap();
fs::write(app_dir.join("0001_initial.rs"), "").unwrap();
fs::write(app_dir.join("README.md"), "").unwrap();
fs::write(app_dir.join("myapp.rs"), "").unwrap();
fs::write(app_dir.join("invalid_name.rs"), "").unwrap();
// Act
let highest = MigrationNumbering::get_highest_number(&migrations_dir, "myapp");
// Assert
assert_eq!(highest, 1);
}
#[rstest]
#[test]
fn test_get_all_numbers() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
let app1_dir = migrations_dir.join("app1");
let app2_dir = migrations_dir.join("app2");
fs::create_dir_all(&app1_dir).unwrap();
fs::create_dir_all(&app2_dir).unwrap();
fs::write(app1_dir.join("0001_initial.rs"), "").unwrap();
fs::write(app1_dir.join("0002_add_field.rs"), "").unwrap();
fs::write(app2_dir.join("0001_initial.rs"), "").unwrap();
// Act
let all_numbers = MigrationNumbering::get_all_numbers(&migrations_dir);
// Assert
assert_eq!(all_numbers.get("app1"), Some(&2));
assert_eq!(all_numbers.get("app2"), Some(&1));
}
#[rstest]
#[test]
fn test_zero_padding() {
// Arrange
let temp_dir = tempfile::tempdir().unwrap();
let migrations_dir = temp_dir.path().join("migrations");
let app_dir = migrations_dir.join("myapp");
fs::create_dir_all(&app_dir).unwrap();
fs::write(app_dir.join("0099_test.rs"), "").unwrap();
// Act
let next = MigrationNumbering::next_number(&migrations_dir, "myapp");
// Assert
assert_eq!(next, "0100");
}
}