| 41 | 41 | | "DELETE FROM month_closes WHERE workspace = ?1", |
| 42 | 42 | | "DELETE FROM storage_days WHERE workspace = ?1", |
| 43 | 43 | | "DELETE FROM package_storage_days WHERE workspace = ?1", |
| 44 | | − | "DELETE FROM sandbox_months WHERE workspace = ?1", |
| 45 | 44 | | "DELETE FROM token_usage WHERE workspace = ?1", |
| 46 | 45 | | "DELETE FROM spikes WHERE workspace = ?1", |
| 47 | 46 | | "DELETE FROM closed_workspaces WHERE workspace = ?1", |
| ⋯ |
| 124 | 123 | | "ledger", "runs", "checkouts", "workspace_invoices", "workspace_invoice_lines", "sales_notes", "accounts", |
| 125 | 124 | | "pending_usage", "pending_days", "limits", "subscriptions", "month_closes", "sales_records", |
| 126 | 125 | | "billing_accounts", "allowance_use", "trial_grants", "storage_days", "package_storage_days", |
| 127 | | − | "sandbox_months", "token_usage", "reservations", "spikes", "limit_requests", "plan_payments", |
| 126 | + | "token_usage", "reservations", "spikes", "limit_requests", "plan_payments", |
| 128 | 127 | | "card_checks", "alerts_sent", "price_notices", "closed_workspaces", "workspace_costs", |
| 129 | 128 | | "margin_alerts", "budget_alerts", |
| 130 | 129 | | ] { |
| ⋯ |
| 133 | 132 | | } |
| 134 | 133 | | } |
| 135 | 134 | | |
| 135 | + | /// The tables the migrations leave: every one made, less those dropped. |
| 136 | + | fn live_tables() -> std::collections::BTreeSet<String> { |
| 137 | + | let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("migrations"); |
| 138 | + | let mut files: Vec<_> = std::fs::read_dir(dir).unwrap().map(|e| e.unwrap().path()).collect(); |
| 139 | + | files.sort(); |
| 140 | + | let mut live = std::collections::BTreeSet::new(); |
| 141 | + | for file in files { |
| 142 | + | let sql = std::fs::read_to_string(file).unwrap(); |
| 143 | + | for line in sql.lines().map(str::trim) { |
| 144 | + | let words: Vec<&str> = line.split(|c: char| c.is_whitespace() || c == '(' || c == ';').filter(|w| !w.is_empty()).collect(); |
| 145 | + | let name = |at: usize| words.get(at).map(|w| w.to_string()); |
| 146 | + | match words.as_slice() { |
| 147 | + | ["CREATE", "TABLE", "IF", "NOT", "EXISTS", ..] => live.extend(name(5)), |
| 148 | + | ["CREATE", "TABLE", ..] => live.extend(name(2)), |
| 149 | + | ["DROP", "TABLE", "IF", "EXISTS", ..] => { |
| 150 | + | name(4).map(|n| live.remove(&n)); |
| 151 | + | } |
| 152 | + | ["DROP", "TABLE", ..] => { |
| 153 | + | name(2).map(|n| live.remove(&n)); |
| 154 | + | } |
| 155 | + | _ => {} |
| 156 | + | } |
| 157 | + | } |
| 158 | + | } |
| 159 | + | live |
| 160 | + | } |
| 161 | + | |
| 162 | + | #[test] |
| 163 | + | fn every_table_wiped_is_one_the_migrations_leave() { |
| 164 | + | let live = live_tables(); |
| 165 | + | assert!(live.contains("ledger") && !live.contains("sandbox_months"), "{live:?}"); |
| 166 | + | for sql in STATEMENTS { |
| 167 | + | let table = sql.split_whitespace().nth(2).unwrap(); |
| 168 | + | assert!(live.contains(table), "{table} is not a table after the migrations"); |
| 169 | + | } |
| 170 | + | } |
| 171 | + | |
| 136 | 172 | | #[test] |
| 137 | 173 | | fn statements_name_at_most_the_workspace_and_its_account() { |
| 138 | 174 | | assert!(STATEMENTS.iter().all(|sql| crate::rename::parameters(sql) <= 2)); |