-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add initial patch support & schema checks
- Loading branch information
Showing
4 changed files
with
291 additions
and
41 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,38 @@ | ||
-- migrate:up | ||
|
||
ALTER TABLE `t_contacts_objects` | ||
DROP COLUMN `c_private`, | ||
DROP COLUMN `c_rev`; | ||
|
||
ALTER TABLE `t_contacts_objects` | ||
ADD COLUMN `c_private` bit(1) AS (COALESCE(JSON_UNQUOTE(JSON_EXTRACT(`c_data`, '$._.private'))+0, 0)) STORED COMMENT 'Exclude private data from ft search' AFTER `c_data`; | ||
|
||
|
||
CREATE TABLE `t_contacts_objects_audit` ( | ||
`c_uuid` binary(16) NOT NULL COMMENT 'Object UUID', | ||
`c_hash` binary(16) GENERATED ALWAYS AS (unhex(md5(concat(`c_data`)))) STORED, | ||
`c_data` json DEFAULT NULL COMMENT 'Object data as JSON', | ||
`c_ts` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Timestamp column' | ||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci; | ||
|
||
|
||
CREATE OR REPLACE VIEW v_contacts_objects_audit AS SELECT | ||
t.*, | ||
ROW_NUMBER() OVER (PARTITION BY t.c_uuid ORDER BY t.c_ts) AS c_rev, | ||
COUNT(*) OVER (PARTITION BY t.c_uuid) AS c_rev_count | ||
FROM t_contacts_objects_audit t; | ||
|
||
|
||
-- migrate:down | ||
|
||
DROP VIEW IF EXISTS `v_contacts_objects_audit`; | ||
DROP TABLE IF EXISTS `t_contacts_objects_audit`; | ||
|
||
ALTER TABLE `t_contacts_objects` | ||
DROP COLUMN `c_private`; | ||
|
||
ALTER TABLE `t_contacts_objects` | ||
ADD COLUMN `c_rev` int unsigned NOT NULL DEFAULT '0' AFTER `c_uuid`, | ||
ADD COLUMN `c_private` bit(1) NOT NULL DEFAULT b'0' AFTER `c_data`; | ||
|
||
|
13 changes: 13 additions & 0 deletions
13
glued/Config/Migrations/20240211150426_add_audit_trigger.sql
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,13 @@ | ||
-- migrate:up | ||
|
||
CREATE TRIGGER `before_update_t_contacts_objects` BEFORE UPDATE ON `t_contacts_objects` FOR EACH ROW | ||
BEGIN | ||
IF (OLD.c_hash <> NEW.c_hash) THEN | ||
INSERT INTO `t_contacts_objects_audit` (`c_uuid`, `c_data`, `c_ts`) | ||
VALUES (OLD.c_uuid, OLD.c_data, NOW()); | ||
END IF; | ||
END | ||
|
||
-- migrate:down | ||
|
||
DROP TRIGGER IF EXISTS `before_update_t_contacts_objects`; |
Oops, something went wrong.