Skip to content

Commit a720f34

Browse files
authored
Merge pull request #99 from itk-dev/feature/mysql-in-docker
Run mysql client inside mariadb container
2 parents 19062dc + 47151cd commit a720f34

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

completion/bash/itkdev-docker-compose-completion.bash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ _idc_completions()
99
fi
1010

1111
# Keep the suggestions in a local variable
12-
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]}"))
12+
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]}"))
1313

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

completion/zsh/_itkdev-docker-compose

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ _itkdev-docker-compose() {
3737
'sync[Sync both database and files.]' \
3838
'sync\:db[Sync database base.]' \
3939
'sync\:files[Sync files base.]' \
40+
'sql\:cli[Open mysql cli.]' \
4041
'sql\:connect[Print mysql command for connecting to database.]' \
4142
'sql\:log[Log SQL queries sent to database.]' \
4243
'sql\:port[Display the exposed MariaDB SQL server port.]' \

scripts/itkdev-docker-compose

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,18 @@ Commands:
186186
Sync files base on 'REMOTE_PATH' or 'LOCAL_PATH' in
187187
the env file.
188188
189+
sql:cli
190+
Open MySQL client to the database (named `db`) in the database
191+
container (`mariadb`).
192+
193+
Execute a SQL query from the command line:
194+
195+
itkdev-docker-compose sql:cli --table <<< 'SHOW TABLES'
196+
197+
Run a SQL script:
198+
199+
itkdev-docker-compose sql:cli < query.sql
200+
189201
sql:connect
190202
Print mysql command for connecting to database (named
191203
`db`) in the database container (`mariadb`).
@@ -564,7 +576,7 @@ case "$cmd" in
564576
fi
565577

566578
if command -v pv >/dev/null 2>&1; then
567-
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | pv | eval $(itkdev-docker-compose sql:connect)
579+
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | pv | $script_path sql:cli
568580
else
569581
cat <<EOF
570582
Protip: run
@@ -573,7 +585,7 @@ Protip: run
573585
574586
to show progress
575587
EOF
576-
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | eval $(itkdev-docker-compose sql:connect)
588+
ssh ${REMOTE_HOST} ${REMOTE_DB_DUMP_CMD} | $script_path sql:cli
577589
fi
578590

579591
if [ ! -z "${SYNC_DB_POST_SCRIPT:-}" ]; then
@@ -608,11 +620,19 @@ EOF
608620
eval rsync -avz ${excludes} ${REMOTE_HOST}:${REMOTE_PATH}/ ${docker_compose_dir}/${LOCAL_PATH}
609621
;;
610622

623+
sql:cli)
624+
db_service=mariadb
625+
exec_args=()
626+
# @see https://stackoverflow.com/a/2456870?
627+
if ! [ -t 0 ]; then
628+
exec_args+=("--no-TTY")
629+
fi
630+
docker compose exec "${exec_args[@]}" $db_service mysql --user=db --password=db --database=db "$@"
631+
;;
632+
611633
sql:connect)
612-
address=$(docker_compose port mariadb 3306)
613-
host=$(echo $address | cut -d: -f1)
614-
port=$(echo $address | cut -d: -f2)
615-
echo mysql --host=$host --port=$port --user=db --password=db db
634+
db_service=mariadb
635+
echo docker compose exec $db_service mysql --user=db --password=db db
616636
;;
617637

618638
sql:open)

0 commit comments

Comments
 (0)