<commit_before>-- ---------------------------------------------------------------------------
-- CONVERT SENT AND SENT_DATE TO MESSAGE_STATE AND PROCESSING_DATE
-- ---------------------------------------------------------------------------

ALTER TABLE
	message
		ADD COLUMN state VARCHAR(255) AFTER sent,
		CHANGE sent_date processing_date DATETIME;

UPDATE
	message
SET
	state = 'SENT'
WHERE
	sent = 1;

UPDATE
	message
SET
	state = 'FINAL'
WHERE
	sent = 0;

ALTER TABLE
	message
		DROP COLUMN sent;

-- ---------------------------------------------------------------------------
-- MERGE MESSAGE AND MESSAGE_RECIPIENT TABLES
-- ---------------------------------------------------------------------------

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

ALTER TABLE 
	message_recipient 
		DROP INDEX FK398E4FE149BE803A, 
		DROP FOREIGN KEY FK398E4FE149BE803A;

ALTER TABLE 
	message_recipient 
		DROP INDEX FK398E4FE1CAFD62F8, 
		DROP FOREIGN KEY FK398E4FE1CAFD62F8;

-- Add the recipient column to the message table
ALTER TABLE
	message
		ADD COLUMN recipient BIGINT AFTER sender, 
		ADD INDEX IDX_MESSAGE_RECIPIENT (recipient), 
		ADD CONSTRAINT FK_MESSAGE_RECIPIENT_PERSON FOREIGN KEY (recipient) REFERENCES person (id),
-- This column is temporary so we now what to clean up
		ADD COLUMN tmp TINYINT(1) DEFAULT 0;

UPDATE
	message
SET
	recipient = (SELECT person_id FROM message_recipient WHERE message.id=message_recipient.message_id LIMIT 1),
	tmp = 1;

INSERT INTO
	message
(
	message_type, create_date, subject, body, sender, recipient, sent, sent_date, mail_type
)
SELECT
	message_type, create_date, subject, body, sender, mr.person_id, sent, sent_date, mail_type
FROM
	message AS m
	INNER JOIN
	message_recipient AS mr ON m.id=mr.message_id;

DELETE FROM
	message
WHERE
	tmp = 1;

ALTER TABLE
	message
		DROP COLUMN tmp;

-- Drop the message_recipient table
DROP TABLE
	message_recipient;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;




-- Upgrade version
UPDATE
	application_configuration 
SET
	`value`='1.46'
WHERE
	name='VERSION';
<commit_msg>Fixed a bug in the upgrade script.<commit_after>-- ---------------------------------------------------------------------------
-- CONVERT SENT AND SENT_DATE TO MESSAGE_STATE AND PROCESSING_DATE
-- ---------------------------------------------------------------------------

ALTER TABLE
	message
		ADD COLUMN state VARCHAR(255) AFTER sent,
		CHANGE sent_date processing_date DATETIME;

UPDATE
	message
SET
	state = 'SENT'
WHERE
	sent = 1;

UPDATE
	message
SET
	state = 'FINAL'
WHERE
	sent = 0;

ALTER TABLE
	message
		DROP COLUMN sent;

-- ---------------------------------------------------------------------------
-- MERGE MESSAGE AND MESSAGE_RECIPIENT TABLES
-- ---------------------------------------------------------------------------

/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

ALTER TABLE 
	message_recipient 
		DROP INDEX FK398E4FE149BE803A, 
		DROP FOREIGN KEY FK398E4FE149BE803A;

ALTER TABLE 
	message_recipient 
		DROP INDEX FK398E4FE1CAFD62F8, 
		DROP FOREIGN KEY FK398E4FE1CAFD62F8;

-- Add the recipient column to the message table
ALTER TABLE
	message
		ADD COLUMN recipient BIGINT AFTER sender, 
		ADD INDEX IDX_MESSAGE_RECIPIENT (recipient), 
		ADD CONSTRAINT FK_MESSAGE_RECIPIENT_PERSON FOREIGN KEY (recipient) REFERENCES person (id),
-- This column is temporary so we now what to clean up
		ADD COLUMN tmp TINYINT(1) DEFAULT 0;

UPDATE
	message
SET
	recipient = (SELECT person_id FROM message_recipient WHERE message.id=message_recipient.message_id LIMIT 1),
	tmp = 1;

INSERT INTO
	message
(
	message_type, create_date, subject, body, sender, recipient, processing_date, mail_type
)
SELECT
	message_type, create_date, subject, body, sender, mr.person_id, processing_date, mail_type
FROM
	message AS m
	INNER JOIN
	message_recipient AS mr ON m.id=mr.message_id;

DELETE FROM
	message
WHERE
	tmp = 1;

ALTER TABLE
	message
		DROP COLUMN tmp;

-- Drop the message_recipient table
DROP TABLE
	message_recipient;

/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;




-- Upgrade version
UPDATE
	application_configuration 
SET
	`value`='1.46'
WHERE
	name='VERSION';

{'fraction_non_alphanumeric': 0.19764428739693757, 'fraction_numerical': 0.028268551236749116, 'mean_word_length': 2.353870458135861, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6981222', 'n_tokens_mistral': 1491, 'n_tokens_neox': 1336, 'n_words': 398}