
-- Fix vn.workerLog.changedModel

ALTER TABLE vn.workerLog
	MODIFY COLUMN changedModel ENUM('Worker','workerDocument','Calendar','WorkerTimeControlMail','Business', 'WorkerDms')
		CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'Worker' NOT NULL;

UPDATE vn.workerLog SET changedModel = 'WorkerDms' WHERE changedModel = 'workerDocument';

ALTER TABLE vn.workerLog
	MODIFY COLUMN changedModel ENUM('Worker','Calendar','WorkerTimeControlMail','Business', 'WorkerDms')
		CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'Worker' NOT NULL;

-- Fix account.userLog.changedModel

ALTER TABLE account.userLog
	MODIFY COLUMN changedModel enum('User', 'VnUser', 'Account', 'MailAliasAccount', 'MailForward')
		CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'User' NOT NULL;

UPDATE account.userLog SET changedModel = 'VnUser' WHERE changedModel = 'User';

UPDATE account.userLog u
	SET changedModel = 'MailForward'
	WHERE
		changedModel = ''
		AND (JSON_VALUE(newInstance, '$.forwardTo') IS NOT NULL
		OR JSON_VALUE(oldInstance, '$.forwardTo') IS NOT NULL);

UPDATE account.userLog u
	SET changedModel = 'MailAliasAccount'
	WHERE
		changedModel = ''
		AND (JSON_VALUE(newInstance, '$.mailAlias') IS NOT NULL
		OR JSON_VALUE(oldInstance, '$.mailAlias') IS NOT NULL);

ALTER TABLE account.userLog
	MODIFY COLUMN changedModel enum('VnUser', 'Account', 'MailAliasAccount', 'MailForward')
		CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci DEFAULT 'VnUser' NOT NULL;
