Skip to content

Commit 7c7600e

Browse files
authored
adding test_db_sql (#3405)
* Update CHANGELOG.md * clean plugins * patch 54.sql * fix Internal Qiita jobs * sample_template.generate_files * rm extra filepath * qiita.filepath_filepath_id_seq * basedir_len * basedir_len * 2 * basedir_len * 3 * get_db_files_base_dir * adding test_db_sql * missing ` * echo $QIITA_CONFIG_FP * -p 32768 * -l localhost * pgport * testing 1 * testing 2 * localhost * testing 3 * -h localhost * -W * PGPASSWORD * doc * flake8 * mods after chatting with @charles-cowart * exit 1 * "$row_counts" * # * `echo $row_counts` * full tests
1 parent 7b9cb33 commit 7c7600e

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

.github/workflows/qiita-ci.yml

+28-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,34 @@ jobs:
201201
QIITA_PID=`cat /tmp/supervisord.pid`
202202
kill $QIITA_PID
203203
sleep 10
204-
if [[ "$COVER_PACKAGE" != *"qiita_db"* ]]; then test_data_studies/commands.sh; all-qiita-cron-job; fi
204+
# due to qiita_db tests being more complex and taking longer than
205+
# the other tests we will only add some extra tests to the run that is
206+
# not testing qiita_db
207+
if [[ "$COVER_PACKAGE" != *"qiita_db"* ]]; then
208+
# 1. testing that we can add some "dummy" studies to the db via
209+
# CLI
210+
test_data_studies/commands.sh;
211+
# 2. making sure that all qiita cron jobs complete as expected
212+
all-qiita-cron-job;
213+
# 3. making sure than a production system has the expected rows
214+
# in all our tables; steps: a. drop test db, b. change $QIITA_CONFIG_FP
215+
# c. create new production system, c. count rows in the db.
216+
qiita-env drop;
217+
cp $QIITA_CONFIG_FP ${QIITA_CONFIG_FP}.bk
218+
sed 's/TEST_ENVIRONMENT = TRUE/TEST_ENVIRONMENT = FALSE/g' ${QIITA_CONFIG_FP}.bk > $QIITA_CONFIG_FP;
219+
qiita-env make --no-load-ontologies;
220+
221+
export PGPASSWORD=postgres
222+
pgport=${{ job.services.postgres.ports[5432] }}
223+
row_counts=`psql -h localhost -U postgres -d qiita_test -p $pgport -c "SELECT SUM(c.reltuples) FROM pg_class c JOIN pg_namespace n on n.oid = c.relnamespace WHERE n.nspname = 'qiita' AND c.relkind = 'r' AND n.nspname NOT IN ('information_schema', 'pg_catalog');"`
224+
if [[ `echo $row_counts` != *" 0 "* ]]; then
225+
echo "***********";
226+
echo "The number of rows in a production system is not what's expected:";
227+
echo $row_counts;
228+
echo "***********";
229+
exit 1
230+
fi
231+
fi
205232
206233
- name: Submit coveralls
207234
uses: AndreMiras/coveralls-python-action@develop

CONTRIBUTING.md

+11
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ After the initial production release of Qiita, changes to the database schema wi
9494
2. We keep fully patched versions of the DBS and HTML files in the repository
9595
3. We keep a patch file for each patch as required in the `qiita_db/support_files/patches` directory. Note that **the patches will be applied in order based on the natural sort order of their filename** (e.g., `2.sql` will be applied before `10.sql`, and `10.sql` will be applied before `a.sql`)
9696

97+
### Patch 91.sql
98+
99+
In May 2024 we decided to:
100+
* Merge all patches into the main database schema, this means that there are no patches younger than 92.sql.
101+
* Added a new folder `patches/test_db_sql/` where we can store sql files that will only be applied for the test environment.
102+
* Added a test to the GitHub actions to test that the production database has an expected number of rows.
103+
104+
Note that these changes mean:
105+
1. 92.sql is the current first sql file to patch the database.
106+
2. If you need to make changes (like INSERTS) _only_ to the tests database you need to add a patch to `patches/test_db_sql/`.
107+
97108
### Developer Workflow
98109

99110
1. Load the fully patched DBS file (e.g., `qiita-db.dbs`) in [DBSchema](http://www.dbschema.com/)

qiita_db/environment_manager.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
381381
current_patch = qdb.sql_connection.TRN.execute_fetchlast()
382382
current_sql_patch_fp = join(patches_dir, current_patch)
383383
corresponding_py_patch = partial(join, patches_dir, 'python_patches')
384+
corresponding_test_sql = partial(join, patches_dir, 'test_db_sql')
384385

385386
sql_glob = join(patches_dir, '*.sql')
386387
sql_patch_files = natsorted(glob(sql_glob))
@@ -402,15 +403,23 @@ def patch(patches_dir=PATCHES_DIR, verbose=False, test=False):
402403

403404
patch_prefix = splitext(basename(sql_patch_fp))[0]
404405
py_patch_fp = corresponding_py_patch(f'{patch_prefix}.py')
406+
test_sql_fp = corresponding_test_sql(f'{patch_prefix}.sql')
405407

406408
with qdb.sql_connection.TRN:
407409
with open(sql_patch_fp, newline=None) as patch_file:
408410
if verbose:
409411
print('\tApplying patch %s...' % sql_patch_filename)
410-
411412
qdb.sql_connection.TRN.add(patch_file.read())
412413
qdb.sql_connection.TRN.add(
413414
patch_update_sql, [sql_patch_filename])
415+
416+
if test and exists(test_sql_fp):
417+
if verbose:
418+
print('\t\tApplying test SQL %s...'
419+
% basename(test_sql_fp))
420+
with open(test_sql_fp) as test_sql:
421+
qdb.sql_connection.TRN.add(test_sql.read())
422+
414423
qdb.sql_connection.TRN.execute()
415424

416425
if exists(py_patch_fp):
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- .. date ..
2+
-- tmp file for @Gossty
3+
4+
SELECT 1;

0 commit comments

Comments
 (0)