From 5819eb3dfc7e5e244af1619a95d83a74d41bda20 Mon Sep 17 00:00:00 2001 From: Cmdv Date: Tue, 21 May 2024 15:19:25 +0100 Subject: [PATCH] add ability to add/remove jsonb --- .../Unit/Babbage/CommandLineArg/ForceIndex.hs | 4 +- .../Unit/Conway/CommandLineArg/ForceIndex.hs | 4 +- cardano-db-sync/src/Cardano/DbSync.hs | 17 +++----- cardano-db-sync/src/Cardano/DbSync/Api.hs | 5 +++ .../Migration/Extra/JsonbInSchemaQueries.hs | 40 +++++++++++++++++++ 5 files changed, 55 insertions(+), 15 deletions(-) diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs index ac9cbaaea..ab70aae65 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Babbage/CommandLineArg/ForceIndex.hs @@ -19,7 +19,7 @@ checkForceIndexesArg = withCustomConfig commandLineForceIndexArgs Nothing babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do startDBSync dbSyncEnv threadDelay 3_000_000 - assertEqQuery dbSyncEnv DB.queryPgIndexesCount 168 "there wasn't the correct number of indexes" + assertEqQuery dbSyncEnv DB.queryPgIndexesCount 169 "there wasn't the correct number of indexes" where testLabel = "CLAcheckForceIndexesArg" commandLineForceIndexArgs = @@ -32,7 +32,7 @@ checkNoForceIndexesArg = withCustomConfigAndDropDB commandLineNoForceIndexArgs Nothing babbageConfigDir testLabel $ \_ _ dbSyncEnv -> do startDBSync dbSyncEnv threadDelay 3_000_000 - assertEqQuery dbSyncEnv DB.queryPgIndexesCount 103 "there wasn't the correct number of indexes" + assertEqQuery dbSyncEnv DB.queryPgIndexesCount 104 "there wasn't the correct number of indexes" where testLabel = "CLAcheckNoForceIndexesArg" commandLineNoForceIndexArgs = diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs index 05fb025ca..277aabd0b 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/CommandLineArg/ForceIndex.hs @@ -17,7 +17,7 @@ checkForceIndexesArg = startDBSync dbSync -- Verify number of DB indexes - assertEqQuery dbSync DB.queryPgIndexesCount 168 "unexpected number of indexes" + assertEqQuery dbSync DB.queryPgIndexesCount 169 "unexpected number of indexes" where cliArgs = initCommandLineArgs {claForceIndexes = True} testLabel = "conwayCLACheckForceIndexesArg" @@ -28,7 +28,7 @@ checkNoForceIndexesArg = startDBSync dbSync -- Verify number of DB indexes - assertEqQuery dbSync DB.queryPgIndexesCount 103 "unexpected number of indexes" + assertEqQuery dbSync DB.queryPgIndexesCount 104 "unexpected number of indexes" where cliArgs = initCommandLineArgs {claForceIndexes = False} testLabel = "conwayCLACheckNoForceIndexesArg" diff --git a/cardano-db-sync/src/Cardano/DbSync.hs b/cardano-db-sync/src/Cardano/DbSync.hs index 39da87942..2d8533266 100644 --- a/cardano-db-sync/src/Cardano/DbSync.hs +++ b/cardano-db-sync/src/Cardano/DbSync.hs @@ -175,17 +175,6 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc runOrThrowIO $ runExceptT $ do genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile resJsonbInSchema <- dbJsonbInSchema backend - - -- if the database has jsonb datatypes and the configuration does not have add_jsonb_to_schema enabled, then warn the user - when (resJsonbInSchema && not isJsonBInSchemaConfig) $ - liftIO $ - logAndThrowIO trce $ - SNErrJsonbInSchema - ( "The database has jsonb datatypes active. " - <> "Once jsonb datatypes have been enabled via `add_jsonb_to_schema` " - <> "configuration you need to make sure that configuration is not removed and always present." - ) - logProtocolMagicId trce $ genesisProtocolMagicId genCfg syncEnv <- ExceptT $ @@ -199,6 +188,12 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc syncNodeParams ranMigrations runMigrationFnc + + -- if the database has jsonb datatypes and the configuration does not have add_jsonb_to_schema enabled, then warn the user + when (resJsonbInSchema && not isJsonBInSchemaConfig) $ do + liftIO $ logWarning trce "The database has jsonb datatypes, but the configuration does not have add_jsonb_to_schema enabled. The all jsonb datatypes will be put back which can take time." + liftIO $ runDisableJsonbInSchema syncEnv + -- if the database doesn't have jsonb datatypes and the configuration does have add_jsonb_to_schema enabled, then add jsonb datatypes to the database when (not resJsonbInSchema && isJsonBInSchemaConfig) $ liftIO $ runEnableJsonbInSchema syncEnv liftIO $ runExtraMigrationsMaybe syncEnv diff --git a/cardano-db-sync/src/Cardano/DbSync/Api.hs b/cardano-db-sync/src/Cardano/DbSync/Api.hs index 2ef89b6a0..f2e8d33f5 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Api.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Api.hs @@ -27,6 +27,7 @@ module Cardano.DbSync.Api ( initPruneConsumeMigration, runExtraMigrationsMaybe, runEnableJsonbInSchema, + runDisableJsonbInSchema, getSafeBlockNoDiff, getPruneInterval, whenConsumeOrPruneTxOut, @@ -179,6 +180,10 @@ runEnableJsonbInSchema :: SyncEnv -> IO () runEnableJsonbInSchema syncEnv = void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.enableJsonbInSchema +runDisableJsonbInSchema :: SyncEnv -> IO () +runDisableJsonbInSchema syncEnv = + void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.disableJsonbInSchema + getSafeBlockNoDiff :: SyncEnv -> Word64 getSafeBlockNoDiff syncEnv = 2 * getSecurityParam syncEnv diff --git a/cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs b/cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs index 1b3b28a02..5eba5b4be 100644 --- a/cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs +++ b/cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs @@ -54,6 +54,46 @@ enableJsonbInSchema = do "ALTER TABLE off_chain_vote_data ALTER COLUMN json TYPE jsonb USING json::jsonb" [] +disableJsonbInSchema :: + forall m. + ( MonadBaseControl IO m + , MonadIO m + ) => + ReaderT SqlBackend m () +disableJsonbInSchema = do + handle exceptHandler $ + rawExecute + "ALTER TABLE 'tx_metadata' ALTER COLUMN 'json' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'script' ALTER COLUMN 'json' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'datum' ALTER COLUMN 'value' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'redeemer_data' ALTER COLUMN 'value' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'cost_model' ALTER COLUMN 'costs' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'gov_action_proposal' ALTER COLUMN 'description' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'off_chain_pool_data' ALTER COLUMN 'json' TYPE VARCHAR" + [] + handle exceptHandler $ + rawExecute + "ALTER TABLE 'off_chain_vote_data' ALTER COLUMN 'json' TYPE VARCHAR" + [] + queryJsonbInSchemaExists :: (MonadIO m) => ReaderT SqlBackend m Bool