Skip to content

Refactor database handling and enhance testing infrastructure#138

Merged
akiidjk merged 15 commits intodevfrom
feature/akiidjk-refactor-database
Mar 3, 2026
Merged

Refactor database handling and enhance testing infrastructure#138
akiidjk merged 15 commits intodevfrom
feature/akiidjk-refactor-database

Conversation

@akiidjk
Copy link
Collaborator

@akiidjk akiidjk commented Mar 2, 2026

This pull request migrates the old database implementation based on raw queries to a new implementation using sqlc, improving maintainability without losing performance. I also set up a command to test the server code using gotestsum.

Some client code was changed to consistently use the Flag struct from the server/database package instead of the previous ClientData struct from models. This is not ideal because some server components are called from the client codebase, but it is a temporary constraint caused by sqlc. I plan to improve this later. This change affects all major flag-handling logic, including parsing, submitting, and communication via HTTP and WebSockets. Additionally, there are some linter improvements and dependency updates.

I also started implementing a dependency injection pattern for the database, which is reflected in the API handling layer as well. The goal is to remove all global instances and variables across the codebase.

The ANALYSIS.md is slopped but reasonably good.

The tests are also slopped and not satisfactory. Next time I will write them manually. I forgot to write tests while migrating functions, which is my mistake and will not happen again.

Test coverage is approximately 89.5%.

Refactoring to use server/database.Flag for flag data

  • Replaced all instances of models.ClientData with database.Flag throughout the client codebase, including in function signatures, struct fields, and channels in files such as api.go, exploit.go, parse.go, run.go, submitter.go, and websocket-related files. This ensures a single source of truth for flag data structures and streamlines integration with the server. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14]

  • Updated imports across client files to reference server/database instead of models for flag-related functionality. [1] [2] [3] [4] [5] [6]

Project structure and configuration

  • Updated the Go workspace to use ./server/database instead of ./server/sqlite, reflecting the new location of shared data structures.

Linter and code quality improvements

  • Added dupl, zerologlint, and whitespace linters to .golangci.yml to improve code quality and catch duplication and formatting issues. [1] [2]

Dependency updates

  • Added new dependencies, including golang.org/x/exp, and updated several indirect dependencies in go.mod and go.work.sum files for various client and logger packages, ensuring compatibility and access to new features. [1] [2] [3] [4] [5] [6]

Minor logging improvement

  • Improved debug logging in run.go by adding a message to clarify when a command is prepared for execution.

Last things

  • The code is tested and works in the artificial testing environment, but i have not yet tested performance with benchmark and grafana
  • There a problem with sqlc + sqlite. in the add phase precisely with the AddFlag functions, sqlc with SQLite not support batch insert native, so for now we are obligated to use a for loop with an add inside (view for example line 176 in handling_api.go)

akiidjk and others added 15 commits February 27, 2026 19:39
Introduce server/database package with sqlc-generated queries,
DB connection and Store. Remove legacy server/sqlite implementation,
switch to database/sql with modernc.org/sqlite, and update imports
and go.work accordingly
- Remove pkg/models and update client and server code to use
  server/database.Flag
- Introduce DB Store and Runner; wire store into startup, API handlers
  and core loops
- Update sqlc config, queries and DB models (JSON tags and type
  overrides)
- Add API/websocket request types and minor handler/route refactors
Adjust demo/scripts/setup.sh to use the demo-relative venv and
flagchecker paths and remove the tests dir cd. Update justfile working
directories and server bin dir to match the new layout, and add a
generate target to run sqlc.
Replace the old client-test target with server-test running in
cookiefarm/server.
Use gotestsum with testdox, generate coverage.out and coverage.html,
open the
report, and send a desktop notification on successful test completion.
Update
the nearby comment to "Test client binaries".
Add the following linters:
- dupl
- zerologlint
- whitespace
Introduce FlagCollector, mappers, queries and store helpers
Add schema.sql and extensive unit tests for connection, queries,
mapper, collector and store, and include generated coverage report
Convert TeamID fields to int64 across server and client, adding
casts where needed. Move API request types into pkg/models and
update client usage. Add a buffered FlagCollector singleton to batch
DB writes with Start/Stop/Flush methods. Embed DB schema and update
sqlc config; add mapper helpers and CountFlags query. Misc:
change DSN to cookiefarm.db, adjust route handlers, and update some
logging and module/sum entries.
Update server-test in justfile to create ./coverage and write
coverage.out and coverage.html into that directory. Remove the
legacy cookiefarm/server/coverage.html tracked file.
Add comprehensive coverage_test.go to exercise DB transaction paths,
QueryContext/ExecContext error branches, Store.WithTx, NewDB edge cases,
and FlagCollector timer/flush behaviors (including buffer-overflow
drop).
Introduce rowsAffectedErrDB and blockingFailStore helpers to simulate
error
and blocking conditions.

Set fc.running = false in FlagCollector.Stop so the background goroutine
observes the stopped state before stats are snapshotted, avoiding a
race.
@akiidjk akiidjk self-assigned this Mar 2, 2026
@akiidjk akiidjk added documentation Improvements or additions to documentation enhancement New feature or request dependencies Pull requests that update a dependency file go Pull requests that update go code labels Mar 2, 2026
@akiidjk
Copy link
Collaborator Author

akiidjk commented Mar 2, 2026

I hope I haven't forgotten anything.

@KronosPNG don't forget to fix/add the rules for SonarQube

@akiidjk akiidjk changed the base branch from main to dev March 2, 2026 16:26
Copy link
Contributor

@vympel7 vympel7 left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@giovanni-iannaccone giovanni-iannaccone left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@KronosPNG KronosPNG left a comment

Choose a reason for hiding this comment

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

LGTM

@KronosPNG
Copy link
Collaborator

I hope I haven't forgotten anything.

@KronosPNG don't forget to fix/add the rules for SonarQube

Pushed on feature/sonarqube_ruleset_tweak1 the new ruleset, for now coverage is ignored until we make proper tests

@akiidjk akiidjk merged commit fe52984 into dev Mar 3, 2026
2 checks passed
@akiidjk akiidjk deleted the feature/akiidjk-refactor-database branch March 4, 2026 13:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file documentation Improvements or additions to documentation enhancement New feature or request go Pull requests that update go code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants