graph/db: let migration test test retry safety

Currently, the graph SQL migration is not retry safe. Meaning that if
the source DB exeutes a retry under the hood, this could result in the
migration failing. In preparation for fixing this, we adust the
migration test accordingly.
This commit is contained in:
Elle Mouton
2025-08-15 09:00:29 +02:00
parent 849d81c691
commit 5fa5ca1073

View File

@@ -56,6 +56,13 @@ var (
// for to ensure that our migration from a graph store backed by a KV DB to a // for to ensure that our migration from a graph store backed by a KV DB to a
// SQL database works as expected. At the end of each test, the DBs are compared // SQL database works as expected. At the end of each test, the DBs are compared
// and expected to have the exact same data in them. // and expected to have the exact same data in them.
// This test also ensures that the migration is "retry-safe". This is needed
// because the migration is hooked up to 2 dbs: the source DB and the
// destination. The source DB is a db behind the kvdb.Backend interface and
// the migration makes use of methods on this interface that may be retried
// under the hood. The migration often does logic inside call-back functions
// passed to the source DB methods which may be retried, and so we need to
// ensure that the migration can handle this.
func TestMigrateGraphToSQL(t *testing.T) { func TestMigrateGraphToSQL(t *testing.T) {
t.Parallel() t.Parallel()
ctx := context.Background() ctx := context.Background()
@@ -98,6 +105,15 @@ func TestMigrateGraphToSQL(t *testing.T) {
write func(t *testing.T, db *KVStore, object any) write func(t *testing.T, db *KVStore, object any)
objects []any objects []any
expGraphStats graphStats expGraphStats graphStats
// expNotRetrySafety is true if we expect an error to occur for
// the test if the migration is run twice. In other-words, if
// the specific case in question is currently not idempotent.
//
// NOTE: we want _all_ the cases here to be idempotent, so this
// is a temporary field which will be removed once we have
// properly made the migration retry-safe.
expNotRetrySafety bool
}{ }{
{ {
name: "empty", name: "empty",
@@ -140,6 +156,7 @@ func TestMigrateGraphToSQL(t *testing.T) {
expGraphStats: graphStats{ expGraphStats: graphStats{
numNodes: 6, numNodes: 6,
}, },
expNotRetrySafety: true,
}, },
{ {
name: "source node", name: "source node",
@@ -157,6 +174,7 @@ func TestMigrateGraphToSQL(t *testing.T) {
numNodes: 1, numNodes: 1,
srcNodeSet: true, srcNodeSet: true,
}, },
expNotRetrySafety: true,
}, },
{ {
name: "channels and policies", name: "channels and policies",
@@ -229,6 +247,7 @@ func TestMigrateGraphToSQL(t *testing.T) {
numChannels: 3, numChannels: 3,
numPolicies: 3, numPolicies: 3,
}, },
expNotRetrySafety: true,
}, },
{ {
name: "prune log", name: "prune log",
@@ -339,6 +358,20 @@ func TestMigrateGraphToSQL(t *testing.T) {
// Validate that the two databases are now in sync. // Validate that the two databases are now in sync.
assertInSync(t, kvDB, sql, test.expGraphStats) assertInSync(t, kvDB, sql, test.expGraphStats)
// NOTE: for now, not all the cases in the test are
// retry safe! The aim is to completely remove this
// field once we have made the migration retry-safe.
err = MigrateGraphToSQL(ctx, sql.cfg, kvDB.db, sql.db)
if !test.expNotRetrySafety {
// The migration should be retry-safe, so
// running it again should not change the state
// of the databases.
require.NoError(t, err)
assertInSync(t, kvDB, sql, test.expGraphStats)
} else {
require.Error(t, err)
}
}) })
} }
} }