-
-
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.
- Loading branch information
Showing
7 changed files
with
123 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
-- re-add the old column | ||
ALTER TABLE | ||
audit_logs | ||
ADD | ||
COLUMN entity_type TEXT NOT NULL DEFAULT 'unknown'; | ||
|
||
-- revert data migration | ||
UPDATE | ||
audit_logs | ||
SET | ||
entity_type = CASE | ||
entity_type_id | ||
WHEN 1 THEN 'License' | ||
WHEN 2 THEN 'Node' | ||
END; | ||
|
||
-- drop the new column | ||
ALTER TABLE | ||
audit_logs DROP COLUMN entity_type_id; | ||
|
||
-- drop the new table | ||
DROP TABLE entity_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,49 @@ | ||
-- create entities table | ||
CREATE TABLE entity_types (id TINYINT PRIMARY KEY, name TEXT NOT NULL); | ||
|
||
-- insert events | ||
INSERT INTO | ||
entity_types (id, name) | ||
VALUES | ||
(0, 'unknown'), | ||
(1, 'license'), | ||
(2, 'node'); | ||
|
||
-- 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 ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
event_type_id TINYINT NOT NULL REFERENCES event_types (id), | ||
entity_type_id TINYINT NOT NULL REFERENCES entity_types (id), | ||
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_id, | ||
entity_id, | ||
created_at | ||
) | ||
SELECT | ||
id, | ||
event_type_id, | ||
CASE | ||
entity_type | ||
WHEN 'License' THEN 1 | ||
WHEN 'Node' THEN 2 | ||
ELSE 0 | ||
END AS entity_type_id, | ||
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 (event_type_id, entity_type, entity_id) | ||
INSERT INTO audit_logs (event_type_id, entity_type_id, entity_id) | ||
VALUES (?, ?, ?); | ||
|
||
-- name: GetAuditLogs :many | ||
SELECT id, event_type_id, entity_type, entity_id, created_at | ||
SELECT id, event_type_id, entity_type_id, entity_id, created_at | ||
FROM audit_logs | ||
ORDER BY created_at DESC | ||
LIMIT ?; | ||
|
||
-- name: GetAuditLogsByEntity :many | ||
SELECT id, event_type_id, entity_type, entity_id, created_at | ||
SELECT id, event_type_id, entity_type_id, entity_id, created_at | ||
FROM audit_logs | ||
WHERE entity_id = ? AND entity_type = ? | ||
WHERE entity_type_id = ? AND entity_id = ? | ||
ORDER BY created_at DESC; |
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.
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