Skip to content

[sql-60] Add privacy mapper kvdb->SQL migration itest#1218

Open
ViktorT-11 wants to merge 1 commit intolightninglabs:migration-itest-kv-storesfrom
ViktorT-11:2025-01-privacy-mapper-migration-itest
Open

[sql-60] Add privacy mapper kvdb->SQL migration itest#1218
ViktorT-11 wants to merge 1 commit intolightninglabs:migration-itest-kv-storesfrom
ViktorT-11:2025-01-privacy-mapper-migration-itest

Conversation

@ViktorT-11
Copy link
Contributor

Based on #1217

Implements part of step 6. of "Phase 3" in #917.

This PR implements itest coverage for the kvdb->SQL migration of privacy mapper values.

Implement itest coverage for migrating privacy mapper pairs data from
kvdb to sql.
The itest coverage mimics the unit tests for privacy mapper in
firewalldb/sql_migration_test.go, except for the randomized migration
tests.

This coverage ensures that the full kvdb->SQL migration flow for litd
works as expected for the privacy mapper data.
@ViktorT-11 ViktorT-11 added the no-changelog This PR is does not require a release notes entry label Jan 29, 2026
@gemini-code-assist
Copy link

Summary of Changes

Hello @ViktorT-11, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the integration test suite by adding dedicated coverage for the migration of privacy mapper data from the key-value database (KVDB) to a SQL database. This ensures that the critical privacy mapping functionality remains intact and correctly migrates during the broader database transition, contributing to the overall reliability and data integrity of the system.

Highlights

  • Integration Test Expansion: The existing testKvdbSQLMigration integration test has been extended to include comprehensive coverage for the privacy mapper KVDB to SQL migration.
  • New Data Structures for Privacy Mapping: Introduced firewallPrivacyPairMigrationData and updated firewallMigrationData to properly encapsulate privacy mapper entries for testing purposes.
  • Privacy Mapper Data Seeding: A new helper function, setupFirewallPrivacyMapperMigrationData, has been added to generate various privacy mapper scenarios, including single, multiple, and deleted session entries, for robust migration testing.
  • Comprehensive Assertion Logic: New assertion functions (assertFirewallPrivacyMapperMigrationDataBolt, assertFirewallPrivacyPairsBolt, assertPrivacyPairsViaLitCLI, assertFirewallPrivacyMapperMigrationDataSQL) have been implemented to validate the correctness of privacy mapper data after migration in both BoltDB and SQL databases, and through litcli commands.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds valuable integration test coverage for the kvdb to SQL migration of privacy mapper values. The test cases are comprehensive and cover various scenarios, including single and multiple pairs, as well as handling of deleted sessions. The overall implementation is solid. I've identified a couple of areas for improvement related to database transaction efficiency and error handling within the test logic, which I've detailed in the specific comments.

Comment on lines +1409 to +1431
addPrivacyPairs := func(groupID session.ID, numPairs int) error {
pairs := make(map[string]string)
for i := 0; i < numPairs; i++ {
realKey := fmt.Sprintf("real-%d", i)
pseudoKey := fmt.Sprintf("pseudo-%d", i)
err := firewallStore.PrivacyDB(groupID).Update(
ctx, func(ctx context.Context,
tx firewalldb.PrivacyMapTx) error {

return tx.NewPair(
ctx, realKey, pseudoKey,
)
},
)
if err != nil {
return err
}
pairs[realKey] = pseudoKey
}

data.privPairs[groupID] = pairs
return nil
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The addPrivacyPairs function currently opens a new database transaction for each privacy pair it adds by calling Update inside a loop. This is inefficient, especially when adding multiple pairs. It's better to perform all NewPair calls within a single Update transaction to reduce database overhead.

addPrivacyPairs := func(groupID session.ID, numPairs int) error {
			pairs := make(map[string]string)
			err := firewallStore.PrivacyDB(groupID).Update(
				ctx, func(ctx context.Context,
					tx firewalldb.PrivacyMapTx) error {

					for i := 0; i < numPairs; i++ {
						realKey := fmt.Sprintf("real-%d", i)
						pseudoKey := fmt.Sprintf("pseudo-%d", i)
						err := tx.NewPair(
							ctx, realKey, pseudoKey,
						)
						if err != nil {
							return err
						}
						pairs[realKey] = pseudoKey
					}

					return nil
				},
			)
			if err != nil {
				return err
			}

			data.privPairs[groupID] = pairs
			return nil
		}

Comment on lines +2537 to +2539
if err == sql.ErrNoRows {
continue
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This check for sql.ErrNoRows is incorrect for a sqlc :many query like GetAllPrivacyPairs. These queries return an empty slice and a nil error if no rows are found, not sql.ErrNoRows. This if block is effectively dead code and can be removed. The require.NoError call on the next line is sufficient to handle any actual database errors.

@lightninglabs-deploy
Copy link

@ViktorT-11, remember to re-request review from reviewers when ready

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no-changelog This PR is does not require a release notes entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants