Skip to content

Commit 09919ac

Browse files
Cmdvkderme
authored andcommitted
add ability to add/remove jsonb
1 parent 2ddc5d2 commit 09919ac

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,6 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
175175
runOrThrowIO $ runExceptT $ do
176176
genCfg <- readCardanoGenesisConfig syncNodeConfigFromFile
177177
resJsonbInSchema <- dbJsonbInSchema backend
178-
179-
-- if the database has jsonb datatypes and the configuration does not have add_jsonb_to_schema enabled, then warn the user
180-
when (resJsonbInSchema && not isJsonBInSchemaConfig) $
181-
liftIO $
182-
logAndThrowIO trce $
183-
SNErrJsonbInSchema
184-
( "The database has jsonb datatypes active. "
185-
<> "Once jsonb datatypes have been enabled via `add_jsonb_to_schema` "
186-
<> "configuration you need to make sure that configuration is not removed and always present."
187-
)
188-
189178
logProtocolMagicId trce $ genesisProtocolMagicId genCfg
190179
syncEnv <-
191180
ExceptT $
@@ -199,6 +188,12 @@ runSyncNode metricsSetters trce iomgr dbConnString ranMigrations runMigrationFnc
199188
syncNodeParams
200189
ranMigrations
201190
runMigrationFnc
191+
192+
-- if the database has jsonb datatypes and the configuration does not have add_jsonb_to_schema enabled, then warn the user
193+
when (resJsonbInSchema && not isJsonBInSchemaConfig) $ do
194+
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."
195+
liftIO $ runDisableJsonbInSchema syncEnv
196+
202197
-- 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
203198
when (not resJsonbInSchema && isJsonBInSchemaConfig) $ liftIO $ runEnableJsonbInSchema syncEnv
204199
liftIO $ runExtraMigrationsMaybe syncEnv

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ module Cardano.DbSync.Api (
2727
initPruneConsumeMigration,
2828
runExtraMigrationsMaybe,
2929
runEnableJsonbInSchema,
30+
runDisableJsonbInSchema,
3031
getSafeBlockNoDiff,
3132
getPruneInterval,
3233
whenConsumeOrPruneTxOut,
@@ -180,6 +181,10 @@ runEnableJsonbInSchema :: SyncEnv -> IO ()
180181
runEnableJsonbInSchema syncEnv =
181182
void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.enableJsonbInSchema
182183

184+
runDisableJsonbInSchema :: SyncEnv -> IO ()
185+
runDisableJsonbInSchema syncEnv =
186+
void $ DB.runDbIohkNoLogging (envBackend syncEnv) DB.disableJsonbInSchema
187+
183188
getSafeBlockNoDiff :: SyncEnv -> Word64
184189
getSafeBlockNoDiff syncEnv = 2 * getSecurityParam syncEnv
185190

cardano-db/src/Cardano/Db/Migration/Extra/JsonbInSchemaQueries.hs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ enableJsonbInSchema = do
5454
"ALTER TABLE off_chain_vote_data ALTER COLUMN json TYPE jsonb USING json::jsonb"
5555
[]
5656

57+
disableJsonbInSchema ::
58+
forall m.
59+
( MonadBaseControl IO m
60+
, MonadIO m
61+
) =>
62+
ReaderT SqlBackend m ()
63+
disableJsonbInSchema = do
64+
handle exceptHandler $
65+
rawExecute
66+
"ALTER TABLE 'tx_metadata' ALTER COLUMN 'json' TYPE VARCHAR"
67+
[]
68+
handle exceptHandler $
69+
rawExecute
70+
"ALTER TABLE 'script' ALTER COLUMN 'json' TYPE VARCHAR"
71+
[]
72+
handle exceptHandler $
73+
rawExecute
74+
"ALTER TABLE 'datum' ALTER COLUMN 'value' TYPE VARCHAR"
75+
[]
76+
handle exceptHandler $
77+
rawExecute
78+
"ALTER TABLE 'redeemer_data' ALTER COLUMN 'value' TYPE VARCHAR"
79+
[]
80+
handle exceptHandler $
81+
rawExecute
82+
"ALTER TABLE 'cost_model' ALTER COLUMN 'costs' TYPE VARCHAR"
83+
[]
84+
handle exceptHandler $
85+
rawExecute
86+
"ALTER TABLE 'gov_action_proposal' ALTER COLUMN 'description' TYPE VARCHAR"
87+
[]
88+
handle exceptHandler $
89+
rawExecute
90+
"ALTER TABLE 'off_chain_pool_data' ALTER COLUMN 'json' TYPE VARCHAR"
91+
[]
92+
handle exceptHandler $
93+
rawExecute
94+
"ALTER TABLE 'off_chain_vote_data' ALTER COLUMN 'json' TYPE VARCHAR"
95+
[]
96+
5797
queryJsonbInSchemaExists ::
5898
(MonadIO m) =>
5999
ReaderT SqlBackend m Bool

schema/migration-2-0040-20240513.sql renamed to schema/migration-2-0041-20240521.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ DECLARE
55
next_version int ;
66
BEGIN
77
SELECT stage_two + 1 INTO next_version FROM schema_version ;
8-
IF next_version = 40 THEN
8+
IF next_version = 41 THEN
99
EXECUTE 'ALTER TABLE "tx_metadata" ALTER COLUMN "json" TYPE VARCHAR' ;
1010
EXECUTE 'ALTER TABLE "script" ALTER COLUMN "json" TYPE VARCHAR' ;
1111
EXECUTE 'ALTER TABLE "datum" ALTER COLUMN "value" TYPE VARCHAR' ;

0 commit comments

Comments
 (0)