2021-01-11 23:46:48 +01:00
#!/bin/bash
#=================================================
# COMMON VARIABLES
#=================================================
#=================================================
# PERSONAL HELPERS
#=================================================
2023-02-06 23:05:30 +01:00
mariadb-to-pg( ) {
2022-06-28 23:12:39 +02:00
2023-03-06 22:26:35 +01:00
ynh_print_info --message= "Migrating to PostgreSQL database..."
mysqlpwd = $( ynh_app_setting_get --app= $app --key= mysqlpwd)
2022-06-28 23:12:39 +02:00
2023-02-06 23:05:30 +01:00
# In old instance db_user is `mmuser`
mysql_db_user = " $db_user "
if ynh_mysql_connect_as --user= "mmuser" --password= " $mysqlpwd " 2> /dev/null <<< ";" ; then
mysql_db_user = "mmuser"
fi
2023-02-07 22:26:27 +01:00
# Initialize PostgreSQL database
2023-03-06 22:26:35 +01:00
ynh_psql_test_if_first_run
ynh_psql_setup_db --db_user= $db_user --db_name= $db_name --db_pwd= $mysqlpwd
psqlpwd = $( ynh_app_setting_get --app= $app --key= psqlpwd)
2022-09-28 22:20:41 +02:00
2023-02-07 22:26:27 +01:00
# Configure the new database and run Mattermost in order to create tables
2023-03-07 09:11:34 +01:00
ynh_write_var_in_file --file= " $install_dir /config/config.json " --key= "DriverName" --value= "postgres" --after= "SqlSettings"
ynh_write_var_in_file --file= " $install_dir /config/config.json " --key= "DataSource" --value= " postgres:// $db_user : $psqlpwd @localhost:5432/ $db_name ?sslmode=disable&connect_timeout=10 " --after= "SqlSettings"
cat " $install_dir /config/config.json "
pushd $install_dir
2023-02-06 23:05:30 +01:00
ynh_systemd_action --service_name= " $app " --action= "stop"
set +e
2023-03-06 22:26:35 +01:00
sudo -u mattermost timeout --preserve-status 180 "./bin/mattermost"
2023-02-06 23:05:30 +01:00
if [ " $? " != "0" ] && [ " $? " != "143" ] ; then
2023-03-06 22:26:35 +01:00
ynh_die --message= "Failed to run Mattermost to create PostgreSQL database tables" --ret_code= 1
2023-02-06 23:05:30 +01:00
fi
set -e
popd
2023-02-07 22:26:27 +01:00
# Some fixes to let the MariaDB -> PostgreSQL conversion working
2023-03-06 22:26:35 +01:00
ynh_psql_execute_as_root --sql= 'DROP INDEX public.idx_fileinfo_content_txt;' --database= $db_name
ynh_psql_execute_as_root --sql= 'DROP INDEX public.idx_posts_message_txt;' --database= $db_name
ynh_mysql_execute_as_root --sql= "ALTER TABLE mattermost.Users DROP COLUMN IF EXISTS acceptedtermsofserviceid;" --database= $db_name
ynh_mysql_execute_as_root --sql= "ALTER TABLE mattermost.SharedChannelRemotes DROP COLUMN IF EXISTS description;" --database= $db_name
ynh_mysql_execute_as_root --sql= "ALTER TABLE mattermost.SharedChannelRemotes DROP COLUMN IF EXISTS nextsyncat;" --database= $db_name
2023-03-07 11:42:53 +01:00
# Focalboard is broken in Mattermost 7.3.0
if ynh_compare_current_package_version --comparison eq --version 7.3.0~ynh1
then
# Remove Focalboard tables
# Command from https://stackoverflow.com/a/1589324
cmd = $( ynh_mysql_execute_as_root --sql= " SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' ) AS statement FROM information_schema.tables WHERE table_schema = ' $db_name ' AND table_name LIKE 'focalboard_%'; " --database= $db_name | tail -n 1)
ynh_mysql_execute_as_root --sql= " $cmd " --database= $db_name
fi
2023-02-06 23:05:30 +01:00
2023-03-06 22:26:35 +01:00
# Use pgloader to migrate database content from MariaDB to PostgreSQL
2023-02-06 23:05:30 +01:00
tmpdir = " $( mktemp -d) "
cat <<EOT > $tmpdir /commands.load
LOAD DATABASE
FROM mysql://$mysql_db_user :$mysqlpwd @127.0.0.1:3306/$db_name
INTO postgresql://$db_user :$psqlpwd @127.0.0.1:5432/$db_name
WITH include no drop, truncate, create no tables,
create no indexes, preserve index names, no foreign keys,
data only, workers = 16, concurrency = 1
SET MySQL PARAMETERS
net_read_timeout = '90' ,
net_write_timeout = '180'
ALTER SCHEMA '$db_name' RENAME TO 'public'
;
EOT
2023-03-06 22:26:35 +01:00
pgloader $tmpdir /commands.load
2023-02-06 23:05:30 +01:00
# Rebuild INDEX
2023-03-06 22:26:35 +01:00
ynh_psql_execute_as_root --sql= 'CREATE INDEX idx_fileinfo_content_txt ON public.fileinfo USING gin (to_tsvector(' \' 'english' \' '::regconfig, content))' --database= $db_name
ynh_psql_execute_as_root --sql= 'CREATE INDEX idx_posts_message_txt ON public.posts USING gin (to_tsvector(' \' 'english' \' '::regconfig, (message)::text));' --database= $db_name
2022-09-27 16:53:38 +02:00
2023-03-06 22:26:35 +01:00
if ynh_compare_current_package_version --comparison eq --version 7.3.0~ynh1
then
# There is a problem with version 7.3.0 and the database migration.
# More information here: https://forum.mattermost.com/t/migrating-from-mariadb-to-postgresql-db/14194/6
ynh_psql_execute_as_root --sql= "DELETE FROM db_migrations WHERE version=92;" --database= $db_name
fi
# Remove the MariaDB database
ynh_mysql_remove_db --db_user= $mysql_db_user --db_name= $db_name
2022-06-28 23:12:39 +02:00
}
2021-01-11 23:46:48 +01:00
#=================================================
# EXPERIMENTAL HELPERS
#=================================================
#=================================================
# FUTURE OFFICIAL HELPERS
#=================================================