Skip to content

Commit dbfb3e1

Browse files
Improve DB Import/Export Scripts and Add Tests (#166)
* Add test cases for sql import/export scripts * healthchecks because scripts are faster than me * Use DROP/CREATE strategy for MySQL DB reset * Should work whether or not the database exists * Turn down that racket! * loggin --------- Co-authored-by: Chris Burgess <[email protected]>
1 parent 4b72dad commit dbfb3e1

File tree

8 files changed

+186
-23
lines changed

8 files changed

+186
-23
lines changed

.github/workflows/pr-core-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ jobs:
3131
- examples/orchestrator
3232
- examples/plugin-commands
3333
- examples/services
34+
- examples/sql-helpers
3435
- examples/tooling
3536
node-version:
3637
- "18"

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## {{ UNRELEASED_VERSION }} - [{{ UNRELEASED_DATE }}]({{ UNRELEASED_LINK }})
22

3+
* Added `leia` tests for `sql-import.sh` and `sql-export.sh`
4+
* Improved `sql-import.sh` to drop and recreate `mysql` and `mariadb` tables before importing
5+
36
## v3.21.0-beta.19 - [May 9, 2024](https://github.com/lando/core/releases/tag/v3.21.0-beta.19)
47

58
### New Features

examples/sql-helpers/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sqlhelpers
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: lando-sqlhelpers
2+
services:
3+
mariadb:
4+
api: 3
5+
type: lando
6+
healthcheck: mysqladmin ping -h mariadb -u test -ptest
7+
services:
8+
image: bitnami/mariadb:10.4
9+
command: /opt/bitnami/scripts/mariadb/entrypoint.sh /opt/bitnami/scripts/mariadb/run.sh
10+
environment:
11+
ALLOW_EMPTY_PASSWORD: yes
12+
MARIADB_DATABASE: lando_test
13+
MYSQL_DATABASE: lando_test
14+
MARIADB_USER: test
15+
MARIADB_PASSWORD: test
16+
17+
mysql57:
18+
api: 3
19+
type: lando
20+
healthcheck: mysqladmin ping -h mysql57 -u test -ptest
21+
services:
22+
image: bitnami/mysql:5.7
23+
command: /opt/bitnami/scripts/mysql/entrypoint.sh /opt/bitnami/scripts/mysql/run.sh
24+
environment:
25+
ALLOW_EMPTY_PASSWORD: yes
26+
MYSQL_AUTHENTICATION_PLUGIN: mysql_native_password
27+
MYSQL_DATABASE: lando_test
28+
MYSQL_PASSWORD: test
29+
MYSQL_USER: test
30+
31+
mysql80:
32+
api: 3
33+
type: lando
34+
healthcheck: mysqladmin ping -h mysql80 -u test -ptest
35+
services:
36+
image: bitnami/mysql:8.0
37+
command: /opt/bitnami/scripts/mysql/entrypoint.sh /opt/bitnami/scripts/mysql/run.sh
38+
environment:
39+
ALLOW_EMPTY_PASSWORD: yes
40+
MYSQL_AUTHENTICATION_PLUGIN: caching_sha2_password
41+
MYSQL_DATABASE: lando_test
42+
MYSQL_PASSWORD: test
43+
MYSQL_USER: test
44+
45+
postgres16:
46+
api: 3
47+
type: lando
48+
healthcheck: pg_isready -h postgres16 -U postgres
49+
services:
50+
image: bitnami/postgresql:16
51+
command: /opt/bitnami/scripts/postgresql/entrypoint.sh /opt/bitnami/scripts/postgresql/run.sh
52+
environment:
53+
ALLOW_EMPTY_PASSWORD: yes
54+
POSTGRESQL_DATABASE: lando_test
55+
POSTGRES_DB: lando_test
56+
57+
plugins:
58+
"@lando/core": "../../.."

examples/sql-helpers/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
SQL Import/Export Example
2+
============
3+
4+
This example exists primarily to test the `sql-import.sh` and `sql-export.sh` helper scripts.
5+
6+
Start up tests
7+
--------------
8+
9+
```bash
10+
# Should init and start a lando app
11+
rm -rf sqlhelpers && mkdir -p sqlhelpers
12+
cp -rf .lando.sqlhelpers.yml sqlhelpers/.lando.yml
13+
cp -rf testdata1.sql sqlhelpers/testdata1.sql
14+
cp -rf testdata2.sql sqlhelpers/testdata2.sql
15+
cd sqlhelpers && lando start
16+
```
17+
18+
Verification commands
19+
---------------------
20+
21+
Run the following commands to verify things work as expected
22+
23+
```bash
24+
# Should import test data into mariadb
25+
cd sqlhelpers
26+
lando ssh -s mariadb -c "/helpers/sql-import.sh testdata1.sql"
27+
lando ssh -s mariadb -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original"
28+
29+
# Should import test data into mysql57
30+
cd sqlhelpers
31+
lando ssh -s mysql57 -c "/helpers/sql-import.sh testdata1.sql"
32+
lando ssh -s mysql57 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original"
33+
34+
# Should import test data into mysql80
35+
cd sqlhelpers
36+
lando ssh -s mysql80 -c "/helpers/sql-import.sh testdata1.sql"
37+
lando ssh -s mysql80 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep "lando_original"
38+
39+
# Should import test data into postgres16
40+
cd sqlhelpers
41+
lando ssh -s postgres16 -c "/helpers/sql-import.sh testdata1.sql"
42+
lando ssh -s postgres16 -c "psql -U postgres -d lando_test -c 'select * from lando_test'" | grep "lando_original"
43+
44+
# Should export gzipped files from mariadb
45+
cd sqlhelpers
46+
lando ssh -s mariadb -c "/helpers/sql-export.sh mariadb_dump.sql" -u root
47+
gzip -d mariadb_dump.sql.gz
48+
49+
# Should export gzipped files from mysql57
50+
cd sqlhelpers
51+
lando ssh -s mysql57 -c "/helpers/sql-export.sh mysql57_dump.sql" -u root
52+
gzip -d mysql57_dump.sql.gz
53+
54+
# Should export gzipped files from mysql80
55+
cd sqlhelpers
56+
lando ssh -s mysql80 -c "/helpers/sql-export.sh mysql80_dump.sql" -u root
57+
gzip -d mysql80_dump.sql.gz
58+
59+
# Should export gzipped files from postgres16
60+
cd sqlhelpers
61+
lando ssh -s postgres16 -c "/helpers/sql-export.sh postgres16_dump.sql" -u root
62+
gzip -d postgres16_dump.sql.gz
63+
64+
# Should have the correct data in all exported files
65+
cd sqlhelpers
66+
grep "lando_original" mariadb_dump.sql
67+
grep "lando_original" mysql57_dump.sql
68+
grep "lando_original" mysql80_dump.sql
69+
grep "lando_original" postgres16_dump.sql
70+
71+
# Should be able to replace data with testdata2 in mariadb
72+
cd sqlhelpers
73+
lando ssh -s mariadb -c "/helpers/sql-import.sh testdata2.sql"
74+
lando ssh -s mariadb -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated"
75+
76+
# Should be able to replace data with testdata2 in mysql57
77+
cd sqlhelpers
78+
lando ssh -s mysql57 -c "/helpers/sql-import.sh testdata2.sql"
79+
lando ssh -s mysql57 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated"
80+
81+
# Should be able to replace data with testdata2 in mysql80
82+
cd sqlhelpers
83+
lando ssh -s mysql80 -c "/helpers/sql-import.sh testdata2.sql"
84+
lando ssh -s mysql80 -c "mysql -utest -ptest lando_test -e 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated"
85+
86+
# Should be able to replace data with testdata2 in postgres16
87+
cd sqlhelpers
88+
lando ssh -s postgres16 -c "/helpers/sql-import.sh testdata2.sql"
89+
lando ssh -s postgres16 -c "psql -U postgres -d lando_test -c 'select * from lando_test'" | grep -v "lando_original" | grep "lando_updated"
90+
```
91+
92+
Destroy tests
93+
-------------
94+
95+
```bash
96+
# Should destroy sqlhelpers successfully
97+
cd sqlhelpers && lando destroy -y
98+
99+
# Should poweroff
100+
lando poweroff
101+
```

examples/sql-helpers/testdata1.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE lando_test (
2+
id SERIAL PRIMARY KEY,
3+
column1 VARCHAR(255) DEFAULT NULL,
4+
column2 VARCHAR(255) DEFAULT NULL
5+
);
6+
INSERT INTO lando_test VALUES (1, 'lando_original', 'lando_original');

examples/sql-helpers/testdata2.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
CREATE TABLE lando_test (
2+
id SERIAL PRIMARY KEY,
3+
column1 VARCHAR(255) DEFAULT NULL,
4+
column2 VARCHAR(255) DEFAULT NULL
5+
);
6+
INSERT INTO lando_test VALUES (1, 'lando_updated', 'lando_updated');

scripts/sql-import.sh

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,21 @@ echo "Preparing to import $FILE into database '$DATABASE' on service '$SERVICE'
9595

9696
# Wipe the database if set
9797
if [ "$WIPE" == "true" ]; then
98-
echo ""
99-
echo "Emptying $DATABASE... "
98+
lando_pink "\nEmptying $DATABASE... "
10099
lando_yellow "NOTE: See the --no-wipe flag to avoid this step!"
101100

102101
# DO db specific wiping
103102
if [[ ${POSTGRES_DB} != '' ]]; then
104103
# Drop and recreate database
105-
lando_yellow "\t\tDropping database ...\n\n"
106-
psql postgresql://$USER@$HOST:$PORT/postgres -c "drop database $DATABASE"
107-
108-
lando_green "\t\tCreating database ...\n\n"
109-
psql postgresql://$USER@$HOST:$PORT/postgres -c "create database $DATABASE"
104+
psql postgresql://$USER@$HOST:$PORT/postgres -c "DROP DATABASE IF EXISTS $DATABASE" --quiet --echo-errors
105+
psql postgresql://$USER@$HOST:$PORT/postgres -c "CREATE DATABASE $DATABASE" --quiet --echo-errors
110106
else
111-
# Build the SQL prefix
112-
SQLSTART="mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS} $DATABASE"
113-
114-
# Gather and destroy tables
115-
TABLES=$($SQLSTART -e 'SHOW TABLES' | awk '{ print $1}' | grep -v '^Tables' || true)
116-
117-
# PURGE IT ALL! Drop views and tables as needed
118-
for t in $TABLES; do
119-
echo "Dropping $t from $DATABASE database..."
120-
$SQLSTART <<-EOF
121-
SET FOREIGN_KEY_CHECKS=0;
122-
DROP VIEW IF EXISTS \`$t\`;
123-
DROP TABLE IF EXISTS \`$t\`;
124-
EOF
125-
done
107+
# Connection string
108+
SQLSTART="mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS}"
109+
110+
# Drop and recreate database
111+
$SQLSTART -e "DROP DATABASE IF EXISTS ${DATABASE}"
112+
$SQLSTART -e "CREATE DATABASE ${DATABASE}"
126113
fi
127114
fi
128115

@@ -151,7 +138,7 @@ fi
151138

152139
# Build DB specific import command
153140
if [[ ${POSTGRES_DB} != '' ]]; then
154-
CMD="$CMD | psql postgresql://$USER@$HOST:$PORT/$DATABASE"
141+
CMD="$CMD | psql --quiet --echo-errors postgresql://$USER@$HOST:$PORT/$DATABASE"
155142
else
156143
CMD="$CMD | mysql -h $HOST -P $PORT -u $USER ${LANDO_EXTRA_DB_IMPORT_ARGS} $DATABASE"
157144
fi

0 commit comments

Comments
 (0)