-
Notifications
You must be signed in to change notification settings - Fork 1
Add notifications_schema table & verify on daemon startup
#422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yhabteab
wants to merge
1
commit into
main
Choose a base branch
from
schema-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package internal | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/icinga/icinga-go-library/backoff" | ||
| "github.com/icinga/icinga-go-library/database" | ||
| "github.com/icinga/icinga-go-library/retry" | ||
| ) | ||
|
|
||
| const ( | ||
| // Both MySQL and PostgreSQL schema versions are currently the same, but they can | ||
| // evolve independently in the future, so can't be merged into a single constant. | ||
| expectedMysqlSchemaVersion = "v1.0" | ||
| expectedPostgresSchemaVersion = "v1.0" | ||
| ) | ||
|
|
||
| // CheckSchema verifies that the database schema version matches the expected version for the database driver. | ||
| func CheckSchema(ctx context.Context, db *database.DB) error { | ||
| var expectedSchemaVersion string | ||
| switch db.DriverName() { | ||
| case database.MySQL: | ||
| expectedSchemaVersion = expectedMysqlSchemaVersion | ||
| case database.PostgreSQL: | ||
| expectedSchemaVersion = expectedPostgresSchemaVersion | ||
| default: | ||
| return fmt.Errorf("unsupported database driver %q", db.DriverName()) | ||
| } | ||
|
|
||
| if hasSchemaTable, err := db.HasTable(ctx, "notifications_schema"); err != nil { | ||
| return fmt.Errorf("cannot verify existence of database schema table: %w", err) | ||
| } else if !hasSchemaTable { | ||
| return errors.New( | ||
| "notifications_schema table does not exist, please make sure you have applied all" + | ||
| " database migrations after upgrading Icinga Notifications", | ||
| ) | ||
| } | ||
|
|
||
| var dbResult []string | ||
| err := retry.WithBackoff( | ||
| ctx, | ||
| func(ctx context.Context) error { | ||
| qs := `SELECT version FROM notifications_schema ORDER BY timestamp DESC LIMIT 1` | ||
| if err := db.SelectContext(ctx, &dbResult, qs); err != nil { | ||
| return database.CantPerformQuery(err, qs) | ||
| } | ||
| return nil | ||
| }, | ||
| retry.Retryable, | ||
| backoff.DefaultBackoff, | ||
| db.GetDefaultRetrySettings(), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if len(dbResult) == 0 { | ||
| return errors.New("no database schema version found") | ||
| } | ||
|
|
||
| if actualSchemaVersion := dbResult[0]; actualSchemaVersion != expectedSchemaVersion { | ||
| return fmt.Errorf( | ||
| "unexpected database schema version: %s (expected %s), please make sure you have applied all"+ | ||
| " database migrations after upgrading Icinga Notifications", | ||
| actualSchemaVersion, expectedSchemaVersion, | ||
| ) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| DROP PROCEDURE IF EXISTS assert_correct_schema_version; | ||
| DELIMITER // | ||
| -- This procedure can be used in upgrade scripts to assert that the schema version in the database matches the | ||
| -- expected version before applying the upgrade. This is important to prevent users from accidentally skipping | ||
| -- intermediate upgrade scripts, which could lead to an inconsistent database state. For instance, since every | ||
| -- upgrade script knows its predecessor's version, we can just do "CALL assert_correct_schema_version('v1.0')" | ||
| -- at the beginning of the 1.x upgrade scripts to ensure that the 1.0 script has been applied before. | ||
| CREATE PROCEDURE assert_correct_schema_version(expected_version text) | ||
| READS SQL DATA | ||
| COMMENT 'Asserts that the schema version in the database matches the expected version and raises an error if not.' | ||
| BEGIN | ||
| DECLARE actual_version text; | ||
| DECLARE error_message text; | ||
| SELECT version INTO actual_version FROM notifications_schema ORDER BY timestamp DESC LIMIT 1; | ||
| IF actual_version IS NULL THEN | ||
| SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Schema version not found in notifications_schema table.'; | ||
| ELSEIF actual_version != expected_version THEN | ||
| SET error_message = CONCAT('Schema version mismatch: expected ', expected_version, ', got ', actual_version, '. Please apply all previous upgrade scripts in order before applying this one.'); | ||
| SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = error_message; | ||
| END IF; | ||
| END; | ||
| // | ||
| DELIMITER ; | ||
|
|
||
| CREATE TABLE notifications_schema ( | ||
| id int NOT NULL AUTO_INCREMENT, | ||
| version varchar(64) NOT NULL, | ||
| timestamp bigint NOT NULL, | ||
|
|
||
| CONSTRAINT pk_notifications_schema PRIMARY KEY (id), | ||
| CONSTRAINT uk_notifications_schema_version UNIQUE (version) | ||
| ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; | ||
|
|
||
| INSERT INTO notifications_schema(version, timestamp) VALUES('v1.0', UNIX_TIMESTAMP() * 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| -- This procedure can be used in upgrade scripts to assert that the schema version in the database matches the | ||
| -- expected version before applying the upgrade. This is important to prevent users from accidentally skipping | ||
| -- intermediate upgrade scripts, which could lead to an inconsistent database state. For instance, since every | ||
| -- upgrade script knows its predecessor's version, we can just do "CALL assert_correct_schema_version('v1.0')" | ||
| -- at the beginning of the 1.x upgrade scripts to ensure that the 1.0 script has been applied before. | ||
| CREATE OR REPLACE PROCEDURE assert_correct_schema_version(expected_version text) | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| actual_version text := (SELECT version FROM notifications_schema ORDER BY timestamp DESC LIMIT 1); | ||
| BEGIN | ||
| IF actual_version IS NULL THEN | ||
| RAISE 'Schema version not found in notifications_schema table.'; | ||
| ELSIF actual_version != expected_version THEN | ||
| RAISE 'Schema version mismatch: expected %, got %. Please apply all previous upgrade scripts in order before applying this one.', expected_version, actual_version; | ||
| END IF; | ||
| END; | ||
| $$; | ||
| COMMENT ON PROCEDURE assert_correct_schema_version IS 'Asserts that the schema version in the database matches the expected version and raises an error if not.'; | ||
|
|
||
| CREATE TABLE notifications_schema ( | ||
| id serial, | ||
| version varchar(64) NOT NULL, | ||
| timestamp bigint NOT NULL, | ||
|
|
||
| CONSTRAINT pk_notifications_schema PRIMARY KEY (id), | ||
| CONSTRAINT uk_notifications_schema_version UNIQUE (version) | ||
| ); | ||
|
|
||
| INSERT INTO notifications_schema(version, timestamp) VALUES('v1.0', EXTRACT(EPOCH from NOW()) * 1000); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.