//// The readiness schema probe against a live Postgres, gated on //// `DATABASE_URL` (a no-op when unset). This is the only check that the //// hand-maintained `required_tables` lists still name the tables the stores' //// migrations actually create. import crate_server/catalog_index_postgres import crate_server/follow_index_postgres import crate_server/known_users_postgres import crate_server/oauth/sessions_postgres import crate_server/readiness import crate_server/shelf_index_postgres import envoy import exception import gleam/erlang/process import gleam/option.{None, Some} import pog fn with_migrated_conn(run: fn(pog.Connection) -> Nil) -> Nil { case envoy.get("DATABASE_URL") { Error(Nil) -> Nil Ok(url) -> { let assert Ok(#(conn, pid)) = sessions_postgres.connect_test_pool(url) // `pog.start` links the pool to its caller and `kill`'s exit signal is // untrappable, so unlink first or this test process dies with the pool. use <- exception.defer(fn() { process.unlink(pid) process.kill(pid) }) migrate_every_store(conn) run(conn) } } } fn migrate_every_store(conn: pog.Connection) -> Nil { let assert Ok(_) = sessions_postgres.table_store( conn, sessions_postgres.oauth_sessions_table, Some(3600), ) let assert Ok(_) = sessions_postgres.table_store( conn, sessions_postgres.discogs_creds_table, None, ) let assert Ok(_) = known_users_postgres.table_store(conn) let assert Ok(_) = catalog_index_postgres.table_store(conn) let assert Ok(_) = shelf_index_postgres.table_store(conn) let assert Ok(_) = follow_index_postgres.table_store(conn) Nil } pub fn the_schema_probe_passes_against_the_migrated_schema_test() { use conn <- with_migrated_conn() assert readiness.schema_readiness(conn, readiness.schema_check_sql()) == readiness.Ready } pub fn the_schema_probe_reports_down_for_a_table_migrate_never_created_test() { use conn <- with_migrated_conn() let sql = readiness.schema_check_sql_for(["crate_no_such_table"]) let assert readiness.Down(_) = readiness.schema_readiness(conn, sql) Nil } // A query that errors instead of answering: the same shape a checkout timeout // against a busy pool has, and equally not proof of anything. pub fn a_failed_query_is_unknown_rather_than_down_test() { use conn <- with_migrated_conn() let assert readiness.Unknown(_) = readiness.schema_readiness(conn, "select no_such_column") Nil }