graph/db+sqldb: make policy migration idempotent

Finally, we make the channel-policy part of the SQL migration idempotent
by adding a migration-only policy insert query which will not error out
if the policy already exists and does not have a timestamp that is newer
than the existing records timestamp. To keep the commit simple, a
insertChanEdgePolicyMig function is added which is basically identical
to the updateChanEdgePolicy function except for the fact that it uses
the newly added query. In the next commit, it will be simplified even
more.
This commit is contained in:
Elle Mouton committed 2025-09-01 08:06:12 +02:00
1 parent 8736fcafa8
commit 22bf88e900
6 files changed
+221 -23

No files matched your search

+4 -22
View File
@@ -105,15 +105,6 @@ func TestMigrateGraphToSQL(t *testing.T) {
write func(t *testing.T, db *KVStore, object any)
objects []any
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",
@@ -297,7 +288,6 @@ func TestMigrateGraphToSQL(t *testing.T) {
numChannels: 3,
numPolicies: 3,
},
expNotRetrySafety: true,
},
{
name: "prune log",
@@ -409,19 +399,11 @@ func TestMigrateGraphToSQL(t *testing.T) {
// Validate that the two databases are now in sync.
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.
// The migration should be retry-safe, so running it
// again should not change the state of the databases.
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)
}
require.NoError(t, err)
assertInSync(t, kvDB, sql, test.expGraphStats)
})
}
}