From 10540621a8c743fffc72101a7d36c178d4a03088 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 14 Jul 2023 18:18:49 +0200 Subject: [PATCH] Add migration script for missing users DEFAULTs This commit adds a migration script to add missing DEFAULT values on the users table. INSERTs into the users table fail without these DEFAULTs on MariaDB in strict mode. --- scripts/db_migrations/version7.sh | 74 +++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100755 scripts/db_migrations/version7.sh diff --git a/scripts/db_migrations/version7.sh b/scripts/db_migrations/version7.sh new file mode 100755 index 0000000..cd2d7b6 --- /dev/null +++ b/scripts/db_migrations/version7.sh @@ -0,0 +1,74 @@ +#!/bin/sh +# CAcert WebDB - CAcert web application +# Copyright (C) 2023 CAcert Inc. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + + + +# script to do database migrations + +set -e # script fails if any command fails + +STDIN=0 +STDOUT=1 +STDERR=2 + +if [ "$1" = "--help" ]; then + cat >&$STDERR <<- USAGE + Usage: $0 [MARIADB_OPTIONS] + You have to specify all options needed by "mariadb" as if you had started + the MariaDB command line client directly (including the name of the + database to operate on). The MariaDB user used has to have enough + privileges to do all necessary operations (among others CREATE, ALTER, + DROP, UPDATE, INSERT, DELETE). + You might need to enter the mariadb password multiple times if you + specify the -p option. + USAGE + exit 1 +fi + +mariadb_opt=" --batch --skip-column-names $@" + +schema_version=$(mariadb $mariadb_opt <<- 'SQL' + + SELECT MAX(`version`) FROM `schema_version`; +SQL +) +if [ $schema_version != 6 ]; then + cat >&$STDERR <<- ERROR + Error: database schema is not in the right version to do the migration! + Expected version: 6 + ERROR + exit 2 +fi + +mariadb $mariadb_opt <<- 'SQL' +ALTER TABLE `users` ALTER `orgadmin` SET DEFAULT 0; +ALTER TABLE `users` ALTER `adadmin` SET DEFAULT 0; +ALTER TABLE `users` ALTER `locked` SET DEFAULT 0; +ALTER TABLE `users` ALTER `otphash` SET DEFAULT ''; +ALTER TABLE `users` ALTER `otppin` SET DEFAULT 0; +system echo "added missing default values on users table" + + -- Update schema version number + INSERT INTO `schema_version` + (`version`, `when`) VALUES + (7, CURRENT_TIMESTAMP); +SQL + + +echo "Database successfully migrated to version 7" +exit 0 +