Skip to content

Instantly share code, notes, and snippets.

@korniychuk
Created September 2, 2017 14:53
Show Gist options
  • Save korniychuk/d73f1c6b851066cc194a3d6fe2af8629 to your computer and use it in GitHub Desktop.
Save korniychuk/d73f1c6b851066cc194a3d6fe2af8629 to your computer and use it in GitHub Desktop.
check if mysql database exists
hasDatabase() {
DB_NAME=$1;
return $(MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" mysql -u${DB_USER} --skip-column-names --batch -e "SHOW DATABASES LIKE '${DB_NAME}'" | wc -l)
}
@korniychuk
Copy link
Author

checkIfMySQLTableExists() {
    table=$1;
    if [ $(mysql -N -s -u root -p -e \
        "select count(*) from information_schema.tables where \
            table_schema='${table}' and table_name='${DB}';") -eq 1 ]; then
        echo "Table ${table} exists! ...";
    else
        echo "Table ${table} does not exist! ..."
        exit 1
    fi

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment