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
use anyhow::Result;
use include_dir::{include_dir, Dir};
use sqlx::PgPool;
use std::sync::Arc;
static MIGRATIONS: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations");
/// Migration metadata
#[derive(Debug)]
struct Migration {
version: i64,
name: String,
sql: String,
}
/// Migration runner that handles schema-qualified migrations
pub struct MigrationRunner {
pool: Arc<PgPool>,
schema_name: String,
}
impl MigrationRunner {
/// Create a new migration runner
pub fn new(pool: Arc<PgPool>, schema_name: String) -> Self {
Self { pool, schema_name }
}
/// Run all pending migrations
pub async fn migrate(&self) -> Result<()> {
// Ensure schema exists
if self.schema_name != "public" {
sqlx::query(&format!("CREATE SCHEMA IF NOT EXISTS {}", self.schema_name))
.execute(&*self.pool)
.await?;
}
// Load migrations from filesystem
let migrations = self.load_migrations()?;
tracing::debug!(
"Loaded {} migrations for schema {}",
migrations.len(),
self.schema_name
);
// Ensure migration tracking table exists (in the schema)
self.ensure_migration_table().await?;
// Get applied migrations
let applied_versions = self.get_applied_versions().await?;
tracing::debug!("Applied migrations: {:?}", applied_versions);
// Check if key tables exist - if not, we need to re-run migrations even if marked as applied
// This handles the case where cleanup dropped tables but not the migration tracking table
let tables_exist = self.check_tables_exist().await.unwrap_or(false);
// Apply pending migrations (or re-apply if tables don't exist)
for migration in migrations {
let should_apply = if !applied_versions.contains(&migration.version) {
true // New migration
} else if !tables_exist {
// Migration was applied but tables don't exist - re-apply
tracing::warn!(
"Migration {} is marked as applied but tables don't exist, re-applying",
migration.version
);
// Remove the old migration record so we can re-apply
sqlx::query(&format!(
"DELETE FROM {}._duroxide_migrations WHERE version = $1",
self.schema_name
))
.bind(migration.version)
.execute(&*self.pool)
.await?;
true
} else {
false // Already applied and tables exist
};
if should_apply {
tracing::debug!(
"Applying migration {}: {}",
migration.version,
migration.name
);
self.apply_migration(&migration).await?;
} else {
tracing::debug!(
"Skipping migration {}: {} (already applied)",
migration.version,
migration.name
);
}
}
Ok(())
}
/// Load migrations from the embedded migrations directory
fn load_migrations(&self) -> Result<Vec<Migration>> {
let mut migrations = Vec::new();
// Get all files from embedded directory
let mut files: Vec<_> = MIGRATIONS
.files()
.filter(|file| file.path().extension().and_then(|ext| ext.to_str()) == Some("sql"))
.collect();
// Sort by path to ensure consistent ordering
files.sort_by_key(|f| f.path());
for file in files {
let file_name = file
.path()
.file_name()
.and_then(|n| n.to_str())
.ok_or_else(|| anyhow::anyhow!("Invalid filename in migrations"))?;
let sql = file
.contents_utf8()
.ok_or_else(|| anyhow::anyhow!("Migration file is not valid UTF-8: {file_name}"))?
.to_string();
let version = self.parse_version(file_name)?;
let name = file_name.to_string();
migrations.push(Migration { version, name, sql });
}
Ok(migrations)
}
/// Parse version number from migration filename (e.g., "0001_initial.sql" -> 1)
fn parse_version(&self, filename: &str) -> Result<i64> {
let version_str = filename
.split('_')
.next()
.ok_or_else(|| anyhow::anyhow!("Invalid migration filename: {filename}"))?;
version_str
.parse::<i64>()
.map_err(|e| anyhow::anyhow!("Invalid migration version {version_str}: {e}"))
}
/// Ensure migration tracking table exists
async fn ensure_migration_table(&self) -> Result<()> {
// Create migration table in the target schema
sqlx::query(&format!(
r#"
CREATE TABLE IF NOT EXISTS {}._duroxide_migrations (
version BIGINT PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
)
"#,
self.schema_name
))
.execute(&*self.pool)
.await?;
Ok(())
}
/// Check if key tables exist
async fn check_tables_exist(&self) -> Result<bool> {
// Check if instances table exists (as a proxy for all tables)
let exists: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM information_schema.tables WHERE table_schema = $1 AND table_name = 'instances')",
)
.bind(&self.schema_name)
.fetch_one(&*self.pool)
.await?;
Ok(exists)
}
/// Get list of applied migration versions
async fn get_applied_versions(&self) -> Result<Vec<i64>> {
let versions: Vec<i64> = sqlx::query_scalar(&format!(
"SELECT version FROM {}._duroxide_migrations ORDER BY version",
self.schema_name
))
.fetch_all(&*self.pool)
.await?;
Ok(versions)
}
/// Split SQL into statements, respecting dollar-quoted strings ($$...$$)
/// This handles stored procedures and other constructs that use dollar-quoting
fn split_sql_statements(sql: &str) -> Vec<String> {
let mut statements = Vec::new();
let mut current_statement = String::new();
let chars: Vec<char> = sql.chars().collect();
let mut i = 0;
let mut in_dollar_quote = false;
let mut dollar_tag: Option<String> = None;
while i < chars.len() {
let ch = chars[i];
if !in_dollar_quote {
// Check for start of dollar-quoted string
if ch == '$' {
let mut tag = String::new();
tag.push(ch);
i += 1;
// Collect the tag (e.g., $$, $tag$, $function$)
while i < chars.len() {
let next_ch = chars[i];
if next_ch == '$' {
tag.push(next_ch);
dollar_tag = Some(tag.clone());
in_dollar_quote = true;
current_statement.push_str(&tag);
i += 1;
break;
} else if next_ch.is_alphanumeric() || next_ch == '_' {
tag.push(next_ch);
i += 1;
} else {
// Not a dollar quote, just a $ character
current_statement.push(ch);
break;
}
}
} else if ch == ';' {
// End of statement (only if not in dollar quote)
current_statement.push(ch);
let trimmed = current_statement.trim().to_string();
if !trimmed.is_empty() {
statements.push(trimmed);
}
current_statement.clear();
i += 1;
} else {
current_statement.push(ch);
i += 1;
}
} else {
// Inside dollar-quoted string
current_statement.push(ch);
// Check for end of dollar-quoted string
if ch == '$' {
let tag = dollar_tag.as_ref().unwrap();
let mut matches = true;
// Check if the following characters match the closing tag
for (j, tag_char) in tag.chars().enumerate() {
if j == 0 {
continue; // Skip first $ (we already matched it)
}
if i + j >= chars.len() || chars[i + j] != tag_char {
matches = false;
break;
}
}
if matches {
// Found closing tag - consume remaining tag characters
for _ in 0..(tag.len() - 1) {
if i + 1 < chars.len() {
current_statement.push(chars[i + 1]);
i += 1;
}
}
in_dollar_quote = false;
dollar_tag = None;
}
}
i += 1;
}
}
// Add remaining statement if any
let trimmed = current_statement.trim().to_string();
if !trimmed.is_empty() {
statements.push(trimmed);
}
statements
}
/// Apply a single migration
async fn apply_migration(&self, migration: &Migration) -> Result<()> {
// Start transaction
let mut tx = self.pool.begin().await?;
// Set search_path for this transaction
sqlx::query(&format!("SET LOCAL search_path TO {}", self.schema_name))
.execute(&mut *tx)
.await?;
// Remove comment lines and split SQL into individual statements
let sql = migration.sql.trim();
let cleaned_sql: String = sql
.lines()
.map(|line| {
// Remove full-line comments
if let Some(idx) = line.find("--") {
// Check if -- is inside a string (simple check)
let before = &line[..idx];
if before.matches('\'').count() % 2 == 0 {
// Even number of quotes means -- is not in a string
line[..idx].trim()
} else {
line
}
} else {
line
}
})
.filter(|line| !line.is_empty())
.collect::<Vec<_>>()
.join("\n");
// Split by semicolon, but respect dollar-quoted strings ($$...$$)
let statements = Self::split_sql_statements(&cleaned_sql);
tracing::debug!(
"Executing {} statements for migration {}",
statements.len(),
migration.version
);
for (idx, statement) in statements.iter().enumerate() {
if !statement.trim().is_empty() {
tracing::debug!(
"Executing statement {} of {}: {}...",
idx + 1,
statements.len(),
&statement.chars().take(50).collect::<String>()
);
sqlx::query(statement)
.execute(&mut *tx)
.await
.map_err(|e| {
anyhow::anyhow!(
"Failed to execute statement {} in migration {}: {}\nStatement: {}",
idx + 1,
migration.version,
e,
statement
)
})?;
}
}
// Record migration as applied
sqlx::query(&format!(
"INSERT INTO {}._duroxide_migrations (version, name) VALUES ($1, $2)",
self.schema_name
))
.bind(migration.version)
.bind(&migration.name)
.execute(&mut *tx)
.await?;
// Commit transaction
tx.commit().await?;
tracing::info!(
"Applied migration {}: {}",
migration.version,
migration.name
);
Ok(())
}
}