-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add event types table for audit logs
- Loading branch information
Showing
13 changed files
with
275 additions
and
220 deletions.
There are no files selected for viewing
This file contains 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 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,27 @@ | ||
-- re-add the old column | ||
ALTER TABLE | ||
audit_logs | ||
ADD | ||
COLUMN action TEXT NOT NULL DEFAULT 'unknown'; | ||
|
||
-- revert data migration | ||
UPDATE | ||
audit_logs | ||
SET | ||
action = CASE | ||
event_type_id | ||
WHEN 1 THEN 'added' | ||
WHEN 2 THEN 'removed' | ||
WHEN 3 THEN 'claimed' | ||
WHEN 4 THEN 'released' | ||
WHEN 5 THEN 'activated' | ||
WHEN 6 THEN 'ping' | ||
WHEN 7 THEN 'culled' | ||
END; | ||
|
||
-- drop the new column | ||
ALTER TABLE | ||
audit_logs DROP COLUMN event_type_id; | ||
|
||
-- drop the new table | ||
DROP TABLE event_types; |
This file contains 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,59 @@ | ||
-- create events table | ||
CREATE TABLE event_types (id TINYINT PRIMARY KEY, name TEXT NOT NULL); | ||
|
||
-- insert events | ||
INSERT INTO | ||
event_types (id, name) | ||
VALUES | ||
(0, 'unknown'), | ||
(1, 'license.added'), | ||
(2, 'license.removed'), | ||
(3, 'license.claimed'), | ||
(4, 'license.released'), | ||
(5, 'node.activated'), | ||
(6, 'node.ping'), | ||
(7, 'node.culled'); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
-- rebuild the table with the new schema (this is a workaround for sqlite not supporting ALTER TABLE x ALTER COLUMN y NOT NULL) | ||
CREATE TABLE _audit_logs ( | ||
This comment has been minimized.
Sorry, something went wrong. |
||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
event_type_id TINYINT NOT NULL REFERENCES event_types (id), | ||
entity_type TEXT NOT NULL, | ||
entity_id TEXT NOT NULL, | ||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP | ||
); | ||
|
||
-- copy data from the old table to the new | ||
INSERT INTO | ||
_audit_logs ( | ||
id, | ||
event_type_id, | ||
entity_type, | ||
entity_id, | ||
created_at | ||
) | ||
SELECT | ||
id, | ||
CASE | ||
action | ||
WHEN 'added' THEN 1 | ||
WHEN 'removed' THEN 2 | ||
WHEN 'claimed' THEN 3 | ||
WHEN 'released' THEN 4 | ||
WHEN 'activated' THEN 5 | ||
WHEN 'ping' THEN 6 | ||
WHEN 'culled' THEN 7 | ||
ELSE 0 | ||
END AS event_type_id, | ||
entity_type, | ||
entity_id, | ||
created_at | ||
FROM | ||
audit_logs; | ||
|
||
-- drop the old table | ||
DROP TABLE audit_logs; | ||
|
||
-- replace with new table | ||
ALTER TABLE | ||
_audit_logs RENAME TO audit_logs; |
This file contains 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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
-- name: InsertAuditLog :exec | ||
INSERT INTO audit_logs (action, entity_type, entity_id) | ||
INSERT INTO audit_logs (event_type_id, entity_type, entity_id) | ||
VALUES (?, ?, ?); | ||
|
||
-- name: GetAuditLogs :many | ||
SELECT id, action, entity_type, entity_id, created_at | ||
SELECT id, event_type_id, entity_type, entity_id, created_at | ||
FROM audit_logs | ||
ORDER BY created_at DESC | ||
LIMIT ?; | ||
|
||
-- name: GetAuditLogsByEntity :many | ||
SELECT id, action, entity_type, entity_id, created_at | ||
SELECT id, event_type_id, entity_type, entity_id, created_at | ||
FROM audit_logs | ||
WHERE entity_id = ? AND entity_type = ? | ||
ORDER BY created_at DESC; |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add
node.deactivated
?