Commit

Billing: a workspace rename moves its token usage, package storage, month-end snapshots, price notices, cost and count rows, margin alert and budget alerts too; a test keeps the rename and the reset naming the same tables

syntaqxcommitted Parent630fd0dBrowse files
2 files+41−00/2 viewed
+31−0
7979 "DELETE FROM trial_grants WHERE workspace = ?2",
8080 "UPDATE OR IGNORE storage_days SET workspace = ?1 WHERE workspace = ?2",
8181 "DELETE FROM storage_days WHERE workspace = ?2",
82+ "UPDATE OR IGNORE package_storage_days SET workspace = ?1 WHERE workspace = ?2",
83+ "DELETE FROM package_storage_days WHERE workspace = ?2",
84+ // Month-end snapshots, by day: the current slug's stays if it has one.
85+ "UPDATE OR IGNORE pending_days SET workspace = ?1 WHERE workspace = ?2",
86+ "DELETE FROM pending_days WHERE workspace = ?2",
87+ // Model tokens, by day, person, session and model: the counts add.
88+ "INSERT INTO token_usage (day, workspace, person, session, model, tier, input, output, cache_read, cache_write, requests)
89+ SELECT day, ?1, person, session, model, tier, input, output, cache_read, cache_write, requests FROM token_usage WHERE workspace = ?2
90+ ON CONFLICT (day, workspace, person, session, model) DO UPDATE SET
91+ input = token_usage.input + excluded.input,
92+ output = token_usage.output + excluded.output,
93+ cache_read = token_usage.cache_read + excluded.cache_read,
94+ cache_write = token_usage.cache_write + excluded.cache_write,
95+ requests = token_usage.requests + excluded.requests",
96+ "DELETE FROM token_usage WHERE workspace = ?2",
97+ // Price notices sent, one per version: told once is told.
98+ "UPDATE OR IGNORE price_notices SET workspace = ?1 WHERE workspace = ?2",
99+ "DELETE FROM price_notices WHERE workspace = ?2",
100+ "UPDATE OR IGNORE closed_workspaces SET workspace = ?1 WHERE workspace = ?2",
101+ "DELETE FROM closed_workspaces WHERE workspace = ?2",
102+ // The margin figures and counts are redone each day from the ledger and
103+ // the meters; moved so the days between keep adding up.
104+ "UPDATE OR IGNORE workspace_costs SET workspace = ?1 WHERE workspace = ?2",
105+ "DELETE FROM workspace_costs WHERE workspace = ?2",
106+ "UPDATE OR IGNORE own_counts SET workspace = ?1 WHERE workspace = ?2",
107+ "DELETE FROM own_counts WHERE workspace = ?2",
108+ "UPDATE margin_alerts SET subject = ?1 WHERE kind = 'workspace' AND subject = ?2",
82109 // Holds, spikes, requests and the plan's payments: many per workspace.
83110 "UPDATE reservations SET workspace = ?1 WHERE workspace = ?2",
84111 "UPDATE reservations SET repo = ?1 || substr(repo, length(?2) + 1) WHERE substr(repo, 1, length(?2) + 1) = ?2 || '/'",
147174 "UPDATE OR IGNORE billing_accounts SET id = ?3, name = CASE WHEN name = ?2 THEN ?1 ELSE name END WHERE id = ?4",
148175 "DELETE FROM billing_accounts WHERE id = ?4",
149176 "UPDATE admin_actions SET account = ?3 WHERE account = ?4",
177+ "UPDATE OR IGNORE budget_alerts SET account = ?3 WHERE account = ?4",
178+ "DELETE FROM budget_alerts WHERE account = ?4",
150179 "UPDATE account_members SET account_id = ?3 WHERE account_id = ?4",
151180 "UPDATE enterprise_invoices SET account_id = ?3 WHERE account_id = ?4",
152181 ];
278307 "enterprise_invoice_lines", "billing_accounts", "admin_actions", "enterprise_invoices",
279308 "allowance_use", "trial_grants", "storage_days",
280309 "reservations", "spikes", "limit_requests", "plan_payments", "card_checks", "alerts_sent",
310+ "package_storage_days", "pending_days", "token_usage", "price_notices", "closed_workspaces",
311+ "workspace_costs", "own_counts",
281312 ] {
282313 assert!(all.contains(&format!("FROM {table} WHERE workspace = ?2"))
283314 || all.contains(&format!("UPDATE {table} SET"))
+10−0
180180 }
181181
182182 #[test]
183+ fn a_rename_moves_every_table_a_reset_wipes() {
184+ let moved = crate::rename::STATEMENTS.join("\n");
185+ // Lines follow their invoice, which carries the workspace.
186+ for sql in STATEMENTS.iter().filter(|sql| !sql.contains("workspace_invoice_lines")) {
187+ let table = sql.split_whitespace().nth(2).unwrap();
188+ assert!(moved.contains(&format!(" {table} ")), "{table} is wiped on a reset but not moved on a rename");
189+ }
190+ }
191+
192+ #[test]
183193 fn statements_name_at_most_the_workspace_and_its_account() {
184194 assert!(STATEMENTS.iter().all(|sql| crate::rename::parameters(sql) <= 2));
185195 assert_eq!(crate::rename::parameters(STATEMENTS.last().unwrap()), 2);