Skip to content

Add notifications_schema table & verify on daemon startup#422

Open
yhabteab wants to merge 1 commit into
mainfrom
schema-table
Open

Add notifications_schema table & verify on daemon startup#422
yhabteab wants to merge 1 commit into
mainfrom
schema-table

Conversation

@yhabteab
Copy link
Copy Markdown
Member

@yhabteab yhabteab commented May 13, 2026

This PR adds a new notifications_schema table to the database, which is used to store the schema version for notifications. It quite similar to the one used in Icinga DB, but with a small but important difference: the version column is of type TEXT instead of INTEGER as opposed to Icinga DB. Instead of using an independent integer versioning system that can't be easily determined to which Icinga Notifications version it corresponds, we will use the actual semver version exactly as its upgrade script is named.

In order to prevent users from applying the upgrade scripts out of order or the same script multiple times, any upgrade script after Icinga Notifications v1.0 will have use an SQL procedure introduced in this PR, which will check if the current version in the notifications_schema table matches the expected one before applying the upgrade. If the version does not match, the procedure will raise an error and prevent the upgrade from being applied. For instance, if we have an upgrade script for version v1.1, at the top of the script we will have a call to the procedure like this:

CALL assert_correct_schema_version('v1.0');

This ensures that the current upgrade script can only be applied if the current version in the notifications_schema table is v1.0, which means that the previous upgrade script has been applied. If the current version is not v1.0, the procedure will raise an error and prevent the upgrade from being applied, thus ensuring that the upgrade scripts are applied in the correct order and that no script is applied multiple times. And of course, Icinga Notifications performs a check at startup to ensure that the expected schema version is applied before doing anything else, but it doesn't go through the notifications_schema entries to determine for missing upgrade scripts since that is supposed to be handled by the upgrade scripts themselves.

resolves #343

@cla-bot cla-bot Bot added the cla/signed CLA is signed by all contributors of a PR label May 13, 2026
@yhabteab yhabteab added this to the 1.0 milestone May 13, 2026
@yhabteab yhabteab requested review from nilmerg and oxzi May 13, 2026 10:53
@yhabteab yhabteab added the enhancement New feature or request label May 13, 2026
@nilmerg
Copy link
Copy Markdown
Member

nilmerg commented May 13, 2026

Focusing on the schema differences here: That the version column is of type varchar now suits me well, because I was already thinking about moving the schema authority to Web, because it's got this nice migration feature with Icinga Web v2.12 and most other configuration already happens in Web. So with respect to that, that's fine as it is the case anywhere else as well with this feature in mind.

I too find it confusing that the schema table of icingadb uses a integer that differs from the file name or product version…

@yhabteab yhabteab force-pushed the schema-table branch 2 times, most recently from 9f5dd65 to 1408de6 Compare May 18, 2026 10:46
@yhabteab
Copy link
Copy Markdown
Member Author

Changed the variable len of the version column from 255 to 64 and added a unique constraint, to make it consistent with Icinga Web 2' schema.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Introduces a notifications_schema table that records the applied schema version (as a semver string like v1.0), plus a SQL assert_correct_schema_version procedure so that future upgrade scripts can refuse to run out of order. The daemon now verifies on startup that the schema version stored in the DB matches the version the binary expects, failing fast otherwise. This resolves #343 and lays the groundwork for safer future migrations.

Changes:

  • Add notifications_schema table and assert_correct_schema_version PL/pgSQL / MySQL stored procedure in both the base schema and a new upgrades/1.0.sql for PostgreSQL and MySQL.
  • Add internal.CheckSchema which compares the latest notifications_schema.version row against a per-driver expected version using golang.org/x/mod/semver.
  • Wire CheckSchema into the daemon startup in cmd/icinga-notifications/main.go and pull in golang.org/x/mod as a new dependency.

Reviewed changes

Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
schema/pgsql/schema.sql Defines the assert procedure and the notifications_schema table; seeds v1.0.
schema/pgsql/upgrades/1.0.sql Upgrade script equivalent that creates the procedure, table and seed row for existing installs.
schema/mysql/schema.sql MySQL counterpart of the procedure/table/seed, using SIGNAL SQLSTATE '45000' for errors.
schema/mysql/upgrades/1.0.sql MySQL upgrade script mirroring the base schema additions.
internal/schema.go New CheckSchema helper that verifies the table exists and the latest version matches per driver, with retry.
cmd/icinga-notifications/main.go Calls internal.CheckSchema after connecting to the DB; fatals on mismatch.
go.mod / go.sum Adds golang.org/x/mod v0.35.0 for semver.Compare.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread schema/pgsql/schema.sql Outdated
Comment thread internal/schema.go
Comment thread internal/schema.go Outdated
Comment thread internal/schema.go Outdated
Comment thread schema/pgsql/upgrades/1.0.sql Outdated
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated no new comments.

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

Labels

cla/signed CLA is signed by all contributors of a PR enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

notifications_schema table

3 participants