Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for dropping views in script: mysql-drop-tables #15

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions mysql/scripts/mysql-drop-tables
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,28 @@ if [[ "${BASH_SOURCE[0]}" = "$0" ]]; then
# Grep is wrapped in { COMMAND || true; } because it returns an exit code of 1 if no results are found.
# We have set -e for this script so if we don't do this wrapping the script will exit right here if no tables are found.
# This script will fail on new environments with an empty database and break initial imports.
TABLES=$($CONNECTION_STRING -e 'show tables' | awk '{ print $1}' | { grep -v '^Tables' || true; } )
TABLES=$($CONNECTION_STRING -e 'show full tables where Table_Type = "BASE TABLE"' | awk '{ print $1}' | { grep -v '^Tables' || true; } )

if [ "$TABLES" == "" ]
if [ "$TABLES" != "" ]
then
info "No tables found in $DATABASE_NAME database!"
exit 0
for table in $TABLES
do
info "Deleting $DATABASE_NAME/$table"
$CONNECTION_STRING -e "drop table $table"
done
fi

for table in $TABLES
do
info "Deleting $DATABASE_NAME/$table"
$CONNECTION_STRING -e "drop table $table"
done
# Grep is wrapped in { COMMAND || true; } because it returns an exit code of 1 if no results are found.
# We have set -e for this script so if we don't do this wrapping the script will exit right here if no tables are found.
# This script will fail on new environments with an empty database and break initial imports.
VIEWS=$($CONNECTION_STRING -e 'show full tables where Table_Type = "VIEW"' | awk '{ print $1}' | { grep -v '^Tables' || true; } )

if [ "$VIEWS" != "" ]
then
for view in $VIEWS
do
info "Deleting view $DATABASE_NAME/$view"
$CONNECTION_STRING -e "drop view $view"
done
fi
fi