sqlmig: support des mises à jour administratives

This commit is contained in:
Jephté Clain 2018-05-03 14:17:30 +04:00
parent b5e2417514
commit f1dca506fb
1 changed files with 215 additions and 121 deletions

336
sqlmig
View File

@ -161,6 +161,33 @@ function abort_on_error() {
[ -z "$force" ] && die "$@"
}
function ensure_dbtype() {
local dir="$1" type="$2"
if [ "$type" == auto ]; then
if [ -f "$dir/my.cnf" ]; then
dbtype=mysql
elif [ -f "$dir/ora.conf" ]; then
dbtype=oracle
else
die "Vous devez spécifier le type --mysql ou --oracle"
fi
else
dbtype="$type"
fi
}
function ensure_dbmode() {
local dbtype="$1" mode="$2"
if [ "$mode" == auto ]; then
if [ "$dbtype" == mysql ]; then
is_root && dbmode=prod || dbmode=devel
else
dbmode=prod
fi
else
dbmode="$mode"
fi
}
################################################################################
# MySQL
@ -261,6 +288,73 @@ function mysql_after_update() {
fi
}
function mysql_set_adminargs() {
local dir="$1"
adminargs=()
if [ -f "$dir/my.cnf" ]; then
array_add adminargs --defaults-file="$dir/my.cnf"
if [ -n "$admindefaults" ]; then
array_add adminargs --defaults-extra-file="$admindefaults"
fi
elif [ -n "$admindefaults" ]; then
array_add adminargs --defaults-file="$admindefaults"
fi
[ ${#adminargs[*]} -gt 0 ] || array_add adminargs --default-character-set utf8
}
function mysql_set_userargs() {
local dir="$1" dbname="$2"
userargs=()
if [ -f "$dir/my.cnf" ]; then
array_add userargs --defaults-file="$dir/my.cnf"
if [ -n "$userdefaults" ]; then
array_add userargs --defaults-extra-file="$userdefaults"
elif [ -f "$dir/my-${dbname}.cnf" ]; then
array_add userargs --defaults-extra-file="$dir/my-${dbname}.cnf"
fi
elif [ -n "$userdefaults" ]; then
array_add userargs --defaults-file="$userdefaults"
elif [ -f "$dir/my-${dbname}.cnf" ]; then
array_add userargs --defaults-file="$dir/my-${dbname}.cnf"
fi
[ ${#userargs[*]} -gt 0 ] || array_add userargs --default-character-set utf8
}
function mysql_set_mysqlargs() {
mysqlargs=()
[ -n "$user" ] && array_add mysqlargs -u "$user"
[ -n "$pwset" ] && array_add mysqlargs -p"$password"
[ -n "$host" ] && array_add mysqlargs -h "$host"
[ -n "$port" ] && array_add mysqlargs -P "$port"
[ -n "$socket" ] && array_add mysqlargs -S "$socket"
[ -n "$charset" ] && array_add mysqlargs --default-character-set "$charset"
[ -n "$force" ] && array_add mysqlargs -f
array_add mysqlargs -B
}
MYSQL_ADMIN_CONF_DONE=
function mysql_admin_update() {
local name="$1" update="$2" done
if [ -z "$MYSQL_ADMIN_CONF_DONE" ]; then
MYSQL_ADMIN_CONF_DONE=1
mysql_tbconf
fi
setx done=mysql_get_done "$name" || die
[ -n "$done" ] && return
estep "$name"
mysql_before_update || die
cat "$update" | mysql_admin_ve || abort_on_error
mysql_after_update || die
}
function mysql_user_update() {
local name="$1" update="$2" dbname="$3" done
setx done=mysql_get_done "$name" "$dbname" || die
[ -n "$done" ] && return
estep "$name"
mysql_before_update "$dbname" || die
cat "$update" | mysql_user_ve || abort_on_error
mysql_after_update "$dbname" || die
}
################################################################################
# Oracle
@ -473,6 +567,81 @@ commit;"
fi
}
function oracle_ensure_opdir() {
if [ -z "$OPDIR" ]; then
ac_set_tmpdir OPDIR
>"$SQLMIGLOG"
fi
}
function oracle_source_adminconf() {
local dir="$1"
unset ORACLE_SID NLS_LANG ADMINCONNECT USERCONNECT SQLMIGLOG
[ -f "$dir/ora.conf" ] && source "$dir/ora.conf"
[ -n "$admindefaults" ] && {
source "$admindefaults" || die
}
[ -n "$oracle_sid" ] && ORACLE_SID="$oracle_sid"
[ -n "$nls_lang" ] && NLS_LANG="$nls_lang"
export ORACLE_SID NLS_LANG
if [ -n "$ADMINCONNECT" ]; then
ADMINDBA=
else
ADMINCONNECT=/
ADMINDBA=1
fi
[ -n "$SQLMIGLOG" ] || SQLMIGLOG="/tmp/sqlmig-${ORACLE_SID}-${dbname}.log"
}
function oracle_source_userconf() {
local dir="$1" dbname="$2"
unset ORACLE_SID NLS_LANG ADMINCONNECT USERCONNECT SQLMIGLOG
[ -f "$dir/ora.conf" ] && source "$dir/ora.conf"
[ -f "$dir/ora-${dbname}.conf" ] && source "$dir/ora-${dbname}.conf"
[ -n "$userdefaults" ] && {
source "$userdefaults" || die
}
[ -n "$oracle_sid" ] && ORACLE_SID="$oracle_sid"
[ -n "$nls_lang" ] && NLS_LANG="$nls_lang"
export ORACLE_SID NLS_LANG
if [ -n "$ADMINCONNECT" ]; then
ADMINDBA=
else
ADMINCONNECT=/
ADMINDBA=1
fi
if [ -n "$USERCONNECT" ]; then
USERDBA=
else
USERCONNECT=/
USERDBA=1
fi
[ -n "$SQLMIGLOG" ] || SQLMIGLOG="/tmp/sqlmig-${ORACLE_SID}-${dbname}.log"
}
ORACLE_ADMIN_CONF_DONE=
function oracle_admin_update() {
local name="$1" update="$2" done
if [ -z "$ORACLE_ADMIN_CONF_DONE" ]; then
ORACLE_ADMIN_CONF_DONE=
oracle_tbconf
fi
setx done=oracle_get_done "$name" || die
[ -n "$done" ] && return
estep "$name"
oracle_before_update || die
cat "$update" | oracle_admin_ve || abort_on_error
oracle_after_update || die
}
function oracle_user_update() {
local name="$1" update="$2" dbname="$3" done
setx done=oracle_get_done "$name" "$dbname" || die
[ -n "$done" ] && return
estep "$name"
oracle_before_update "$dbname" || die
cat "$update" | oracle_user_ve || abort_on_error
oracle_after_update "$dbname" || die
}
################################################################################
admindefaults=
@ -722,32 +891,49 @@ fi
# répertoire temporaire pour Oracle
OPDIR=
if [ -n "$updatedir" ]; then
## mises à jour administratives
array_lsfiles updates "$updatedir" "*.sql"
if [ ${#updates[*]} -gt 0 ]; then
ensure_dbtype "$updatedir" "$type"
if [ "$dbtype" == mysql ]; then
# construire les paramètres pour mysql
mysql_set_adminargs "$updatedir"
mysql_set_mysqlargs
# Mises à jour
etitled "Mises à jour admin"
for update in "${updates[@]}"; do
setx name=basename "$update"
mysql_admin_update "$name" "$update"
done
eend; eclearp
elif [ "$dbtype" == oracle ]; then
# lire les paramètres
oracle_source_adminconf "$updatedir"
oracle_ensure_opdir
# Mises à jour
etitled "Mises à jour"
for update in "${updates[@]}"; do
setx name=basename "$update"
oracle_admin_update "$name" "$update"
done
eend; eclearp
fi
fi
fi
## mises à jour utilisateur
force_dbname="$dbname"
for dbdir in "${dbdirs[@]}"; do
dbname="$force_dbname"
[ -n "$dbname" ] || setx dbname=basename "$dbdir"
etitle "$dbname"
if [ "$type" == auto ]; then
if [ -f "$dbdir/my.cnf" ]; then
dbtype=mysql
elif [ -f "$dbdir/ora.conf" ]; then
dbtype=oracle
else
die "Vous devez spécifier le type --mysql ou --oracle"
fi
else
dbtype="$type"
fi
if [ "$mode" == auto ]; then
if [ "$dbtype" == mysql ]; then
is_root && dbmode=prod || dbmode=devel
else
dbmode=prod
fi
else
dbmode="$mode"
fi
ensure_dbtype "$dbdir" "$type"
ensure_dbmode "$dbtype" "$mode"
# lister les mises à jour disponibles
drops=()
@ -767,41 +953,9 @@ for dbdir in "${dbdirs[@]}"; do
############################################################################
if [ "$dbtype" == mysql ]; then
# construire les paramètres pour mysql
adminargs=()
if [ -f "$dbdir/my.cnf" ]; then
array_add adminargs --defaults-file="$dbdir/my.cnf"
if [ -n "$admindefaults" ]; then
array_add adminargs --defaults-extra-file="$admindefaults"
fi
elif [ -n "$admindefaults" ]; then
array_add adminargs --defaults-file="$admindefaults"
fi
[ ${#adminargs[*]} -gt 0 ] || array_add adminargs --default-character-set utf8
userargs=()
if [ -f "$dbdir/my.cnf" ]; then
array_add userargs --defaults-file="$dbdir/my.cnf"
if [ -n "$userdefaults" ]; then
array_add userargs --defaults-extra-file="$userdefaults"
elif [ -f "$dbdir/my-${dbname}.cnf" ]; then
array_add userargs --defaults-extra-file="$dbdir/my-${dbname}.cnf"
fi
elif [ -n "$userdefaults" ]; then
array_add userargs --defaults-file="$userdefaults"
elif [ -f "$dbdir/my-${dbname}.cnf" ]; then
array_add userargs --defaults-file="$dbdir/my-${dbname}.cnf"
fi
[ ${#userargs[*]} -gt 0 ] || array_add userargs --default-character-set utf8
mysqlargs=()
[ -n "$user" ] && array_add mysqlargs -u "$user"
[ -n "$pwset" ] && array_add mysqlargs -p"$password"
[ -n "$host" ] && array_add mysqlargs -h "$host"
[ -n "$port" ] && array_add mysqlargs -P "$port"
[ -n "$socket" ] && array_add mysqlargs -S "$socket"
[ -n "$charset" ] && array_add mysqlargs --default-character-set "$charset"
[ -n "$force" ] && array_add mysqlargs -f
array_add mysqlargs -B
mysql_set_adminargs "$dbdir"
mysql_set_userargs "$dbdir" "$dbname"
mysql_set_mysqlargs
# Suppression
if [ -n "$drop" ]; then
@ -835,29 +989,12 @@ for dbdir in "${dbdirs[@]}"; do
# Mises à jour
etitled "Mises à jour"
mysql_tbconf "$dbname"
admin_conf=1
for update in "${updates[@]}"; do
setx name=basename "$update"
if have_tag admin "$update"; then
if [ -n "$admin_conf" ]; then
admin_conf=
mysql_tbconf
fi
setx done=mysql_get_done "$name" || die
[ -n "$done" ] && continue
estep "$name"
mysql_before_update || die
cat "$update" | mysql_admin_ve || abort_on_error
mysql_after_update || die
mysql_admin_update "$name" "$update"
else
setx done=mysql_get_done "$name" "$dbname" || die
[ -n "$done" ] && continue
estep "$name"
mysql_before_update "$dbname" || die
cat "$update" | mysql_user_ve || abort_on_error
mysql_after_update "$dbname" || die
mysql_user_update "$name" "$update" "$dbname"
fi
done
eend; eclearp
@ -867,34 +1004,8 @@ for dbdir in "${dbdirs[@]}"; do
[ -n "$OPDIR" ] || ac_set_tmpdir OPDIR
# lire les paramètres
unset ORACLE_SID NLS_LANG ADMINCONNECT USERCONNECT SQLMIGLOG
if [ -n "$admindefaults" ]; then
source "$admindefaults" || die
elif [ -f "$dbdir/ora.conf" ]; then
source "$dbdir/ora.conf"
fi
if [ -n "$userdefaults" ]; then
source "$userdefaults" || die
elif [ -f "$dbdir/ora-${dbname}.conf" ]; then
source "$dbdir/ora-${dbname}.conf"
fi
[ -n "$oracle_sid" ] && ORACLE_SID="$oracle_sid"
[ -n "$nls_lang" ] && NLS_LANG="$nls_lang"
export ORACLE_SID NLS_LANG
if [ -n "$ADMINCONNECT" ]; then
ADMINDBA=
else
ADMINCONNECT=/
ADMINDBA=1
fi
if [ -n "$USERCONNECT" ]; then
USERDBA=
else
USERCONNECT=/
USERDBA=1
fi
[ -n "$SQLMIGLOG" ] || SQLMIGLOG="/tmp/sqlmig-${ORACLE_SID}-${dbname}.log"
>"$SQLMIGLOG"
oracle_source_userconf "$dbdir" "$dbname"
oracle_ensure_opdir
# Suppression
if [ -n "$drop" ]; then
@ -928,29 +1039,12 @@ for dbdir in "${dbdirs[@]}"; do
# Mises à jour
etitled "Mises à jour"
oracle_tbconf "$dbname"
admin_conf=1
for update in "${updates[@]}"; do
setx name=basename "$update"
if have_tag admin "$update"; then
if [ -n "$admin_conf" ]; then
admin_conf=
oracle_tbconf
fi
setx done=oracle_get_done "$name" || die
[ -n "$done" ] && continue
estep "$name"
oracle_before_update || die
cat "$update" | oracle_admin_ve || abort_on_error
oracle_after_update || die
oracle_admin_update "$name" "$update"
else
setx done=oracle_get_done "$name" "$dbname" || die
[ -n "$done" ] && continue
estep "$name"
oracle_before_update "$dbname" || die
cat "$update" | oracle_user_ve || abort_on_error
oracle_after_update "$dbname" || die
oracle_user_update "$name" "$update" "$dbname"
fi
done
eend; eclearp