backbone-orm 2.7.36

Backbone Framework ORM - Database layer with PostgreSQL support
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
//! Database migration system

use anyhow::Result;
use sqlx::{PgPool, Row};
use std::fs;
use std::path::Path;
use colored::*;
use chrono::{DateTime, Utc};

/// Migration manager
pub struct MigrationManager {
    pool: PgPool,
}

impl MigrationManager {
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }

    /// Run all pending migrations
    pub async fn migrate(&self) -> Result<()> {
        println!("🔄 Running database migrations...");

        // Create migrations table if it doesn't exist
        self.create_migrations_table().await?;

        // Get all migration files
        let migrations = self.load_migration_files()?;

        // Get applied migrations from database
        let applied_migrations = self.get_applied_migrations().await?;

        // Run pending migrations
        for migration in migrations {
            if !applied_migrations.contains(&migration.name) {
                println!("  ↳ Applying migration: {}", migration.name.bright_green());
                self.apply_migration(&migration).await?;
            } else {
                println!("  ✓ Skipping already applied: {}", migration.name);
            }
        }

        println!("✅ Migrations completed successfully");
        Ok(())
    }

    /// Rollback the last migration
    pub async fn rollback(&self) -> Result<()> {
        println!("🔄 Rolling back last migration...");

        // Create migrations table if it doesn't exist
        self.create_migrations_table().await?;

        // Get the last applied migration
        let last_migration = self.get_last_applied_migration().await?;

        match last_migration {
            Some(migration_name) => {
                println!("  ↳ Rolling back migration: {}", migration_name.bright_yellow());

                // Find and execute the down migration
                let down_migration = self.find_down_migration(&migration_name)?;

                if let Some(down_content) = down_migration {
                    self.rollback_migration(&migration_name, &down_content).await?;
                    println!("  ✓ Successfully rolled back: {}", migration_name.bright_green());
                } else {
                    println!("  ⚠️  No down migration found for: {}", migration_name.yellow());
                }
            }
            None => {
                println!("  ℹ️  No migrations to rollback");
            }
        }

        println!("✅ Rollback completed");
        Ok(())
    }

    /// Rollback a specific number of migrations
    pub async fn rollback_n(&self, count: usize) -> Result<()> {
        println!("🔄 Rolling back {} migrations...", count);

        for i in 0..count {
            let last_migration = self.get_last_applied_migration().await?;

            match last_migration {
                Some(migration_name) => {
                    println!("  ↳ Rolling back {}/1: {}", i + 1, migration_name.bright_yellow());

                    let down_migration = self.find_down_migration(&migration_name)?;

                    if let Some(down_content) = down_migration {
                        self.rollback_migration(&migration_name, &down_content).await?;
                        println!("  ✓ Successfully rolled back: {}", migration_name.bright_green());
                    } else {
                        println!("  ⚠️  No down migration found for: {}", migration_name.yellow());
                        break;
                    }
                }
                None => {
                    println!("  ℹ️  No more migrations to rollback");
                    break;
                }
            }
        }

        println!("✅ Rollback of {} migrations completed", count);
        Ok(())
    }

    /// Get migration history
    pub async fn history(&self) -> Result<Vec<MigrationRecord>> {
        // Create migrations table if it doesn't exist
        self.create_migrations_table().await?;

        let rows = sqlx::query("SELECT id, name, applied_at FROM schema_migrations ORDER BY applied_at DESC")
            .fetch_all(&self.pool)
            .await?;

        let records: Result<Vec<MigrationRecord>, sqlx::Error> = rows.into_iter()
            .map(|row| {
                Ok(MigrationRecord {
                    id: row.get("id"),
                    name: row.get("name"),
                    applied_at: row.get("applied_at"),
                })
            })
            .collect();

        match records {
            Ok(history) => {
                if history.is_empty() {
                    println!("  ℹ️  No migrations have been applied yet");
                } else {
                    println!("📋 Migration History:");
                    for (i, record) in history.iter().enumerate() {
                        let status = if i == 0 {
                            "🔹".to_string() // Current (last) migration
                        } else {
                            "  ".to_string()
                        };
                        println!("  {} {} - Applied at {}", status, record.name.bright_cyan(), record.applied_at.format("%Y-%m-%d %H:%M:%S"));
                    }
                }
                Ok(history)
            }
            Err(e) => Err(anyhow::anyhow!("Failed to load migration history: {}", e))
        }
    }

    /// Validate migration status
    pub async fn status(&self) -> Result<MigrationStatus> {
        // Create migrations table if it doesn't exist
        self.create_migrations_table().await?;

        // Get migration files
        let migration_files = self.load_migration_files()?;

        // Get applied migrations
        let applied_migrations = self.get_applied_migrations().await?;

        // Determine pending and applied migrations
        let mut pending_migrations = Vec::new();
        let mut applied_migration_details = Vec::new();

        for migration_file in migration_files {
            if applied_migrations.contains(&migration_file.name) {
                applied_migration_details.push(migration_file.name);
            } else {
                pending_migrations.push(migration_file.name);
            }
        }

        let status = MigrationStatus {
            total_migrations: applied_migration_details.len() + pending_migrations.len(),
            applied_migrations: applied_migration_details,
            pending_migrations,
            last_migration: applied_migrations.last().cloned(),
        };

        // Print status
        println!("📊 Migration Status:");
        println!("  Total migrations: {}", status.total_migrations);
        println!("  Applied migrations: {}", status.applied_migrations.len());
        println!("  Pending migrations: {}", status.pending_migrations.len());

        if !status.pending_migrations.is_empty() {
            println!("  Pending:");
            for migration in &status.pending_migrations {
                println!("    - {}", migration.yellow());
            }
        }

        Ok(status)
    }

    /// Get the last applied migration
    async fn get_last_applied_migration(&self) -> Result<Option<String>> {
        let row = sqlx::query("SELECT name FROM schema_migrations ORDER BY applied_at DESC LIMIT 1")
            .fetch_optional(&self.pool)
            .await?;

        Ok(row.map(|r| r.get("name")))
    }

    /// Find the corresponding down migration file.
    ///
    /// Migrations live as paired siblings: `<name>.up.sql` and
    /// `<name>.down.sql`. The migration "name" stored in the
    /// `schema_migrations` table is the part before `.up.sql`, so finding
    /// the rollback is a direct path lookup.
    fn find_down_migration(&self, migration_name: &str) -> Result<Option<String>> {
        let path = Path::new("migrations").join(format!("{}.down.sql", migration_name));
        if path.exists() {
            Ok(Some(fs::read_to_string(&path)?))
        } else {
            Ok(None)
        }
    }

    /// Rollback a specific migration in a single transaction.
    ///
    /// Down migrations are multi-statement (drop indexes, drop table,
    /// drop functions, drop types) so we use `sqlx::raw_sql` rather than
    /// `sqlx::query` — the latter goes through the extended protocol and
    /// rejects compound statements.
    async fn rollback_migration(&self, migration_name: &str, down_content: &str) -> Result<()> {
        let mut tx = self.pool.begin().await?;

        sqlx::raw_sql(down_content)
            .execute(&mut *tx)
            .await?;

        sqlx::query("DELETE FROM schema_migrations WHERE name = $1")
            .bind(migration_name)
            .execute(&mut *tx)
            .await?;

        tx.commit().await?;
        Ok(())
    }

    /// Create a new paired up/down migration: `<ts>_<name>.up.sql` and
    /// `<ts>_<name>.down.sql` under `migrations/`. Timestamp is the
    /// `YYYYMMDDHHMMSS` form so it sorts cleanly next to generator-emitted
    /// migrations.
    pub async fn create_migration(&self, name: &str) -> Result<()> {
        println!("📝 Creating new migration: {}", name.bright_cyan());

        let timestamp = chrono::Utc::now().format("%Y%m%d%H%M%S");
        let stem = format!("{}_{}", timestamp, name);
        let migrations_dir = "migrations";
        fs::create_dir_all(migrations_dir)?;

        let up_content = format!(
            r#"-- Migration: {name}
-- Created: {timestamp}

-- Add your migration SQL here
-- Example:
-- CREATE TABLE example_table (
--     id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
--     created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
--     updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
-- );
"#,
            name = name,
            timestamp = timestamp
        );
        let up_path = Path::new(migrations_dir).join(format!("{}.up.sql", stem));
        fs::write(&up_path, up_content)?;

        let down_content = format!(
            r#"-- Down Migration: {name}
-- Created: {timestamp}

-- Add your rollback SQL here
-- Example:
-- DROP TABLE IF EXISTS example_table CASCADE;
"#,
            name = name,
            timestamp = timestamp
        );
        let down_path = Path::new(migrations_dir).join(format!("{}.down.sql", stem));
        fs::write(&down_path, down_content)?;

        println!("  ✓ Created: {}", up_path.display().to_string().bright_green());
        println!("  ✓ Created: {}", down_path.display().to_string().bright_green());
        Ok(())
    }

    /// Create migrations table to track applied migrations
    async fn create_migrations_table(&self) -> Result<()> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS schema_migrations (
                id SERIAL PRIMARY KEY,
                name VARCHAR(255) NOT NULL UNIQUE,
                applied_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
            )
            "#
        )
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Load up-migration files from `migrations/`, sorted by name (which
    /// for our convention also sorts chronologically because filenames are
    /// timestamp-prefixed). The "name" returned strips the `.up.sql`
    /// suffix so it matches what gets recorded in `schema_migrations`.
    fn load_migration_files(&self) -> Result<Vec<MigrationFile>> {
        let migrations_dir = Path::new("migrations");
        let mut migrations = Vec::new();

        if migrations_dir.exists() {
            for entry in fs::read_dir(migrations_dir)? {
                let entry = entry?;
                let path = entry.path();
                let Some(file_name) = path.file_name().and_then(|f| f.to_str()) else {
                    continue;
                };
                let Some(name) = file_name.strip_suffix(".up.sql") else {
                    continue;
                };
                let content = fs::read_to_string(&path)?;
                migrations.push(MigrationFile {
                    name: name.to_string(),
                    content,
                });
            }
        }

        migrations.sort_by(|a, b| a.name.cmp(&b.name));
        Ok(migrations)
    }

    /// Get list of applied migrations from database
    async fn get_applied_migrations(&self) -> Result<Vec<String>> {
        let rows = sqlx::query("SELECT name FROM schema_migrations ORDER BY name")
            .fetch_all(&self.pool)
            .await?;

        let migrations: Vec<String> = rows
            .into_iter()
            .map(|row| row.get::<String, _>("name"))
            .collect();

        Ok(migrations)
    }

    /// Apply a single migration in a single transaction.
    ///
    /// Generated `.up.sql` files contain multiple statements (CREATE
    /// TABLE + CREATE INDEX + CREATE FUNCTION + ALTER TABLE …). Use
    /// `sqlx::raw_sql` so the simple-query protocol handles the compound
    /// SQL — `sqlx::query` only accepts a single statement.
    async fn apply_migration(&self, migration: &MigrationFile) -> Result<()> {
        let mut tx = self.pool.begin().await?;

        sqlx::raw_sql(&migration.content)
            .execute(&mut *tx)
            .await?;

        sqlx::query("INSERT INTO schema_migrations (name) VALUES ($1)")
            .bind(&migration.name)
            .execute(&mut *tx)
            .await?;

        tx.commit().await?;
        Ok(())
    }
}

/// Migration file representation
struct MigrationFile {
    name: String,
    content: String,
}

/// Migration trait
pub trait Migration {
    fn name(&self) -> &str;
    fn up(&self) -> &str;
    fn down(&self) -> &str;
}

/// Migration record from database
#[derive(Debug, Clone)]
pub struct MigrationRecord {
    pub id: i32,
    pub name: String,
    pub applied_at: DateTime<Utc>,
}

/// Migration status information
#[derive(Debug, Clone)]
pub struct MigrationStatus {
    pub total_migrations: usize,
    pub applied_migrations: Vec<String>,
    pub pending_migrations: Vec<String>,
    pub last_migration: Option<String>,
}