Skip to content

Commit

Permalink
db: Filter out all generated foreign key constraints
Browse files Browse the repository at this point in the history
While testing it was found that if all foreign key constraints were
removed from the generated schema migrations, total chain sync time
on mainnet dropped from 6 days to a little over 3 days.

These constraints do very little to improve data integrity of the data
in the database because they are already checked in Haskell land. They
can therefore be dropped.

The Haskell land code will however need to be modified/updated to work
around the lack of these constraints. For example removing these
foreign key constraints means that `OnDeleteCascade` will no longer
work and will therefore have to be done manually. This is a small price
to pay for the sync speed improvements.
  • Loading branch information
erikd committed May 13, 2022
1 parent d2b5b6e commit 254e00b
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion cardano-db/src/Cardano/Db/Migration.hs
Expand Up @@ -169,7 +169,7 @@ createMigration source (MigrationDir migdir) = do
create :: ReaderT SqlBackend (NoLoggingT IO) (Maybe (MigrationVersion, Text))
create = do
ver <- getSchemaVersion
statements <- getMigration migrateCardanoDb
statements <- filterMigrations <$> getMigration migrateCardanoDb
if null statements
then pure Nothing
else do
Expand Down Expand Up @@ -212,6 +212,14 @@ createMigration source (MigrationDir migdir) = do
let (SchemaVersion _ stage2 _) = entityVal x
pure $ MigrationVersion 2 stage2 0

filterMigrations :: [Text] -> [Text]
filterMigrations =
filter keepMigration
where
keepMigration :: Text -> Bool
keepMigration txt =
not (Text.isInfixOf "ADD CONSTRAINT" txt && Text.isInfixOf "FOREIGN KEY" txt && Text.isInfixOf "REFERENCES" txt)

recreateDB :: PGPassSource -> IO ()
recreateDB pgpass = do
runWithConnectionNoLogging pgpass $ do
Expand Down

0 comments on commit 254e00b

Please sign in to comment.