Skip to content

Conversation

@adamoutler
Copy link
Collaborator

@adamoutler adamoutler commented Feb 7, 2026

πŸ“Œ Description

Add separate app.sql. Re-reference from scripts.


πŸ” Related Issues


πŸ“‹ Type of Change

Please check the relevant option(s):

  • πŸ› Bug fix
  • ✨ New feature
  • ♻️ Code refactor
  • πŸ“š Documentation update
  • πŸ§ͺ Test addition or change
  • πŸ”§ Build/config update
  • πŸš€ Performance improvement
  • πŸ”¨ CI/CD or automation
  • 🧹 Cleanup / chore

πŸ“· Screenshots or Logs (if applicable)


πŸ§ͺ Testing Steps

Full suite
image


βœ… Checklist

  • I have read the Contribution Guidelines
  • I have tested my changes locally
  • I have updated relevant documentation (if applicable)
  • I have verified my changes do not break existing behavior
  • I am willing to respond to requested changes and feedback

πŸ™‹ Additional Notes

Summary by CodeRabbit

  • Refactor

    • Reorganized database initialization to use an external schema file for improved maintainability.
    • Enhanced error handling during database setup to provide clear guidance on initialization failures.
  • Chores

    • Introduced comprehensive database infrastructure supporting device tracking, event logging, session management, and notification functionality.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 7, 2026

πŸ“ Walkthrough

Walkthrough

Database schema creation was moved out of shell scripts into a centralized server/db/schema/app.sql. Two scripts now load that file and validate its presence, and the schema file itself introduces a comprehensive SQLite schema (tables, indexes, views, triggers).

Changes

Cohort / File(s) Summary
Shell script changes
install/production-filesystem/entrypoint.d/25-first-run-db.sh, scripts/db_cleanup/regenerate-database.sh
Removed embedded heredoc SQL; now compute schema path and load server/db/schema/app.sql into the SQLite DB. Added schema-file existence checks and explicit error handling/reporting on import failures.
Database schema
server/db/schema/app.sql
Added full SQLite schema: many tables (Events, Sessions, Devices, Settings, Parameters, Plugins_*, CurrentScan, AppEvents, Notifications, etc.), multiple indexes, views (Events_Devices, LatestEventsPerMAC, Sessions_Devices, Convert_Events_to_Sessions) and triggers on Devices (insert/update/delete) that create AppEvents.

Possibly related PRs

Poem

🐰 I hopped through scripts both near and far,

Moved scattered SQL into one bright star,
Now schemas gather, neat and sound,
One file sleeps safe in one neat round,
Hooray β€” less mess, more carrot-bound!

πŸš₯ Pre-merge checks | βœ… 2 | ❌ 1
❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'move app.' is vague and incomplete, using generic phrasing that does not clearly convey the purpose of extracting the database schema into a separate file. Revise the title to be more descriptive and specific, such as 'Extract database schema into separate app.sql file' or 'Refactor: separate SQLite schema into app.sql'.
βœ… Passed checks (2 passed)
Check name Status Explanation
Description Check βœ… Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage βœ… Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • πŸ“ Generate docstrings
πŸ§ͺ Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❀️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
install/production-filesystem/entrypoint.d/25-first-run-db.sh (1)

61-85: ⚠️ Potential issue | πŸ”΄ Critical

set -eu (line 11) makes the error-handling block on lines 63–85 unreachable.

Because set -e is active, if sqlite3 on line 61 exits non-zero, the shell terminates immediately β€” database_creation_status=$? on line 63 is never reached, and the user never sees the helpful CRITICAL error message.

You need to suppress set -e for the sqlite3 call so the exit code can be captured and the custom error block runs.

Proposed fix
-sqlite3 "${NETALERTX_DB_FILE}" < "${NETALERTX_SERVER}/db/schema/app.sql"
-
-database_creation_status=$?
-
-if [ $database_creation_status -ne 0 ]; then
+if ! sqlite3 "${NETALERTX_DB_FILE}" < "${NETALERTX_SERVER}/db/schema/app.sql"; then
   RED=$(printf '\033[1;31m')
   RESET=$(printf '\033[0m')
