| 1 | use rusqlite::{Connection, Transaction}; |
| 2 | |
| 3 | use crate::error::{Error, Result}; |
| 4 | use crate::search; |
| 5 | |
| 6 | struct Migration { |
| 7 | script: &'static str, |
| 8 | after: Option<fn(&Transaction<'_>) -> Result<()>>, |
| 9 | } |
| 10 | |
| 11 | const MIGRATIONS: &[Migration] = &[ |
| 12 | Migration { |
| 13 | script: include_str!("migrations/0001_initial.sql"), |
| 14 | after: None, |
| 15 | }, |
| 16 | Migration { |
| 17 | script: include_str!("migrations/0002_delta_and_meta.sql"), |
| 18 | after: None, |
| 19 | }, |
| 20 | Migration { |
| 21 | script: include_str!("migrations/0003_previews_avatars_folders.sql"), |
| 22 | after: None, |
| 23 | }, |
| 24 | Migration { |
| 25 | script: include_str!("migrations/0004_images_and_search.sql"), |
| 26 | after: Some(search::backfill), |
| 27 | }, |
| 28 | Migration { |
| 29 | script: include_str!("migrations/0005_team_layout.sql"), |
| 30 | after: None, |
| 31 | }, |
| 32 | Migration { |
| 33 | script: include_str!("migrations/0006_presence.sql"), |
| 34 | after: None, |
| 35 | }, |
| 36 | Migration { |
| 37 | script: include_str!("migrations/0007_sender_application.sql"), |
| 38 | after: None, |
| 39 | }, |
| 40 | Migration { |
| 41 | script: include_str!("migrations/0008_activity.sql"), |
| 42 | after: None, |
| 43 | }, |
| 44 | Migration { |
| 45 | script: include_str!("migrations/0009_muted.sql"), |
| 46 | after: None, |
| 47 | }, |
| 48 | Migration { |
| 49 | script: include_str!("migrations/0010_name_lookup_indexes.sql"), |
| 50 | after: None, |
| 51 | }, |
| 52 | Migration { |
| 53 | script: include_str!("migrations/0011_message_links.sql"), |
| 54 | after: None, |
| 55 | }, |
| 56 | Migration { |
| 57 | script: include_str!("migrations/0012_outbox_drafts.sql"), |
| 58 | after: None, |
| 59 | }, |
| 60 | Migration { |
| 61 | script: include_str!("migrations/0013_message_subject.sql"), |
| 62 | after: None, |
| 63 | }, |
| 64 | Migration { |
| 65 | script: include_str!("migrations/0014_folder_expanded.sql"), |
| 66 | after: None, |
| 67 | }, |
| 68 | Migration { |
| 69 | script: include_str!("migrations/0015_channel_tabs.sql"), |
| 70 | after: None, |
| 71 | }, |
| 72 | Migration { |
| 73 | script: include_str!("migrations/0016_channel_notifications.sql"), |
| 74 | after: None, |
| 75 | }, |
| 76 | Migration { |
| 77 | script: include_str!("migrations/0017_agents.sql"), |
| 78 | after: None, |
| 79 | }, |
| 80 | Migration { |
| 81 | script: include_str!("migrations/0018_agent_rich_proposals.sql"), |
| 82 | after: None, |
| 83 | }, |
| 84 | Migration { |
| 85 | script: include_str!("migrations/0019_messages_by_sender.sql"), |
| 86 | after: None, |
| 87 | }, |
| 88 | ]; |
| 89 | |
| 90 | pub fn latest_version() -> i64 { |
| 91 | MIGRATIONS.len() as i64 |
| 92 | } |
| 93 | |
| 94 | pub fn migrate(connection: &mut Connection) -> Result<()> { |
| 95 | let found: i64 = connection.pragma_query_value(None, "user_version", |row| row.get(0))?; |
| 96 | if found > latest_version() { |
| 97 | return Err(Error::SchemaTooNew { |
| 98 | found, |
| 99 | supported: latest_version(), |
| 100 | }); |
| 101 | } |
| 102 | for (index, migration) in MIGRATIONS.iter().enumerate().skip(found as usize) { |
| 103 | let transaction = connection.transaction()?; |
| 104 | transaction.execute_batch(migration.script)?; |
| 105 | if let Some(after) = migration.after { |
| 106 | after(&transaction)?; |
| 107 | } |
| 108 | transaction.pragma_update(None, "user_version", index as i64 + 1)?; |
| 109 | transaction.commit()?; |
| 110 | } |
| 111 | Ok(()) |
| 112 | } |