Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database consistency checks #837

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion app/server/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Database.PostgreSQL.Entity
import Database.PostgreSQL.Entity.DBT
import Database.PostgreSQL.Entity.Types (field)
import Database.PostgreSQL.Simple (Only (..))
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Effectful
import Effectful.Fail (runFailIO)
import Effectful.Log (Log, runLog)
Expand All @@ -34,6 +35,11 @@ import FloraWeb.Server
main :: IO ()
main = do
hSetBuffering stdout LineBuffering
preFlightChecks
runFlora

preFlightChecks :: IO ()
preFlightChecks = do
env <- getFloraEnv & runFailIO & runEff
runEff $ do
let withLogger = Logging.makeLogger env.mltp.logger
Expand All @@ -45,9 +51,78 @@ main = do
appLogger
Log.LogTrace
$ do
checkExpectedTables
checkRepositoriesAreConfigured
checkIfIndexRefreshJobIsPlanned env.pool
runFlora

checkExpectedTables :: (DB :> es, IOE :> es, Log :> es) => Eff es ()
checkExpectedTables = do
-- Update the list in alphabetical order when adding or removing a table!
let expectedTables =
Set.fromAscList
[ "affected_packages"
, "affected_version_ranges"
, "blob_relations"
, "categories"
, "downloads"
, "oddjobs"
, "organisations"
, "package_categories"
, "package_components"
, "package_group_packages"
, "package_groups"
, "package_indexes"
, "package_publishers"
, "packages"
, "persistent_sessions"
, "releases"
, "requirements"
, "security_advisories"
, "user_organisation"
, "users"
]
actualTables <-
dbtToEff $
Set.fromAscList . Vector.toList . Vector.map fromOnly
<$> query_
Select
[sql|
SELECT table_name
FROM information_schema.tables
WHERE table_name <> 'schema_migrations'
AND table_type = 'BASE TABLE'
AND EXISTS (SELECT TRUE
FROM unnest(current_schemas(FALSE)) AS cs
WHERE cs = table_schema)
ORDER BY table_name ASC
Comment on lines +90 to +97
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's the workhorse of the feature

|]
let unexpectedTables =
Set.difference
actualTables
expectedTables
let missingExpectedTables =
Set.difference
expectedTables
actualTables
let (messages :: Vector Text) =
let missingTableMessage =
if not $ null missingExpectedTables
then
"Database validation failed! Expected tables are missing: "
<> mconcat (List.intersperse ", " (Set.toList missingExpectedTables))
<> "."
else ""
unexpectedTableMessage =
if not $ null unexpectedTables
then
Text.pack "Database validation failed! Unexpected tables are present: "
<> mconcat (List.intersperse ", " (Set.toList unexpectedTables))
<> "."
else ""
in Vector.fromList $ filter (/= "") [missingTableMessage, unexpectedTableMessage]
unless (null messages) $ do
forM_ messages Log.logAttention_
liftIO exitFailure

checkRepositoriesAreConfigured :: (DB :> es, IOE :> es, Log :> es) => Eff es ()
checkRepositoriesAreConfigured = do
Expand Down
2 changes: 2 additions & 0 deletions changelog.d/flora-242
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
synopsis: Add database consistency checks
prs: #837
8 changes: 8 additions & 0 deletions scripts/get-app-tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SELECT table_name
FROM information_schema.tables
WHERE table_name <> 'schema_migrations'
AND table_type = 'BASE TABLE'
AND EXISTS (SELECT TRUE
FROM unnest(current_schemas(FALSE)) AS cs
WHERE cs = table_schema)
ORDER BY table_name ASC
Loading