Skip to content

Instantly share code, notes, and snippets.

@callemo
Last active July 11, 2018 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save callemo/06bc118a208f43f7c9fd1d4d4d2b3dcd to your computer and use it in GitHub Desktop.
Save callemo/06bc118a208f43f7c9fd1d4d4d2b3dcd to your computer and use it in GitHub Desktop.
Unstructured key-value storage for MySQL
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