@@ -43,6 +43,11 @@ func MigrateGraphToSQL(ctx context.Context, kvBackend kvdb.Backend,
43
43
return fmt .Errorf ("could not migrate nodes: %w" , err )
44
44
}
45
45
46
+ // 2) Migrate the source node.
47
+ if err := migrateSourceNode (ctx , kvBackend , sqlDB ); err != nil {
48
+ return fmt .Errorf ("could not migrate source node: %w" , err )
49
+ }
50
+
46
51
log .Infof ("Finished migration of the graph store from KV to SQL in %v" ,
47
52
time .Since (t0 ))
48
53
@@ -184,3 +189,75 @@ func migrateNodes(ctx context.Context, kvBackend kvdb.Backend,
184
189
185
190
return nil
186
191
}
192
+
193
+ // migrateSourceNode migrates the source node from the KV backend to the
194
+ // SQL database.
195
+ func migrateSourceNode (ctx context.Context , kvdb kvdb.Backend ,
196
+ sqlDB SQLQueries ) error {
197
+
198
+ sourceNode , err := sourceNode (kvdb )
199
+ if errors .Is (err , ErrSourceNodeNotSet ) {
200
+ // If the source node has not been set yet, we can skip this
201
+ // migration step.
202
+ return nil
203
+ } else if err != nil {
204
+ return fmt .Errorf ("could not get source node from kv " +
205
+ "store: %w" , err )
206
+ }
207
+
208
+ pub := sourceNode .PubKeyBytes
209
+
210
+ // Get the DB ID of the source node by its public key. This node must
211
+ // already exist in the SQL database, as it should have been migrated
212
+ // in the previous node-migration step.
213
+ id , err := sqlDB .GetNodeIDByPubKey (
214
+ ctx , sqlc.GetNodeIDByPubKeyParams {
215
+ PubKey : pub [:],
216
+ Version : int16 (ProtocolV1 ),
217
+ },
218
+ )
219
+ if err != nil {
220
+ return fmt .Errorf ("could not get source node ID: %w" , err )
221
+ }
222
+
223
+ // Now we can add the source node to the SQL database.
224
+ err = sqlDB .AddSourceNode (ctx , id )
225
+ if err != nil {
226
+ return fmt .Errorf ("could not add source node to SQL store: %w" ,
227
+ err )
228
+ }
229
+
230
+ // Verify that the source node was added correctly by fetching it back
231
+ // from the SQL database and checking that the expected DB ID and
232
+ // pub key are returned. We don't need to do a whole node comparison
233
+ // here, as this was already done in the previous migration step.
234
+ srcNodes , err := sqlDB .GetSourceNodesByVersion (ctx , int16 (ProtocolV1 ))
235
+ if err != nil {
236
+ return fmt .Errorf ("could not get source nodes from SQL " +
237
+ "store: %w" , err )
238
+ }
239
+
240
+ // The SQL store has support for multiple source nodes (for future
241
+ // protocol versions) but this migration is purely aimed at the V1
242
+ // store, and so we expect exactly one source node to be present.
243
+ if len (srcNodes ) != 1 {
244
+ return fmt .Errorf ("expected exactly one source node, " +
245
+ "got %d" , len (srcNodes ))
246
+ }
247
+
248
+ // Check that the source node ID and pub key match the original
249
+ // source node.
250
+ if srcNodes [0 ].NodeID != id {
251
+ return fmt .Errorf ("source node ID mismatch after migration: " +
252
+ "expected %d, got %d" , id , srcNodes [0 ].NodeID )
253
+ }
254
+ err = sqldb .CompareRecords (pub [:], srcNodes [0 ].PubKey , "source node" )
255
+ if err != nil {
256
+ return fmt .Errorf ("source node pubkey mismatch after " +
257
+ "migration: %w" , err )
258
+ }
259
+
260
+ log .Infof ("Migrated source node with pubkey %x to SQL" , pub [:])
261
+
262
+ return nil
263
+ }
0 commit comments