diff --git a/.github/workflows/pr-core-tests.yml b/.github/workflows/pr-core-tests.yml index 675344605..cd18a7bcd 100644 --- a/.github/workflows/pr-core-tests.yml +++ b/.github/workflows/pr-core-tests.yml @@ -31,6 +31,7 @@ jobs: - examples/orchestrator - examples/plugin-commands - examples/services + - examples/sql-helpers - examples/tooling node-version: - "18" diff --git a/CHANGELOG.md b/CHANGELOG.md index ce9623394..6231f67a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }}) +* Added `leia` tests for `sql-import.sh` and `sql-export.sh` +* Improved `sql-import.sh` to drop and recreate `mysql` and `mariadb` tables before importing + ## v3.21.0-beta.19 - [May 9, 2024](https://github.com/lando/core/releases/tag/v3.21.0-beta.19) ### New Features diff --git a/examples/sql-helpers/.gitignore b/examples/sql-helpers/.gitignore new file mode 100644 index 000000000..39d63ac45 --- /dev/null +++ b/examples/sql-helpers/.gitignore @@ -0,0 +1 @@ +sqlhelpers diff --git a/examples/sql-helpers/.lando.sqlhelpers.yml b/examples/sql-helpers/.lando.sqlhelpers.yml new file mode 100644 index 000000000..42acb1eac --- /dev/null +++ b/examples/sql-helpers/.lando.sqlhelpers.yml @@ -0,0 +1,58 @@ +name: lando-sqlhelpers +services: + mariadb: + api: 3 + type: lando + healthcheck: mysqladmin ping -h mariadb -u test -ptest + services: + image: bitnami/mariadb:10.4 + command: /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh + environment: + ALLOW_EMPTY_PASSWORD: yes + MARIADB_DATABASE: lando_test + MYSQL_DATABASE: lando_test + MARIADB_USER: test + MARIADB_PASSWORD: test + + mysql57: + api: 3 + type: lando + healthcheck: mysqladmin ping -h mysql57 -u test -ptest + services: + image: bitnami/mysql:5.7 + command: /opt/bitnami/scripts/mysql/entrypoint.sh /opt/bitnami/scripts/mysql/run.sh + environment: + ALLOW_EMPTY_PASSWORD: yes + MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password + MYSQL_DATABASE: lando_test + MYSQL_PASSWORD: test + MYSQL_USER: test + + mysql80: + api: 3 + type: lando + healthcheck: mysqladmin ping -h mysql80 -u test -ptest + services: + image: bitnami/mysql:8.0 + command: /opt/bitnami/scripts/mysql/entrypoint.sh /opt/bitnami/scripts/mysql/run.sh + environment: + ALLOW_EMPTY_PASSWORD: yes + MYSQL_AUTHENTICATION_PLUGIN: caching_sha2_password + MYSQL_DATABASE: lando_test + MYSQL_PASSWORD: test + MYSQL_USER: test + + postgres16: + api: 3 + type: lando + healthcheck: pg_isready -h postgres16 -U postgres + services: + image: bitnami/postgresql:16 + command: /opt/bitnami/scripts/postgresql/entrypoint.sh /opt/bitnami/scripts/postgresql/run.sh + environment: + ALLOW_EMPTY_PASSWORD: yes + POSTGRESQL_DATABASE: lando_test + POSTGRES_DB: lando_test + +plugins: + "@lando/core": "../../.." diff --git a/examples/sql-helpers/README.md b/examples/sql-helpers/README.md new file mode 100644 index 000000000..8be680250 --- /dev/null +++ b/examples/sql-helpers/README.md @@ -0,0 +1,101 @@ +SQL Import/Export Example +============ + +This example exists primarily to test the `sql-import.sh` and `sql-export.sh` helper scripts. + +Start up tests +-------------- + +```bash +# Should init and start a lando app +rm -rf sqlhelpers && mkdir -p sqlhelpers +cp -rf .lando.sqlhelpers.yml sqlhelpers/.lando.yml +cp -rf testdata1.sql sqlhelpers/testdata1.sql +cp -rf testdata2.sql sqlhelpers/testdata2.sql +cd sqlhelpers && lando start +``` + +Verification commands +--------------------- + +Run the following commands to verify things work as expected + +```bash +# Should import test data into mariadb +cd sqlhelpers +lando ssh -s mariadb -c "/helpers/sql-import.sh testdata1.sql" +lando ssh -s mariadb -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original" + +# Should import test data into mysql57 +cd sqlhelpers +lando ssh -s mysql57 -c "/helpers/sql-import.sh testdata1.sql" +lando ssh -s mysql57 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original" + +# Should import test data into mysql80 +cd sqlhelpers +lando ssh -s mysql80 -c "/helpers/sql-import.sh testdata1.sql" +lando ssh -s mysql80 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original" + +# Should import test data into postgres16 +cd sqlhelpers +lando ssh -s postgres16 -c "/helpers/sql-import.sh testdata1.sql" +lando ssh -s postgres16 -c "psql -U postgres -d lando_test -c 'select * from lando_test'" | grep "lando_original" + +# Should export gzipped files from mariadb +cd sqlhelpers +lando ssh -s mariadb -c "/helpers/sql-export.sh mariadb_dump.sql" -u root +gzip -d mariadb_dump.sql.gz + +# Should export gzipped files from mysql57 +cd sqlhelpers +lando ssh -s mysql57 -c "/helpers/sql-export.sh mysql57_dump.sql" -u root +gzip -d mysql57_dump.sql.gz + +# Should export gzipped files from mysql80 +cd sqlhelpers +lando ssh -s mysql80 -c "/helpers/sql-export.sh mysql80_dump.sql" -u root +gzip -d mysql80_dump.sql.gz + +# Should export gzipped files from postgres16 +cd sqlhelpers +lando ssh -s postgres16 -c "/helpers/sql-export.sh postgres16_dump.sql" -u root +gzip -d postgres16_dump.sql.gz + +# Should have the correct data in all exported files +cd sqlhelpers +grep "lando_original" mariadb_dump.sql +grep "lando_original" mysql57_dump.sql +grep "lando_original" mysql80_dump.sql +grep "lando_original" postgres16_dump.sql + +# Should be able to replace data with testdata2 in mariadb +cd sqlhelpers +lando ssh -s mariadb -c "/helpers/sql-import.sh testdata2.sql" +lando ssh -s mariadb -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated" + +# Should be able to replace data with testdata2 in mysql57 +cd sqlhelpers +lando ssh -s mysql57 -c "/helpers/sql-import.sh testdata2.sql" +lando ssh -s mysql57 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated" + +# Should be able to replace data with testdata2 in mysql80 +cd sqlhelpers +lando ssh -s mysql80 -c "/helpers/sql-import.sh testdata2.sql" +lando ssh -s mysql80 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated" + +# Should be able to replace data with testdata2 in postgres16 +cd sqlhelpers +lando ssh -s postgres16 -c "/helpers/sql-import.sh testdata2.sql" +lando ssh -s postgres16 -c "psql -U postgres -d lando_test -c 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated" +``` + +Destroy tests +------------- + +```bash +# Should destroy sqlhelpers successfully +cd sqlhelpers && lando destroy -y + +# Should poweroff +lando poweroff +``` diff --git a/examples/sql-helpers/testdata1.sql b/examples/sql-helpers/testdata1.sql new file mode 100644 index 000000000..deaef96d4 --- /dev/null +++ b/examples/sql-helpers/testdata1.sql @@ -0,0 +1,6 @@ +CREATE TABLE lando_test ( + id SERIAL PRIMARY KEY, + column1 VARCHAR(255) DEFAULT NULL, + column2 VARCHAR(255) DEFAULT NULL +); +INSERT INTO lando_test VALUES (1, 'lando_original', 'lando_original'); diff --git a/examples/sql-helpers/testdata2.sql b/examples/sql-helpers/testdata2.sql new file mode 100644 index 000000000..36d757c4d --- /dev/null +++ b/examples/sql-helpers/testdata2.sql @@ -0,0 +1,6 @@ +CREATE TABLE lando_test ( + id SERIAL PRIMARY KEY, + column1 VARCHAR(255) DEFAULT NULL, + column2 VARCHAR(255) DEFAULT NULL +); +INSERT INTO lando_test VALUES (1, 'lando_updated', 'lando_updated'); diff --git a/scripts/sql-import.sh b/scripts/sql-import.sh index 688f4a18d..6db2c2b7f 100755 --- a/scripts/sql-import.sh +++ b/scripts/sql-import.sh @@ -95,34 +95,21 @@ echo "Preparing to import $FILE into database '$DATABASE' on service '$SERVICE' # Wipe the database if set if [ "$WIPE" == "true" ]; then - echo "" - echo "Emptying $DATABASE... " + lando_pink "\nEmptying $DATABASE... " lando_yellow "NOTE: See the --no-wipe flag to avoid this step!" # DO db specific wiping if [[ ${POSTGRES_DB} != '' ]]; then # Drop and recreate database - lando_yellow "\t\tDropping database ...\n\n" - psql postgresql://$USER@$HOST:$PORT/postgres -c "drop database $DATABASE" - - lando_green "\t\tCreating database ...\n\n" - psql postgresql://$USER@$HOST:$PORT/postgres -c "create database $DATABASE" + psql postgresql://$USER@$HOST:$PORT/postgres -c "DROP DATABASE IF EXISTS $DATABASE" --quiet --echo-errors + psql postgresql://$USER@$HOST:$PORT/postgres -c "CREATE DATABASE $DATABASE" --quiet --echo-errors else - # Build the SQL prefix - SQLSTART="mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS} $DATABASE" - - # Gather and destroy tables - TABLES=$($SQLSTART -e 'SHOW TABLES' | awk '{ print $1}' | grep -v '^Tables' || true) - - # PURGE IT ALL! Drop views and tables as needed - for t in $TABLES; do - echo "Dropping $t from $DATABASE database..." - $SQLSTART <<-EOF - SET FOREIGN_KEY_CHECKS=0; - DROP VIEW IF EXISTS \`$t\`; - DROP TABLE IF EXISTS \`$t\`; -EOF - done + # Connection string + SQLSTART="mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS}" + + # Drop and recreate database + $SQLSTART -e "DROP DATABASE IF EXISTS ${DATABASE}" + $SQLSTART -e "CREATE DATABASE ${DATABASE}" fi fi @@ -151,7 +138,7 @@ fi # Build DB specific import command if [[ ${POSTGRES_DB} != '' ]]; then - CMD="$CMD | psql postgresql://$USER@$HOST:$PORT/$DATABASE" + CMD="$CMD | psql --quiet --echo-errors postgresql://$USER@$HOST:$PORT/$DATABASE" else CMD="$CMD | mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS} $DATABASE" fi