From 3f46bc54ef8d5a5fedcf26defab8b4b08cbd51db Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Tue, 27 Oct 2020 11:47:45 -0400 Subject: [PATCH 1/4] docs: add snippet about db migrator --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 113149f..6acf312 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,11 @@ popular frameworks like Laravel and Yii2. The examples make the assumption that any domain used for local development is already added to the `/etc/hosts` file. You can edit to add `127.0.0.1 domain.docker`. +## Scripts +Inside the `scripts` folder you will find one-off scripts to help with tasks. + + * `db_migrate.sh` - Helps migrate databases between versions of mysql. + ## Docs * [Setting up Nginx-Proxy](docs/nginx-proxy/README.md) * [Setting up PHP Testing in PHPStorm](docs/phpstorm-docker/README.md) From 34d6eb8407b20f055ad65928da5d5dc7454ac587 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Tue, 27 Oct 2020 11:48:01 -0400 Subject: [PATCH 2/4] feat: add SDIT DB Migrator (mysql so far) --- scripts/db_migrate.sh | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100755 scripts/db_migrate.sh diff --git a/scripts/db_migrate.sh b/scripts/db_migrate.sh new file mode 100755 index 0000000..103bfa1 --- /dev/null +++ b/scripts/db_migrate.sh @@ -0,0 +1,52 @@ +#!/bin/bash + +# Variable Setup +NEWLINE=$'\n' + +DB_BINARY_NAME='' +DB_PORT='' +DB_HOST='127.0.0.1' +DB_USER='root' +DB_PASS='root' +DB_EXPORT_FLAGS='--column-statistics=0 --quick --hex-blob --single-transaction' +DB_IMPORT_FLAGS='' + +# Determine DB +echo "Welcome to the SDIT DB Migrator (mysql/postgres) $NEWLINE" +read -rp "Select (mysql|postgres):" DBMODE +read -rp "Enter (FROM) port number:" DB_PORT +read -rp "Enter (FROM) database name: " DB_NAME +read -rp "Enter (TO) port number:" DB_TO_PORT +read -rp "Enter (TO) database name: " DB_TO_NAME + +case $DBMODE in + + mysql|maria|mariadb) + DB_BINARY_NAME='mysql' + + # Attempt to connect to DB (FROM and TO). + if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_PORT}" -e "use ${DB_NAME}"; then + echo "Database (FROM) could not be connected ($DB_NAME, $DB_PORT). Aborting...$NEWLINE" + exit 1; + fi + + if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_TO_PORT}" -e "use ${DB_TO_NAME}"; then + echo "Database (TO) could not be connected ($DB_TO_NAME, $DB_TO_PORT). Aborting...$NEWLINE" + exit 1; + fi + + echo -n "Copying...! $NEWLINE" + mysqldump $DB_EXPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_PORT}" "${DB_NAME}" | mysql $DB_IMPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_TO_PORT}" "${DB_TO_NAME}" + echo -n "Copied...! $NEWLINE" + ;; + + pg|postgres) + echo -n "Postgres support is still in progress. Aborting...$NEWLINE" + ;; + + *) + echo -n "This selection is unknown. Aborting...$NEWLINE" + exit 1; +esac + +exit 0; From 1f465ec08aee6bada8240245234dbe7aa2a3eff0 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Tue, 27 Oct 2020 12:44:04 -0400 Subject: [PATCH 3/4] fix: ensure we migrate routines/procedures --- scripts/db_migrate.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/db_migrate.sh b/scripts/db_migrate.sh index 103bfa1..e098857 100755 --- a/scripts/db_migrate.sh +++ b/scripts/db_migrate.sh @@ -8,7 +8,7 @@ DB_PORT='' DB_HOST='127.0.0.1' DB_USER='root' DB_PASS='root' -DB_EXPORT_FLAGS='--column-statistics=0 --quick --hex-blob --single-transaction' +DB_EXPORT_FLAGS='--routines --column-statistics=0 --quick --hex-blob --single-transaction' DB_IMPORT_FLAGS='' # Determine DB From 4692b4c444d2293e9eed754a7b9befa555139f64 Mon Sep 17 00:00:00 2001 From: Connor Tumbleson Date: Wed, 28 Oct 2020 11:34:26 -0400 Subject: [PATCH 4/4] style: 4spaces, align variable with DB_ prefix --- scripts/db_migrate.sh | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/scripts/db_migrate.sh b/scripts/db_migrate.sh index e098857..f7f5892 100755 --- a/scripts/db_migrate.sh +++ b/scripts/db_migrate.sh @@ -13,40 +13,40 @@ DB_IMPORT_FLAGS='' # Determine DB echo "Welcome to the SDIT DB Migrator (mysql/postgres) $NEWLINE" -read -rp "Select (mysql|postgres):" DBMODE +read -rp "Select (mysql|postgres):" DB_MODE read -rp "Enter (FROM) port number:" DB_PORT read -rp "Enter (FROM) database name: " DB_NAME read -rp "Enter (TO) port number:" DB_TO_PORT read -rp "Enter (TO) database name: " DB_TO_NAME -case $DBMODE in +case $DB_MODE in + mysql|maria|mariadb) + DB_BINARY_NAME='mysql' - mysql|maria|mariadb) - DB_BINARY_NAME='mysql' + # Attempt to connect to DB (FROM and TO). + if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_PORT}" -e "use ${DB_NAME}"; then + echo "Database (FROM) could not be connected ($DB_NAME, $DB_PORT). Aborting...$NEWLINE" + exit 1 + fi - # Attempt to connect to DB (FROM and TO). - if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_PORT}" -e "use ${DB_NAME}"; then - echo "Database (FROM) could not be connected ($DB_NAME, $DB_PORT). Aborting...$NEWLINE" - exit 1; - fi + if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_TO_PORT}" -e "use ${DB_TO_NAME}"; then + echo "Database (TO) could not be connected ($DB_TO_NAME, $DB_TO_PORT). Aborting...$NEWLINE" + exit 1 + fi - if ! $DB_BINARY_NAME --host="${DB_HOST}" --user="${DB_USER}" --password="${DB_PASS}" --port="${DB_TO_PORT}" -e "use ${DB_TO_NAME}"; then - echo "Database (TO) could not be connected ($DB_TO_NAME, $DB_TO_PORT). Aborting...$NEWLINE" - exit 1; - fi - - echo -n "Copying...! $NEWLINE" - mysqldump $DB_EXPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_PORT}" "${DB_NAME}" | mysql $DB_IMPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_TO_PORT}" "${DB_TO_NAME}" - echo -n "Copied...! $NEWLINE" + echo -n "Copying...! $NEWLINE" + mysqldump $DB_EXPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_PORT}" "${DB_NAME}" | mysql $DB_IMPORT_FLAGS --user="${DB_USER}" --password="${DB_PASS}" --host="${DB_HOST}" --port="${DB_TO_PORT}" "${DB_TO_NAME}" + echo -n "Copied...! $NEWLINE" ;; - pg|postgres) - echo -n "Postgres support is still in progress. Aborting...$NEWLINE" + pg | postgres) + echo -n "Postgres support is still in progress. Aborting...$NEWLINE" ;; - *) - echo -n "This selection is unknown. Aborting...$NEWLINE" - exit 1; + *) + echo -n "This selection is unknown. Aborting...$NEWLINE" + exit 1 + ;; esac -exit 0; +exit 0