Skip to content
Snippets Groups Projects
Select Git revision
  • c95488305e2f9e6b58ad79e8b704d34756edc983
  • main default protected
  • dev protected
  • f-linkahead-rename
  • f-real-id
  • f-filesystem-import
  • f-filesystem-link
  • f-filesystem-directory
  • f-filesystem-core
  • f-filesystem-cleanup
  • f-filesystem-main
  • f-name
  • keep_changes
  • f-permission-checks-2
  • f-mysql8-tests
  • f-retrieve-history
  • t-distinct-parents
  • v8.1.0
  • v8.0.0
  • v7.0.2
  • v7.0.1
  • v7.0.0
  • v6.0.1
  • v6.0.0
  • v5.0.0
  • v4.1.0
  • v4.0.0
  • v3.0
  • v2.0.30
29 results

make_db

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    make_db 6.40 KiB
    #!/bin/bash
    
    # ** header v3.0
    # This file is a part of the CaosDB Project.
    #
    # Copyright (C) 2019 Daniel Hornung, Göttingen
    # Copyright (C) 2020 Timm Fitschen <t.fitschen@indiscale.com>
    # Copyright (C) 2020 IndiScale <info@indiscale.com>
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU Affero General Public License as
    # published by the Free Software Foundation, either version 3 of the
    # License, or (at your option) any later version.
    #
    # 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 Affero General Public License for more details.
    #
    # You should have received a copy of the GNU Affero General Public License
    # along with this program. If not, see <https://www.gnu.org/licenses/>.
    #
    # ** end header
    
    # To fail fast, but beware https://mywiki.wooledge.org/BashFAQ/105
    echo "this is make_db"
    set -e
    
    function fail() {
      echo "Some error occured, exiting."
      exit 1
    }
    
    UNITTEST_DATABASE=${UNITTEST_DATABASE-_caosdb_schema_unit_tests}
    
    # options: [--fresh]
    function runtests() {
        DATABASE_NAME=$UNITTEST_DATABASE
        _setup_mytap
    
        _install_unit_test_database $@
    
        _execute_tests || ( echo "[FAILURE]" && exit 1 )
    
        # drop test database
        #drop "$UNITTEST_DATABASE"
    }
    
    function _execute_tests () {
        pushd tests
        TESTS="./test*.sql"
    
        rm -f .TEST_RESULTS
        for tfile in $TESTS ; do
            echo "Running $tfile"
            echo "----- $tfile -----" >> .TEST_RESULTS
            cat $tfile | $SQL --disable-pager --batch --raw --skip-column-names --unbuffered >> .TEST_RESULTS
        done;
    
        popd
        cat tests/.TEST_RESULTS
        grep -c -i "failed" tests/.TEST_RESULTS > /dev/null && return 1
        return 0
    }
    
    # install/reset database for unit tests.
    # options: [--fresh]
    function _install_unit_test_database () {
        if _db_exists "$SQL" "$DATABASE_NAME"; then
            if [[ "$1" == "--fresh" ]] ; then
                drop "$DATABASE_NAME" ;
            else
                return 0;
            fi
        fi
    
        sed "s/db_2_0/$UNITTEST_DATABASE/g" "$INSTALL_SQL_FILE" | $SQL
    
        # crate test user
        grant
    
        # update to latest
        cp .config .test_config
        echo "DATABASE_NAME=\"$UNITTEST_DATABASE\"" >> .test_config
        pushd patches
        ./applyPatches.sh --env=../.test_config
        popd
        rm .test_config
    }
    
    # install test framework MyTAP if not installed
    function _setup_mytap() {
        if _db_exists "$SQL" "tap" ; then
            echo MyTAB framework is already installed [OK]
            return 0
        fi
        echo -n "Installing MyTAB framework ... "
        pushd libs > /dev/null
        unzip -u mytap*.zip > /dev/null
        pushd mytap*/ > /dev/null
        $SQL < mytap.sql > /dev/null || exit 1
        popd > /dev/null
        rm -r mytap*/
        popd > /dev/null
        echo [DONE]
    }
    
    function setup_os() {
      # - mariadb-client :: For SQL server configuration.
      PACKAGES="git
      make
      mariadb-client
      maven
      openjdk-8-jdk-headless
      python3-pip
      screen
      unzip
    "
      # - vim :: For debugging
      PACKAGES+="vim
    "
      apt-get update
      apt-get dist-upgrade -y
      apt-get install -y $PACKAGES
    }
    
    function install_db() {
        if _db_exists ; then
            echo -e "\n
    A database with with the name '$DATABASE_NAME' exists already.
    Call 'make drop-$DATABASE_NAME' to delete that database or reconfigure with
    './configure'."
            exit 0
        fi
    
    	  sed "s/db_2_0/$DATABASE_NAME/g" "$INSTALL_SQL_FILE" | $SQL
    }
    
    # Inserts the dump (arg 1) into the database
    function restore_db() {
        SQL_FILE="$1"
        $SQL -D "$DATABASE_NAME" < "$SQL_FILE"
    }
    
    function test-connection() {
        echo "Test $SQL"
        $SQL -e "select 0;"
    }
    
    # Creates a user and grants it sufficient rights.
    # If called with --strict, the user is not allowed to exist previously.
    function grant() {
        if [ -z "$DATABASE_USER_PW" ] ; then
            while true; do
                read -p "Please enter the password for ${DATABASE_USER}: " \
                     -s password
                echo                
                read -p "Please repeat the password: " -s password2
                echo
                [[ "$password" == "$password2" ]] && {
                    DATABASE_USER_PW="$password"
                    break
                }
                echo -e "\nThe passwords didn't match. Try again."
            done
        fi
    
        if [[ "$1" == "--strict" ]] ; then
            for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
                CMD="SELECT COUNT(*) FROM mysql.user WHERE user='${DATABASE_USER}' AND host='${host}';"
    		        [[ $($SQL -s -N -e "$CMD") == 0 ]] || {
                    echo "The user '${DATABASE_USER}@${host}' is already in the database."
                    echo "Please use another user or delete it, e.g. with"
                    echo "'mysql -u ${MYSQL_USER} -p -e \"DROP USER ${DATABASE_USER}@${host};\"'"
                    exit 1
                }
            done
        fi
    
        for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
            echo "Granting admin privileges to '$DATABASE_USER'@'$host'"
            $SQL <<EOF
    CREATE USER IF NOT EXISTS
        '$DATABASE_USER'@'$host' identified by '$DATABASE_USER_PW';
    GRANT USAGE ON *.* TO '$DATABASE_USER'@'$host';
    GRANT ALL PRIVILEGES ON *.* TO '$DATABASE_USER'@'$host' WITH GRANT OPTION;
    GRANT EXECUTE ON *.* TO '$DATABASE_USER'@'$host';
    EOF
        done
    }
    
    # Drops the caosdb user plus a given database from all listed database hosts.
    # The first argument to this function is the database that shal be dropped.
    function drop() {
        DROPDB="$1"
        for host in ${DATABASE_USER_HOST_LIST//,/ } ; do
            $SQL -e "DROP USER '${DATABASE_USER}'@'${host}';" || true
        done
        "$MYSQLADMIN_CMD" $MYSQL_CONNECTION -f drop "$DROPDB"
    }
    
    
    # Returns 0 or non-zero, depending on whether the database exists already.
    # Optional parameters: [SQL [DATABASE_NAME]]
    function _db_exists() {
        ${1-${SQL}} -D "${2-${DATABASE_NAME}}" -e "show tables;" > /dev/null 2>&1 \
            && return 0 || return 1
    }
    
    echo "HIER1"
    
    . .config
    
    echo "HIER2"
    
    INSTALL_SQL_FILE="db_2_0.sql"
    if [ -z $LOGIN_PATH ] ; then
        MYSQL_CONNECTION="--host=${MYSQL_HOST} --port=${MYSQL_PORT}
     --user=${MYSQL_USER}
     --password=${MYSQL_USER_PASSWORD}"
    else
        MYSQL_CONNECTION="--login-path=$LOGIN_PATH"
    fi
    echo "HIER3"
    
    SQL="$MYSQL_CMD $MYSQL_CONNECTION"
    
    echo "HIER4"
    case $1 in
      "drop") drop $2 ;;
      "grant") grant $2 ;;
      "test") shift ; runtests $@ ;;
      "test-connection") test-connection ;;
      "install_db") install_db ;;
      "restore_db") restore_db $2 ;;
      # "prep_sql") prepare_sql ;;
      *) echo "Unknown action: $1"
    esac