@@ -17,6 +17,7 @@ module Cardano.DbSync (
1717 SocketPath (.. ),
1818 Db. MigrationDir (.. ),
1919 runDbSyncNode ,
20+ runMigrationsOnly ,
2021 runDbSync ,
2122 -- For testing and debugging
2223 OffChainFetchError (.. ),
@@ -67,19 +68,19 @@ runDbSyncNode metricsSetters knownMigrations params syncNodeConfigFromFile =
6768 abortOnPanic <- hasAbortOnPanicEnv
6869 startupReport trce abortOnPanic params
6970
70- runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFile abortOnPanic
71+ -- Run initial migrations synchronously first
72+ runMigrationsOnly knownMigrations trce params syncNodeConfigFromFile
7173
72- runDbSync ::
73- MetricSetters ->
74+ runDbSync metricsSetters iomgr trce params syncNodeConfigFromFile abortOnPanic
75+
76+ -- Extract just the initial migration logic (no indexes)
77+ runMigrationsOnly ::
7478 [(Text , Text )] ->
75- IOManager ->
7679 Trace IO Text ->
7780 SyncNodeParams ->
7881 SyncNodeConfig ->
79- -- Should abort on panic
80- Bool ->
8182 IO ()
82- runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFile abortOnPanic = do
83+ runMigrationsOnly knownMigrations trce params syncNodeConfigFromFile = do
8384 logInfo trce $ textShow syncOpts
8485
8586 -- Read the PG connection info
@@ -97,9 +98,11 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
9798 msg <- Db. getMaintenancePsqlConf pgConfig
9899 logInfo trce $ " Running database migrations in mode " <> textShow mode
99100 logInfo trce msg
100- when (mode `elem` [ Db. Indexes , Db. Full ]) $ logWarning trce indexesMsg
101+ -- No index warning here - runMigrationsOnly never runs indexes
101102 Db. runMigrations pgConfig True dbMigrationDir (Just $ Db. LogFileDir " /tmp" ) mode (txOutConfigToTableType txOutConfig)
102- (ranMigrations, unofficial) <- if enpForceIndexes params then runMigration Db. Full else runMigration Db. Initial
103+
104+ -- Always run Initial mode only - never indexes
105+ (ranMigrations, unofficial) <- runMigration Db. Initial
103106 unless (null unofficial) $
104107 logWarning trce $
105108 " Unofficial migration scripts found: "
@@ -109,9 +112,27 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
109112 then logInfo trce " All migrations were executed"
110113 else logInfo trce " Some migrations were not executed. They need to run when syncing has started."
111114
112- if enpForceIndexes params
113- then logInfo trce " All user indexes were created"
114- else logInfo trce " New user indexes were not created. They may be created later if necessary."
115+ logInfo trce " New user indexes were not created. They may be created later if necessary."
116+ where
117+ dbMigrationDir :: Db. MigrationDir
118+ dbMigrationDir = enpMigrationDir params
119+ syncOpts = extractSyncOptions params False syncNodeConfigFromFile
120+ txOutConfig = sioTxOut $ dncInsertOptions syncNodeConfigFromFile
121+
122+ runDbSync ::
123+ MetricSetters ->
124+ IOManager ->
125+ Trace IO Text ->
126+ SyncNodeParams ->
127+ SyncNodeConfig ->
128+ -- Should abort on panic
129+ Bool ->
130+ IO ()
131+ runDbSync metricsSetters iomgr trce params syncNodeConfigFromFile abortOnPanic = do
132+ logInfo trce $ textShow syncOpts
133+
134+ -- Read the PG connection info
135+ pgConfig <- runOrThrowIO (Db. readPGPass $ enpPGPassSource params)
115136
116137 dbConnectionSetting <- case Db. toConnectionSetting pgConfig of
117138 Left err -> do
@@ -123,20 +144,30 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
123144 -- For testing and debugging.
124145 whenJust (enpMaybeRollback params) $ \ slotNo ->
125146 void $ unsafeRollback trce (txOutConfigToTableType txOutConfig) pgConfig slotNo
147+
148+ -- This runMigration is ONLY for delayed migrations during sync (like indexes)
149+ let runDelayedMigration mode = do
150+ msg <- Db. getMaintenancePsqlConf pgConfig
151+ logInfo trce $ " Running database migrations in mode " <> textShow mode
152+ logInfo trce msg
153+ when (mode `elem` [Db. Indexes , Db. Full ]) $ logWarning trce indexesMsg
154+ Db. runMigrations pgConfig True dbMigrationDir (Just $ Db. LogFileDir " /tmp" ) mode (txOutConfigToTableType txOutConfig)
155+
126156 runSyncNode
127157 metricsSetters
128158 trce
129159 iomgr
130160 dbConnectionSetting
131- (void . runMigration )
161+ (void . runDelayedMigration )
132162 syncNodeConfigFromFile
133163 params
134164 syncOpts
135165 where
136166 dbMigrationDir :: Db. MigrationDir
137167 dbMigrationDir = enpMigrationDir params
168+ syncOpts = extractSyncOptions params abortOnPanic syncNodeConfigFromFile
169+ txOutConfig = sioTxOut $ dncInsertOptions syncNodeConfigFromFile
138170
139- indexesMsg :: Text
140171 indexesMsg =
141172 mconcat
142173 [ " Creating Indexes. This may require an extended period of time to perform."
@@ -146,10 +177,6 @@ runDbSync metricsSetters knownMigrations iomgr trce params syncNodeConfigFromFil
146177 , " in the schema directory and restart it."
147178 ]
148179
149- syncOpts = extractSyncOptions params abortOnPanic syncNodeConfigFromFile
150-
151- txOutConfig = sioTxOut $ dncInsertOptions syncNodeConfigFromFile
152-
153180runSyncNode ::
154181 MetricSetters ->
155182 Trace IO Text ->
@@ -162,7 +189,7 @@ runSyncNode ::
162189 SyncNodeParams ->
163190 SyncOptions ->
164191 IO ()
165- runSyncNode metricsSetters trce iomgr dbConnSetting runMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
192+ runSyncNode metricsSetters trce iomgr dbConnSetting runDelayedMigrationFnc syncNodeConfigFromFile syncNodeParams syncOptions = do
166193 whenJust maybeLedgerDir $
167194 \ enpLedgerStateDir -> do
168195 createDirectoryIfMissing True (unLedgerStateDir enpLedgerStateDir)
@@ -190,12 +217,11 @@ runSyncNode metricsSetters trce iomgr dbConnSetting runMigrationFnc syncNodeConf
190217 mkSyncEnvFromConfig
191218 trce
192219 dbEnv
193- -- dbConnString
194220 syncOptions
195221 genCfg
196222 syncNodeConfigFromFile
197223 syncNodeParams
198- runMigrationFnc
224+ runDelayedMigrationFnc
199225
200226 -- Warn the user that jsonb datatypes are being removed from the database schema.
201227 when (isJsonbInSchema && removeJsonbFromSchemaConfig) $ do
0 commit comments