Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an --initial-sql flag to InitDb.pl; allow configuring the REPLICATION_TYPE for create_test_db.sh #3197

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

mwiencek
Copy link
Member

@mwiencek mwiencek commented Mar 6, 2024

This is based on top of #3194

Problem

This resolves two problems:

  • ./admin/InitDb.pl currently always runs admin/sql/InsertDefaultRows.sql on the database, and this cannot be configured (e.g., to specify a different initialization script, or use none at all).
  • ./script/create_test_db.sh currently cannot be used to initialize a mirror test database (i.e., one without foreign keys or triggers).

I'd like to fix these issues in order to improve the test suites that make use of replication packets (and thus require separate master/mirror test databases).

Solution

With this PR, you can set the REPLICATION_TYPE environment variable before running ./script/create_test_db.sh:

REPLICATION_TYPE=1 ./script/create_test_db.sh # master
REPLICATION_TYPE=2 ./script/create_test_db.sh # mirror
REPLICATION_TYPE=3 ./script/create_test_db.sh # standalone

See associated commits for details.

Testing

Just manual invocations of the create_test_db.sh and InitDb.pl commands, plus automated tests.

@mwiencek mwiencek force-pushed the initdb-initial-sql branch 19 times, most recently from 37842e3 to 54b817a Compare March 8, 2024 03:58
is($result, $f1_path, 'scalar file is found by direct path');

$result = find_mbdump_file('artist', $dir1);
is($result, $f1_path, 'scalar file is found in directory');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason you prefer lowercase for these labels? I generally start them with uppercase because they look like full sentences to me the way we print them, but obviously this is mega minor anyway :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess because they read more like fragments than full sentences to me. :) And some of them start with undef or variable names which would be weird to capitalize...

I don't have a particular preference though.

Copy link
Member

@reosarevok reosarevok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTMBDNT, I'd like @yvanzo to check it too. I had no idea script/setup_development_db.sh even existed!

@reosarevok reosarevok requested a review from yvanzo March 8, 2024 09:30
This merges the separate implementations of `find_file` contained in
admin/MBImport.pl and admin/replication/ImportReplicationChanges into
`Script::Utils`, and adds tests for said function.

The main difference between the two was that MBImport.pl's implementation would
return an array of matching files, rather than the first match. We now use
Perl's `wantarray` built-in to distinguish between the two cases.
In case the search paths overlap (e.g., we may find the file via a direct path,
and then again via a parent directory).
It works the same, except it doesn't check `mbdump/` subdirectories nor does it
support returning a scalar result. `find_mbdump_file` calls it internally.
Support specifying a path prefix in the first `$file` paremeter: e.g.,
`caa/CreateTables.sql` in the added test case includes a `caa/` path prefix,
but will now be found in the search path `/tmp/foo/sql/caa/CreateTables.sql`.
Allows specifying which SQL script to initialize a clean database with. Prior
to this commit, admin/sql/InsertDefaultRows.sql was always used (and this could
not be disabled!).

One current use case of this is that we can initialize test databases with
t/sql/initial.sql instead; and in that file, we no longer have to worry about
disabling triggers (because the `initial-sql` script is run before triggers are
added) or specifying `ON CONFLICT` handlers (which were to avoid conflicts with
existing data that was in admin/sql/InsertDefaultRows.sql).

The triggers issue is particularly relevant for mirror databases, where certain
triggers do not exist. We can now initialize mirror test databases with
t/sql/initial.sql, where previously it would error on the `DISABLE TRIGGER
a_ins_instrument` line. But removing that line means we *must* run this script
before creating triggers, hence the lines I've reordered in create_test_db.sh,
and the changes I've made to the Selenium test scripts.
With the `--initial-sql` flag to InitDb.pl, we only have to drop the existing
schemas if the database already exists (and otherwise create the pgtap
extension).
`ON_ERROR_STOP` must be set in order for psql to return a non-zero exit code on
statement failures.

This is not much of an issue now that `IF NOT EXISTS` was added, but I thought
I'd fix it anyway given that the intent of the code is to capture errors.
I've never once used this, and its purpose seems to have been overtaken by the
sample database dumps.
Copy link
Contributor

@yvanzo yvanzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About testing. What has been tested so far? Is there a setup that you would like us to test in particular?

Only minor comments otherwise.

Comment on lines +27 to +29
if [ -z "$SIR_DIR" ]; then
SIR_DIR="$MB_SERVER_ROOT"/../sir
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tip for setting a variable to a default value:

Suggested change
if [ -z "$SIR_DIR" ]; then
SIR_DIR="$MB_SERVER_ROOT"/../sir
fi
SIR_DIR="${SIR_DIR:=$MB_SERVER_ROOT/../sir}"

Comment on lines +37 to +39
if [ -z "$ARTWORK_INDEXER_DIR" ]; then
ARTWORK_INDEXER_DIR="$MB_SERVER_ROOT"/../artwork-indexer
fi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above:

Suggested change
if [ -z "$ARTWORK_INDEXER_DIR" ]; then
ARTWORK_INDEXER_DIR="$MB_SERVER_ROOT"/../artwork-indexer
fi
ARTWORK_INDEXER_DIR="${ARTWORK_INDEXER_DIR:=$MB_SERVER_ROOT/../artwork-indexer}"

@@ -0,0 +1,50 @@
#!/usr/bin/env bash

set -o errexit
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be safer to enable all the error detection options then:

  • nounset detects using unset variables (whether that is due to a coding mistake or a script misuse).
  • pipefail detects failures of piped commands (even though echo is unlikely to fail here).
Suggested change
set -o errexit
set -o errexit -o nounset -o pipefail

@@ -20,7 +20,7 @@ sudo -E -H -u musicbrainz cp docker/musicbrainz-tests/DBDefs.pm lib/

sv_start_if_down postgresql redis

sudo -E -H -u musicbrainz carton exec -- ./script/create_test_db.sh
REPLICATION_TYPE=1 sudo -E -H -u musicbrainz carton exec -- ./script/create_test_db.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for this change isn’t obvious from the message for commit 37a8cf3

@@ -117,6 +117,7 @@ test all => sub {
'-U', $system_db->username,
$test_db->database;

$ENV{REPLICATION_TYPE} = 1; # master
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants