Skip to content

Commit

Permalink
add initial patch support & schema checks
Browse files Browse the repository at this point in the history
  • Loading branch information
killua-eu committed Feb 11, 2024
1 parent 2cc2fa4 commit 50e7dc0
Show file tree
Hide file tree
Showing 4 changed files with 291 additions and 41 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"middlewares/trailing-slash": "^2",
"monolog/monolog": "^3",
"nyholm/psr7": "^1",
"opis/json-schema": "^2",
"opis/json-schema": "^2.3",
"php-di/php-di": "^7",
"php-http/guzzle7-adapter": "^1",
"php-http/message": "^1",
Expand Down
38 changes: 38 additions & 0 deletions glued/Config/Migrations/20240211141927_add_audit.sql
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 glued/Config/Migrations/20240211150426_add_audit_trigger.sql
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`;
Loading

0 comments on commit 50e7dc0

Please sign in to comment.