Last active
July 11, 2018 10:02
-
-
Save callemo/06bc118a208f43f7c9fd1d4d4d2b3dcd to your computer and use it in GitHub Desktop.
Unstructured key-value storage for MySQL
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
CREATE TABLE IF NOT EXISTS `collection` ( | |
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, | |
`uuid` BINARY(16) NOT NULL, | |
`value` MEDIUMBLOB NOT NULL, | |
`version` BIGINT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'optimistic updates', | |
`format` ENUM ('R', 'J', 'M') NOT NULL DEFAULT 'J' COMMENT 'R: raw, J: json, M: msgpack', | |
`compression` ENUM ('N', 'Z') NOT NULL DEFAULT 'N' COMMENT 'N: none, Z: gzip', | |
`deleted_at` DATETIME NULL DEFAULT NULL, | |
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | |
`updated_at` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
PRIMARY KEY (`id`), | |
UNIQUE KEY (`uuid`), | |
INDEX (`updated_at`) | |
) | |
ENGINE = InnoDB | |
ROW_FORMAT = DYNAMIC | |
DEFAULT CHARACTER SET utf8 | |
DEFAULT COLLATE utf8_unicode_ci; | |
DELIMITER // | |
CREATE TRIGGER `collection_insert_uuid` | |
BEFORE INSERT | |
ON `collection` | |
FOR EACH ROW | |
BEGIN | |
IF NEW.`uuid` IS NULL | |
THEN | |
SET @uuid := cast(UUID() AS BINARY(36)); | |
SET NEW.`uuid` = | |
unhex(concat(substr(@uuid, 15, 4), substr(@uuid, 10, 4), | |
substr(@uuid, 1, 8), substr(@uuid, 20, 4), | |
substr(@uuid, 25))); | |
END IF; | |
END; | |
// | |
DELIMITER ; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment