From a98fe2f850b04fd99f5586c374578ba4dc96ae0d Mon Sep 17 00:00:00 2001 From: Francis Lachapelle Date: Fri, 12 Mar 2021 16:42:23 -0500 Subject: [PATCH] fix(core): synchronize database schema with v5 This fixes infrastructures that run both v2 and v5 instances connected to the same database. --- SOPE/GDLContentStore/contact-oracle.ocs | 7 +- SOPE/GDLContentStore/contact.ocs | 7 +- Scripts/sql-update-2.3.0_to_2.3.24-mysql.sh | 82 +++++++++++++++++++++ Scripts/sql-update-2.3.0_to_2.3.24.sh | 64 ++++++++++++++++ SoObjects/Contacts/NGVCard+SOGo.m | 8 +- 5 files changed, 165 insertions(+), 3 deletions(-) create mode 100755 Scripts/sql-update-2.3.0_to_2.3.24-mysql.sh create mode 100755 Scripts/sql-update-2.3.0_to_2.3.24.sh diff --git a/SOPE/GDLContentStore/contact-oracle.ocs b/SOPE/GDLContentStore/contact-oracle.ocs index 6028f9ed2..fd0158b4b 100644 --- a/SOPE/GDLContentStore/contact-oracle.ocs +++ b/SOPE/GDLContentStore/contact-oracle.ocs @@ -76,7 +76,7 @@ }, { columnName = c_mail; - sqlType = "VARCHAR2(255)"; + sqlType = "CLOB"; allowsNull = YES; }, { @@ -104,5 +104,10 @@ sqlType = "VARCHAR2(10)"; allowsNull = NO; }, + { + columnName = c_hascertificate; + sqlType = "INTEGER"; + allowsNull = YES; + }, ); } diff --git a/SOPE/GDLContentStore/contact.ocs b/SOPE/GDLContentStore/contact.ocs index c2f17528c..9996cea7b 100644 --- a/SOPE/GDLContentStore/contact.ocs +++ b/SOPE/GDLContentStore/contact.ocs @@ -76,7 +76,7 @@ }, { columnName = c_mail; - sqlType = "VARCHAR(255)"; + sqlType = "TEXT"; allowsNull = YES; }, { @@ -104,5 +104,10 @@ sqlType = "VARCHAR(10)"; allowsNull = NO; }, + { + columnName = c_hascertificate; + sqlType = "INT4"; + allowsNull = YES; + }, ); } diff --git a/Scripts/sql-update-2.3.0_to_2.3.24-mysql.sh b/Scripts/sql-update-2.3.0_to_2.3.24-mysql.sh new file mode 100755 index 000000000..94e5409c5 --- /dev/null +++ b/Scripts/sql-update-2.3.0_to_2.3.24-mysql.sh @@ -0,0 +1,82 @@ +#!/bin/bash + +set -e + +# This script only works with MySQL - it does: +# +# 1- updates c_defaults and c_settings to longtext in the sogo_user_profile table +# to avoid truncation of data at 64k +# 2- increase the c_mail column to text to contact quick table +# 3- add the c_hascertificate column to contact quick table + +defaultusername=$USER +defaulthostname=127.0.0.1 +defaultdatabase=sogo +indextable=$(sogo-tool dump-defaults -f /etc/sogo/sogo.conf | awk -F\" '/ OCSFolderInfoURL =/ {print $2}' | awk -F/ '{print $NF}') +if [ -z "$indextable" ]; then + echo "Couldn't fetch OCSFolderInfoURL value, aborting" >&2 + exit 1 +fi + +read -p "Username ($defaultusername): " username +read -p "Hostname ($defaulthostname): " hostname +read -p "Database ($defaultdatabase): " database + +if [ -z "$username" ] +then + username=$defaultusername +fi + +if [ -z "$hostname" ] +then + hostname=$defaulthostname +fi + +if [ -z "$database" ] +then + database=$defaultdatabase +fi + +sqlscript="" + +function growUserProfile() { + oldIFS="$IFS" + IFS=" " + part="`echo -e \"ALTER TABLE sogo_user_profile MODIFY c_defaults LONGTEXT;\\n\"`"; + sqlscript="$sqlscript$part" + part="`echo -e \"ALTER TABLE sogo_user_profile MODIFY c_settings LONGTEXT;\\n\"`"; + sqlscript="$sqlscript$part" + IFS="$oldIFS" +} + +function growMailInContactsQuick() { + oldIFS="$IFS" + IFS=" " + part="`echo -e \"ALTER TABLE $table MODIFY c_mail text;\\n\"`"; + sqlscript="$sqlscript$part" + IFS="$oldIFS" +} + +function addCertificateInContactsQuick() { + oldIFS="$IFS" + IFS=" " + part="`echo -e \"ALTER TABLE $table ADD c_hascertificate INT4 DEFAULT 0;\\n\"`"; + sqlscript="$sqlscript$part" + IFS="$oldIFS" +} + + +echo "This script will ask for the database password twice" >&2 +echo "Converting c_content from TEXT to LONGTEXT in the sogo_user_profile table" >&2 +growUserProfile + +echo "Converting c_mail from VARCHAR(255) to TEXT and add c_hascertificate in Contacts quick tables" >&2 +tables=`mysql -p -s -u $username -h $hostname $database -e "select SUBSTRING_INDEX(c_quick_location, '/', -1) from $indextable where c_path3 = 'Contacts';"` + +for table in $tables; +do + growMailInContactsQuick + addCertificateInContactsQuick +done + +echo "$sqlscript" | mysql -p -f -s -u $username -h $hostname $database diff --git a/Scripts/sql-update-2.3.0_to_2.3.24.sh b/Scripts/sql-update-2.3.0_to_2.3.24.sh new file mode 100755 index 000000000..5c63fbdcd --- /dev/null +++ b/Scripts/sql-update-2.3.0_to_2.3.24.sh @@ -0,0 +1,64 @@ +#!/bin/bash + +set -e + +# This script only works with PostgreSQL - it does: +# +# 1- increase the c_mail column to text to contact quick table +# 2- add the c_hascertificate column to contact quick table + +defaultusername=$USER +defaulthostname=localhost +defaultdatabase=sogo +indextable=$(sogo-tool dump-defaults -f /etc/sogo/sogo.conf | awk -F\" '/ OCSFolderInfoURL =/ {print $2}' | awk -F/ '{print $NF}') +if [ -z "$indextable" ]; then + echo "Couldn't fetch OCSFolderInfoURL value, aborting" >&2 + exit 1 +fi + +read -p "Username ($defaultusername): " username +read -p "Hostname ($defaulthostname): " hostname +read -p "Database ($defaultdatabase): " database + +if [ -z "$username" ] +then + username=$defaultusername +fi +if [ -z "$hostname" ] +then + hostname=$defaulthostname +fi +if [ -z "$database" ] +then + database=$defaultdatabase +fi + +sqlscript="" + +function growMailInContactsQuick() { + oldIFS="$IFS" + IFS=" " + part="`echo -e \"ALTER TABLE $table ALTER COLUMN c_mail TYPE TEXT;\\n\"`"; + sqlscript="$sqlscript$part" + IFS="$oldIFS" +} + +function addCertificateInContactsQuick() { + oldIFS="$IFS" + IFS=" " + part="`echo -e \"ALTER TABLE $table ADD c_hascertificate INT4 DEFAULT 0;\\n\"`"; + sqlscript="$sqlscript$part" + IFS="$oldIFS" +} + +echo "This script will ask for the database password twice" >&2 +echo "Converting c_mail from VARCHAR(255) to TEXT and add c_hascertificate in Contacts quick tables" >&2 +tables=`psql -t -U $username -h $hostname $database -c "select split_part(c_quick_location, '/', 5) from $indextable where c_path3 = 'Contacts';"` + +for table in $tables; +do + growMailInContactsQuick + addCertificateInContactsQuick +done + +echo "$sqlscript" | psql -q -e -U $username -h $hostname $database diff --git a/SoObjects/Contacts/NGVCard+SOGo.m b/SoObjects/Contacts/NGVCard+SOGo.m index e69b6dd17..60b4f339f 100644 --- a/SoObjects/Contacts/NGVCard+SOGo.m +++ b/SoObjects/Contacts/NGVCard+SOGo.m @@ -1,6 +1,6 @@ /* NGVCard+SOGo.m - this file is part of SOGo * - * Copyright (C) 2009-2015 Inverse inc. + * Copyright (C) 2009-2021 Inverse inc. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,6 +19,7 @@ */ +#import #import #import @@ -918,6 +919,11 @@ convention: [fields setObject: [NSNull null] forKey: @"c_categories"]; [fields setObject: @"vcard" forKey: @"c_component"]; + // S/MIME certificate + element = [self uniqueChildWithTag: @"key"]; + [fields setObject: [NSNumber numberWithInt: (element && ![element isVoid])] + forKey: @"c_hascertificate"]; + return fields; }