Skip to content

Commit

Permalink
add ability to add/remove jsonb
Browse files Browse the repository at this point in the history
  • Loading branch information
Cmdv committed May 21, 2024
1 parent c27c4e4 commit 3c2bff3
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
17 changes: 6 additions & 11 deletions cardano-db-sync/src/Cardano/DbSync.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 $
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions cardano-db-sync/src/Cardano/DbSync/Api.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ module Cardano.DbSync.Api (
initPruneConsumeMigration,
runExtraMigrationsMaybe,
runEnableJsonbInSchema,
runDisableJsonbInSchema,
getSafeBlockNoDiff,
getPruneInterval,
whenConsumeOrPruneTxOut,
Expand Down Expand Up @@ -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

Expand Down
40 changes: 40 additions & 0 deletions cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ DECLARE
next_version int ;
BEGIN
SELECT stage_two + 1 INTO next_version FROM schema_version ;
IF next_version = 40 THEN
IF next_version = 41 THEN
EXECUTE 'ALTER TABLE "tx_metadata" ALTER COLUMN "json" TYPE VARCHAR' ;
EXECUTE 'ALTER TABLE "script" ALTER COLUMN "json" TYPE VARCHAR' ;
EXECUTE 'ALTER TABLE "datum" ALTER COLUMN "value" TYPE VARCHAR' ;
Expand Down

0 comments on commit 3c2bff3

Please sign in to comment.