πŸ€– Fix all issues with AI agents
In `@install/production-filesystem/entrypoint.d/25-first-run-db.sh`:
- Around line 60-61: The comment above the sqlite3 invocation is stale; update
the comment near the sqlite3 "${NETALERTX_DB_FILE}" <
"${NETALERTX_SERVER}/db/schema/app.sql" line in the 25-first-run-db.sh script to
describe that the schema is being loaded from the app.sql file (via file
redirection) rather than from a heredoc, referencing the NETALERTX_DB_FILE
target and NETALERTX_SERVER/db/schema/app.sql source; replace the old "Write all
text to db file until we see 'end-of-database-schema'" text with a concise note
like "Load database schema from app.sql into NETALERTX_DB_FILE" (or similar) so
the comment matches the current file-based approach.

In `@scripts/db_cleanup/regenerate-database.sh`:
- Line 20: The sqlite3 schema import command (sqlite3 "${NETALERTX_DB_FILE}" <
"${SCHEMA_FILE}") lacks error handling; update regenerate-database.sh to detect
failures from that command and fail fast with a clear error message: either
enable strict error handling (e.g., set -e at script top) or check the exit
status ($?) immediately after the sqlite3 invocation and call echo/process exit
with a non-zero code if it failed, including the NETALERTX_DB_FILE and
SCHEMA_FILE in the message to aid debugging.
- Line 7: The script currently removes only the main DB file
(${NETALERTX_DB_FILE}) and fails to remove SQLite WAL/SHM sidecar files, which
can leave stale state; update the cleanup to also remove the two sidecar files
for the same base name (e.g., "${NETALERTX_DB_FILE}-wal" and
"${NETALERTX_DB_FILE}-shm" or use a glob like "${NETALERTX_DB_FILE}"*), ensuring
you check for their existence before removal so the regenerate-database.sh
script deletes the WAL and SHM files along with the main DB to avoid corruption;
locate the removal call around rm "${NETALERTX_DB_FILE}" and extend it to
include the sidecar filenames.

In `@server/db/schema/app.sql`:
- Around line 296-306: The inline comment for the literal 'DEVICES' is
incorrect: in the INSERT into the audit table the literal maps to the
ObjectPlugin column (not ObjectForeignKey). Update the comments in all three
triggers (trg_insert_devices, trg_update_devices, trg_delete_devices) so the
`'DEVICES'` entry is annotated as "-- ObjectPlugin" (the ObjectForeignKey
comment should remain on the preceding NEW.devGUID / OLD.devGUID value).
- Around line 206-228: The view-column comment blocks for Events_Devices,
LatestEventsPerMAC and Sessions_Devices are stale and missing the Devices
columns devParentRelType and devFQDN; update each trailing /* ... */ comment to
include devParentRelType and devFQDN (matching the Devices table's column names)
so the documented column lists for the views reflect the current schema (refer
to the Devices table and the view names Events_Devices, LatestEventsPerMAC,
Sessions_Devices to locate the comments).
🧹 Nitpick comments (1)
server/db/schema/app.sql (1)

1-2: Inconsistent IF NOT EXISTS usage across table definitions.

Some tables use CREATE TABLE IF NOT EXISTS (Online_History, Settings, Parameters, AppEvents, Notifications) while others use bare CREATE TABLE (Events, Sessions, Devices, Plugins_Objects, Plugins_Events, Plugins_History, Plugins_Language_Strings, CurrentScan). Since this file is intended for fresh database creation, this may be intentional, but it means re-running this file against an existing database will fail on the tables without IF NOT EXISTS. Consider making the usage consistent for robustness.

Also applies to: 13-46, 47-57, 58-61, 62-84, 85-106, 107-136, 137-152, 153-174, 175-187

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@jokob-sk jokob-sk merged commit 3510afe into netalertx:main Feb 7, 2026
5 checks passed
@jokob-sk
Copy link
Collaborator

jokob-sk commented Feb 7, 2026

thanks @adamoutler πŸ™

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants