Skip to content

Commit

Permalink
Refactored and added sql:cli command
Browse files Browse the repository at this point in the history
  • Loading branch information
rimi-itk committed May 29, 2024
1 parent 5b12210 commit 1504f88
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion completion/bash/itkdev-docker-compose-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ _idc_completions()
fi

# Keep the suggestions in a local variable
local suggestions=($(compgen -W "dory:start dory:stop url open drush nfs:enable template:install traefik:start traefik:stop traefik:open traefik:url mail:open mail:url mailhog:open mailhog:url sql:connect sql:log sql:port sql:open xdebug xdebug3 hosts:insert self:update sync sync:db sync:files images:pull composer php version bin/console" -- "${COMP_WORDS[1]}"))
local suggestions=($(compgen -W "dory:start dory:stop url open drush nfs:enable template:install traefik:start traefik:stop traefik:open traefik:url mail:open mail:url mailhog:open mailhog:url sql:cli sql:connect sql:log sql:port sql:open xdebug xdebug3 hosts:insert self:update sync sync:db sync:files images:pull composer php version bin/console" -- "${COMP_WORDS[1]}"))

COMPREPLY=("${suggestions[@]}")

Expand Down
1 change: 1 addition & 0 deletions completion/zsh/_itkdev-docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ _itkdev-docker-compose() {
'sync[Sync both database and files.]' \
'sync\:db[Sync database base.]' \
'sync\:files[Sync files base.]' \
'sql\:cli[Open mysql cli.]' \
'sql\:connect[Print mysql command for connecting to database.]' \
'sql\:log[Log SQL queries sent to database.]' \
'sql\:port[Display the exposed MariaDB SQL server port.]' \
Expand Down
36 changes: 25 additions & 11 deletions scripts/itkdev-docker-compose
Original file line number Diff line number Diff line change
Expand Up @@ -186,20 +186,24 @@ Commands:
Sync files base on 'REMOTE_PATH' or 'LOCAL_PATH' in
the env file.
sql:connect
Print mysql command for connecting to database (named
`db`) in the database container (`mariadb`).
Use `$(itkdev-docker-compose sql:connect)` to open the
database cli.
sql:cli
Open MySQL client to the database (named `db`) in the database
container (`mariadb`).
Execute a SQL query from the command line:
$(itkdev-docker-compose sql:connect) --table <<< 'SHOW TABLES'
itkdev-docker-compose sql:cli --table <<< 'SHOW TABLES'
Run a SQL script:
$(itkdev-docker-compose sql:connect) < query.sql
itkdev-docker-compose sql:cli < query.sql
sql:connect
Print mysql command for connecting to database (named
`db`) in the database container (`mariadb`).
Use `$(itkdev-docker-compose sql:connect)` to open the
database cli.
sql:log
Log SQL queries sent to database.
Expand Down Expand Up @@ -572,7 +576,7 @@ case "$cmd" in
fi

if command -v pv >/dev/null 2>&1; then
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | pv | eval $(itkdev-docker-compose sql:connect)
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | pv | itkdev-docker-compose sql:cli
else
cat <<EOF
Protip: run
Expand All @@ -581,7 +585,7 @@ Protip: run
to show progress
EOF
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | eval $(itkdev-docker-compose sql:connect)
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | itkdev-docker-compose sql:cli
fi

if [ ! -z "${SYNC_DB_POST_SCRIPT:-}" ]; then
Expand Down Expand Up @@ -616,9 +620,19 @@ EOF
eval rsync -avz ${excludes} ${REMOTE_HOST}:${REMOTE_PATH}/ ${docker_compose_dir}/${LOCAL_PATH}
;;

sql:cli)
db_service=mariadb
exec_args=()
# @see https://stackoverflow.com/a/2456870?
if ! [ -t 0 ]; then
exec_args+=("--no-TTY")
fi
docker compose exec "${exec_args[@]}" $db_service mysql --user=db --password=db --database=db "$@"
;;

sql:connect)
db_service=mariadb
echo docker compose exec --no-TTY $db_service mysql --user=db --password=db db
echo docker compose exec $db_service mysql --user=db --password=db db
;;

sql:open)
Expand Down

0 comments on commit 1504f88

Please sign in to comment.