| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | use std::{fs, path::PathBuf}; |
| 23 | |
| 24 | use sqlx::{AssertSqlSafe, SqlitePool}; |
| 25 | |
| 26 | use crate::domain::{ |
| 27 | IssueNumber, IssueState, OrgId, OrgName, PullRequestState, PushEventId, RepoId, RepoName, |
| 28 | UserId, WebhookId, |
| 29 | repository::{ |
| 30 | IssueRepository, LabelRepository, MembershipRepository, OrgRepository, |
| 31 | PullRequestRepository, PushEventRepository, RepoRepository, TokenRepository, |
| 32 | UserRepository, WebhookDeliveryRepository, WebhookRepository, |
| 33 | }, |
| 34 | }; |
| 35 | use crate::infrastructure::repository::{ |
| 36 | SqliteIssueRepo, SqliteLabelRepo, SqliteMembershipRepo, SqliteOrgRepo, SqlitePullRequestRepo, |
| 37 | SqlitePushEventRepo, SqliteRepoRepo, SqliteTokenRepo, SqliteUserRepo, |
| 38 | SqliteWebhookDeliveryRepo, SqliteWebhookRepo, |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | const RELEASED_0_5_0: &[&str] = &[ |
| 47 | "20260804093030_create_orgs.sql", |
| 48 | "20260804093031_create_users.sql", |
| 49 | "20260804093032_create_memberships.sql", |
| 50 | "20260804093033_create_sessions.sql", |
| 51 | "20260804093034_add_org_bio.sql", |
| 52 | "20260804093035_create_repositories.sql", |
| 53 | "20260828120000_create_tokens.sql", |
| 54 | "20260829090000_add_repository_recency.sql", |
| 55 | "20260906120100_add_repository_import.sql", |
| 56 | "20260906120200_create_issues.sql", |
| 57 | "20260906120201_create_issue_comments.sql", |
| 58 | "20260906120300_create_push_events.sql", |
| 59 | "20260906120301_create_webhooks.sql", |
| 60 | "20260906120302_create_webhook_deliveries.sql", |
| 61 | "20260906120400_add_repository_archived.sql", |
| 62 | "20260906140100_add_token_lifecycle.sql", |
| 63 | "20260906140200_create_labels.sql", |
| 64 | "20260906140201_create_issue_labels.sql", |
| 65 | "20260906180100_create_pull_requests.sql", |
| 66 | "20260906180101_create_pull_request_comments.sql", |
| 67 | "20260906190000_create_pull_request_labels.sql", |
| 68 | "20260908090500_create_password_resets.sql", |
| 69 | ]; |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | const RELEASED_0_5_0_DIGEST: &str = |
| 79 | "e94827d74e0e715897f5f66e2d7a9709860dfcdc7b1741db2e059fcf59612b4e"; |
| 80 | |
| 81 | #[test] |
| 82 | fn a_shipped_migration_is_never_edited() { |
| 83 | use sha2::{Digest, Sha256}; |
| 84 | let mut hasher = Sha256::new(); |
| 85 | for name in RELEASED_0_5_0 { |
| 86 | hasher.update(std::fs::read(migrations_dir().join(name)).expect("a released migration")); |
| 87 | } |
| 88 | let digest = format!("{:x}", hasher.finalize()); |
| 89 | assert_eq!( |
| 90 | digest, RELEASED_0_5_0_DIGEST, |
| 91 | "a migration that already shipped has been edited; sqlx would refuse to boot on \ |
| 92 | every instance that applied it. Revert the edit — a new migration is the only \ |
| 93 | way to change a shipped schema." |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | const GRANDFATHERED: &[&str] = &[]; |
| 104 | |
| 105 | fn migrations_dir() -> PathBuf { |
| 106 | PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("migrations") |
| 107 | } |
| 108 | |
| 109 | |
| 110 | fn migration_files() -> Vec<(String, String)> { |
| 111 | let mut files: Vec<_> = fs::read_dir(migrations_dir()) |
| 112 | .expect("migrations/ should be readable") |
| 113 | .map(|entry| entry.expect("a directory entry").path()) |
| 114 | .filter(|path| path.extension().is_some_and(|ext| ext == "sql")) |
| 115 | .map(|path| { |
| 116 | let name = path |
| 117 | .file_name() |
| 118 | .expect("a file name") |
| 119 | .to_string_lossy() |
| 120 | .into_owned(); |
| 121 | let body = fs::read_to_string(&path).expect("a migration should be readable"); |
| 122 | (name, body) |
| 123 | }) |
| 124 | .collect(); |
| 125 | |
| 126 | |
| 127 | |
| 128 | files.sort_by(|(a, _), (b, _)| a.cmp(b)); |
| 129 | files |
| 130 | } |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | fn without_comments(body: &str) -> String { |
| 140 | let mut out = String::with_capacity(body.len()); |
| 141 | let mut chars = body.chars().peekable(); |
| 142 | let mut in_string = false; |
| 143 | |
| 144 | while let Some(c) = chars.next() { |
| 145 | if in_string { |
| 146 | out.push(c); |
| 147 | if c == '\'' { |
| 148 | in_string = false; |
| 149 | } |
| 150 | continue; |
| 151 | } |
| 152 | match c { |
| 153 | '\'' => { |
| 154 | in_string = true; |
| 155 | out.push(c); |
| 156 | } |
| 157 | '-' if chars.peek() == Some(&'-') => { |
| 158 | for skipped in chars.by_ref() { |
| 159 | if skipped == '\n' { |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | out.push(' '); |
| 164 | } |
| 165 | _ => out.push(c), |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | out |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | fn statements_only(body: &str) -> String { |
| 179 | let mut out = String::with_capacity(body.len()); |
| 180 | let stripped = without_comments(body); |
| 181 | let mut in_string = false; |
| 182 | |
| 183 | for c in stripped.chars() { |
| 184 | if in_string { |
| 185 | if c == '\'' { |
| 186 | in_string = false; |
| 187 | } |
| 188 | continue; |
| 189 | } |
| 190 | if c == '\'' { |
| 191 | in_string = true; |
| 192 | } else { |
| 193 | out.push(c.to_ascii_lowercase()); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | out.split_whitespace().collect::<Vec<_>>().join(" ") |
| 198 | } |
| 199 | |
| 200 | #[test] |
| 201 | fn no_migration_contracts_the_schema() { |
| 202 | for (name, body) in migration_files() { |
| 203 | if GRANDFATHERED.contains(&name.as_str()) { |
| 204 | continue; |
| 205 | } |
| 206 | let sql = statements_only(&body); |
| 207 | |
| 208 | for forbidden in ["drop column", "drop table", "rename to", "rename column"] { |
| 209 | assert!( |
| 210 | !sql.contains(forbidden), |
| 211 | "{name} contains `{forbidden}`, which the previous release's binary \ |
| 212 | cannot survive. Contracting waits a release — see plans/architecture.md." |
| 213 | ); |
| 214 | } |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | for statement in sql.split(';') { |
| 220 | let statement = statement.trim(); |
| 221 | if !statement.contains("add column") || !statement.contains("not null") { |
| 222 | continue; |
| 223 | } |
| 224 | assert!( |
| 225 | statement.contains("default"), |
| 226 | "{name} adds a `not null` column without a `default`:\n {statement}\n\ |
| 227 | SQLite refuses that on a non-empty table, and the previous release's \ |
| 228 | inserts would omit the column. Give it a default — see \ |
| 229 | plans/architecture.md." |
| 230 | ); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | #[test] |
| 236 | fn the_released_set_is_a_prefix_of_the_migrations_on_disk() { |
| 237 | let on_disk: Vec<String> = migration_files() |
| 238 | .into_iter() |
| 239 | .map(|(name, _)| name) |
| 240 | .collect(); |
| 241 | |
| 242 | assert!( |
| 243 | on_disk.len() >= RELEASED_0_5_0.len(), |
| 244 | "there are fewer migrations on disk than 0.5.0 shipped — one has been deleted" |
| 245 | ); |
| 246 | assert_eq!( |
| 247 | &on_disk[..RELEASED_0_5_0.len()], |
| 248 | RELEASED_0_5_0, |
| 249 | "RELEASED_0_5_0 no longer matches the start of migrations/. Either a name is \ |
| 250 | mistyped, or a new migration has been back-dated in front of a released one — \ |
| 251 | which would make it run on an instance that has already recorded a later \ |
| 252 | version. See plans/releases.md." |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | async fn pool_at_release_0_4_0() -> SqlitePool { |
| 263 | let pool = SqlitePool::connect("sqlite::memory:") |
| 264 | .await |
| 265 | .expect("in-memory database should open"); |
| 266 | |
| 267 | let dir = migrations_dir(); |
| 268 | for name in RELEASED_0_5_0 { |
| 269 | let body = fs::read_to_string(dir.join(name)).expect("a released migration should exist"); |
| 270 | let sql = without_comments(&body); |
| 271 | for statement in sql |
| 272 | .split(';') |
| 273 | .filter(|statement| !statement.trim().is_empty()) |
| 274 | { |
| 275 | |
| 276 | |
| 277 | sqlx::query(AssertSqlSafe(statement.to_owned())) |
| 278 | .execute(&pool) |
| 279 | .await |
| 280 | .unwrap_or_else(|error| panic!("{name} should apply: {error}\n {statement}")); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | pool |
| 285 | } |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | #[tokio::test] |
| 299 | async fn the_current_reads_work_against_the_last_released_schema() { |
| 300 | let pool = pool_at_release_0_4_0().await; |
| 301 | |
| 302 | let org_id = OrgId::from_trusted("o1"); |
| 303 | let user_id = UserId::from_trusted("u1"); |
| 304 | let repo_id = RepoId::from_trusted("r1"); |
| 305 | |
| 306 | |
| 307 | let orgs = SqliteOrgRepo::new(pool.clone()); |
| 308 | orgs.find_by_id(&org_id).await.expect("orgs.find_by_id"); |
| 309 | orgs.find_by_name(&OrgName::new("james").expect("valid name")) |
| 310 | .await |
| 311 | .expect("orgs.find_by_name"); |
| 312 | assert_eq!(orgs.count().await.expect("orgs.count"), 0); |
| 313 | |
| 314 | let users = SqliteUserRepo::new(pool.clone()); |
| 315 | users.find_by_id(&user_id).await.expect("users.find_by_id"); |
| 316 | users |
| 317 | .find_by_personal_org(&org_id) |
| 318 | .await |
| 319 | .expect("users.find_by_personal_org"); |
| 320 | assert_eq!(users.count().await.expect("users.count"), 0); |
| 321 | assert!(!users.any_exist().await.expect("users.any_exist")); |
| 322 | assert!(users.sole_user().await.expect("users.sole_user").is_none()); |
| 323 | |
| 324 | let memberships = SqliteMembershipRepo::new(pool.clone()); |
| 325 | memberships |
| 326 | .find(&org_id, &user_id) |
| 327 | .await |
| 328 | .expect("memberships.find"); |
| 329 | memberships |
| 330 | .list_for_user(&user_id) |
| 331 | .await |
| 332 | .expect("memberships.list_for_user"); |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | crate::domain::repository::SessionRepository::delete_expired( |
| 338 | &crate::infrastructure::repository::SqliteSessionRepo::new(pool.clone()), |
| 339 | std::time::SystemTime::UNIX_EPOCH, |
| 340 | ) |
| 341 | .await |
| 342 | .expect("sessions.delete_expired"); |
| 343 | |
| 344 | |
| 345 | let repos = SqliteRepoRepo::new(pool.clone()); |
| 346 | repos.find_by_id(&repo_id).await.expect("repos.find_by_id"); |
| 347 | repos |
| 348 | .find_by_org_and_name(&org_id, &RepoName::new("steid").expect("valid name")) |
| 349 | .await |
| 350 | .expect("repos.find_by_org_and_name"); |
| 351 | repos.list_by_org(&org_id).await.expect("repos.list_by_org"); |
| 352 | assert_eq!(repos.count().await.expect("repos.count"), 0); |
| 353 | |
| 354 | |
| 355 | let tokens = SqliteTokenRepo::new(pool.clone()); |
| 356 | tokens |
| 357 | .list_by_user(&user_id) |
| 358 | .await |
| 359 | .expect("tokens.list_by_user"); |
| 360 | |
| 361 | |
| 362 | let issues = SqliteIssueRepo::new(pool.clone()); |
| 363 | issues |
| 364 | .find_by_number(&repo_id, IssueNumber::new(1).expect("valid number")) |
| 365 | .await |
| 366 | .expect("issues.find_by_number"); |
| 367 | issues |
| 368 | .list_by_repo(&repo_id, IssueState::Open, 0, 10) |
| 369 | .await |
| 370 | .expect("issues.list_by_repo"); |
| 371 | issues.counts(&repo_id).await.expect("issues.counts"); |
| 372 | issues |
| 373 | .comment_counts(&repo_id) |
| 374 | .await |
| 375 | .expect("issues.comment_counts"); |
| 376 | |
| 377 | |
| 378 | let pulls = SqlitePullRequestRepo::new(pool.clone()); |
| 379 | pulls |
| 380 | .find_by_number(&repo_id, IssueNumber::new(1).expect("valid number")) |
| 381 | .await |
| 382 | .expect("pulls.find_by_number"); |
| 383 | pulls |
| 384 | .list_by_repo(&repo_id, PullRequestState::Open, 0, 10) |
| 385 | .await |
| 386 | .expect("pulls.list_by_repo"); |
| 387 | pulls.counts(&repo_id).await.expect("pulls.counts"); |
| 388 | pulls |
| 389 | .comment_counts(&repo_id) |
| 390 | .await |
| 391 | .expect("pulls.comment_counts"); |
| 392 | |
| 393 | |
| 394 | let pushes = SqlitePushEventRepo::new(pool.clone()); |
| 395 | pushes |
| 396 | .find(&PushEventId::from_trusted("p1")) |
| 397 | .await |
| 398 | .expect("pushes.find"); |
| 399 | pushes |
| 400 | .list_by_repo(&repo_id, 10) |
| 401 | .await |
| 402 | .expect("pushes.list_by_repo"); |
| 403 | pushes |
| 404 | .list_recent(std::slice::from_ref(&repo_id), 10) |
| 405 | .await |
| 406 | .expect("pushes.list_recent"); |
| 407 | |
| 408 | |
| 409 | let webhook_id = WebhookId::from_trusted("w1"); |
| 410 | let webhooks = SqliteWebhookRepo::new(pool.clone()); |
| 411 | webhooks.find(&webhook_id).await.expect("webhooks.find"); |
| 412 | webhooks |
| 413 | .list_by_repo(&repo_id) |
| 414 | .await |
| 415 | .expect("webhooks.list_by_repo"); |
| 416 | |
| 417 | let deliveries = SqliteWebhookDeliveryRepo::new(pool.clone()); |
| 418 | deliveries |
| 419 | .list_by_webhook(&webhook_id, 0, 10) |
| 420 | .await |
| 421 | .expect("deliveries.list_by_webhook"); |
| 422 | assert_eq!( |
| 423 | deliveries |
| 424 | .count_by_webhook(&webhook_id) |
| 425 | .await |
| 426 | .expect("deliveries.count_by_webhook"), |
| 427 | 0 |
| 428 | ); |
| 429 | deliveries |
| 430 | .latest_by_webhook(&webhook_id) |
| 431 | .await |
| 432 | .expect("deliveries.latest_by_webhook"); |
| 433 | |
| 434 | |
| 435 | let labels = SqliteLabelRepo::new(pool.clone()); |
| 436 | labels |
| 437 | .list_by_repo(&repo_id) |
| 438 | .await |
| 439 | .expect("labels.list_by_repo"); |
| 440 | labels |
| 441 | .find_by_name(&repo_id, "bug") |
| 442 | .await |
| 443 | .expect("labels.find_by_name"); |
| 444 | labels |
| 445 | .labels_for_repo(&repo_id) |
| 446 | .await |
| 447 | .expect("labels.labels_for_repo"); |
| 448 | labels |
| 449 | .labels_for_repo_pulls(&repo_id) |
| 450 | .await |
| 451 | .expect("labels.labels_for_repo_pulls"); |
| 452 | labels |
| 453 | .issue_counts(&repo_id) |
| 454 | .await |
| 455 | .expect("labels.issue_counts"); |
| 456 | labels |
| 457 | .pull_counts(&repo_id) |
| 458 | .await |
| 459 | .expect("labels.pull_counts"); |
| 460 | } |
| 461 | |
| 462 | #[test] |
| 463 | fn the_guard_catches_a_contracting_migration() { |
| 464 | |
| 465 | |
| 466 | |
| 467 | for contracting in [ |
| 468 | "alter table repositories drop column description;", |
| 469 | "drop table webhooks;", |
| 470 | "alter table orgs rename to organisations;", |
| 471 | "alter table users rename column email to address;", |
| 472 | ] { |
| 473 | let sql = statements_only(contracting); |
| 474 | assert!( |
| 475 | ["drop column", "drop table", "rename to", "rename column"] |
| 476 | .iter() |
| 477 | .any(|forbidden| sql.contains(forbidden)), |
| 478 | "the guard no longer recognises `{contracting}`" |
| 479 | ); |
| 480 | } |
| 481 | |
| 482 | let tightening = statements_only("alter table repositories add column stars integer not null;"); |
| 483 | assert!(tightening.contains("add column") && tightening.contains("not null")); |
| 484 | assert!( |
| 485 | !tightening.contains("default"), |
| 486 | "the guard no longer recognises a `not null` column with no default" |
| 487 | ); |
| 488 | } |
| 489 | |
| 490 | #[test] |
| 491 | fn prose_about_dropping_things_is_not_a_dropped_column() { |
| 492 | |
| 493 | |
| 494 | |
| 495 | let sql = statements_only( |
| 496 | "-- We could DROP COLUMN `bio` here, or RENAME TO something else, but\n\ |
| 497 | -- contracting waits a release. See architecture.md.\n\ |
| 498 | alter table orgs add column tagline text;\n", |
| 499 | ); |
| 500 | |
| 501 | assert_eq!(sql, "alter table orgs add column tagline text;"); |
| 502 | |
| 503 | |
| 504 | let sql = statements_only("insert into notes (body) values ('drop table orgs');"); |
| 505 | assert_eq!(sql, "insert into notes (body) values ();"); |
| 506 | } |