umami/db/mysql/migrations/02_add_event_data/migration.sql

91 lines
3.1 KiB
MySQL
Raw Normal View History

2022-08-01 21:22:17 +02:00
-- DropForeignKey
ALTER TABLE `event` DROP FOREIGN KEY `event_ibfk_1`;
ALTER TABLE `event` DROP FOREIGN KEY `event_ibfk_2`;
2022-08-01 21:22:17 +02:00
DROP INDEX `event_created_at_idx` ON `event`;
DROP INDEX `event_session_id_idx` ON `event`;
DROP INDEX `event_website_id_idx` ON `event`;
2022-08-01 21:22:17 +02:00
CREATE INDEX `event_old_created_at_idx` ON `event` (created_at);
CREATE INDEX `event_old_session_id_idx` ON `event` (session_id);
CREATE INDEX `event_old_website_id_idx` ON `event` (website_id);
2022-08-01 21:22:17 +02:00
-- RenameTable
RENAME TABLE `event` TO `_event_old`;
2022-08-01 21:22:17 +02:00
-- CreateTable
CREATE TABLE `event`
2022-08-01 21:22:17 +02:00
(
`event_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
`website_id` INTEGER UNSIGNED NOT NULL,
`session_id` INTEGER UNSIGNED NOT NULL,
`created_at` TIMESTAMP(0) NULL DEFAULT CURRENT_TIMESTAMP(0),
`url` VARCHAR(500) NOT NULL,
`event_name` VARCHAR(50) NOT NULL,
INDEX `event_created_at_idx`(`created_at`),
INDEX `event_session_id_idx`(`session_id`),
INDEX `event_website_id_idx`(`website_id`),
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2022-08-01 21:22:17 +02:00
-- AddForeignKey
ALTER TABLE `event` ADD CONSTRAINT `event_ibfk_2` FOREIGN KEY (`session_id`) REFERENCES `session`(`session_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
2022-08-01 21:22:17 +02:00
-- AddForeignKey
ALTER TABLE `event` ADD CONSTRAINT `event_ibfk_1` FOREIGN KEY (`website_id`) REFERENCES `website`(`website_id`) ON DELETE CASCADE ON UPDATE NO ACTION;
2022-08-01 21:22:17 +02:00
2022-07-28 08:16:16 +02:00
-- CreateTable
CREATE TABLE `event_data` (
`event_data_id` INTEGER NOT NULL AUTO_INCREMENT,
`event_id` INTEGER UNSIGNED NOT NULL,
`event_data` JSON NOT NULL,
UNIQUE INDEX `event_data_event_id_key`(`event_id`),
PRIMARY KEY (`event_data_id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2022-07-28 08:16:16 +02:00
-- AddForeignKey
ALTER TABLE `event_data` ADD CONSTRAINT `event_data_event_id_fkey` FOREIGN KEY (`event_id`) REFERENCES `event`(`event_id`) ON DELETE RESTRICT ON UPDATE CASCADE;
2022-08-11 20:33:21 +02:00
-- CreateProcedureRenameIndex
CREATE PROCEDURE `UmamiRenameIndexIfExists`(
IN i_table_name VARCHAR(128),
IN i_current_index_name VARCHAR(128),
IN i_new_index_name VARCHAR(128)
)
BEGIN
2022-07-28 08:16:16 +02:00
2022-08-11 20:33:21 +02:00
SET @tableName = i_table_name;
SET @currentIndexName = i_current_index_name;
SET @newIndexName = i_new_index_name;
SET @indexExists = 0;
2022-07-28 08:16:16 +02:00
2022-08-11 20:33:21 +02:00
SELECT
1
INTO @indexExists FROM
INFORMATION_SCHEMA.STATISTICS
WHERE
TABLE_NAME = @tableName
AND INDEX_NAME = @currentIndexName
AND TABLE_SCHEMA = database();
2022-08-11 20:33:21 +02:00
SET @query = CONCAT(
'ALTER TABLE `', @tableName, '` RENAME INDEX `', @currentIndexName, '` TO `', @newIndexName, '`;'
);
IF @indexExists THEN
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
END;
2022-07-28 08:16:16 +02:00
-- RenameIndex
2022-08-11 20:33:21 +02:00
CALL UmamiRenameIndexIfExists('account', 'username', 'account_username_key');
CALL UmamiRenameIndexIfExists('session', 'session_uuid', 'session_session_uuid_key');
CALL UmamiRenameIndexIfExists('website', 'share_id', 'website_share_id_key');
CALL UmamiRenameIndexIfExists('website', 'website_uuid', 'website_website_uuid_key');
-- Drop CreateProcedureRenameIndex
drop procedure `UmamiRenameIndexIfExists`;