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
@mwiencek mwiencek force-pushed the initdb-initial-sql branch from 54b817a to 0db1479 Compare March 11, 2024 15:30
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.

script/create_selenium_db.sh Outdated Show resolved Hide resolved
script/create_selenium_db.sh Outdated Show resolved Hide resolved
script/create_selenium_db.sh Outdated Show resolved Hide resolved
t/script/ExportAllTables.t Show resolved Hide resolved
mwiencek added 5 commits May 27, 2024 10:08
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`.
@mwiencek mwiencek force-pushed the initdb-initial-sql branch from 0db1479 to d7c4766 Compare May 27, 2024 19:30
mwiencek added 4 commits May 27, 2024 14:31
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).
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).

Thus, this commit delegates the majority of the work to InitDb.pl. One thing to
be aware of is that the scripts run by InitDb.pl depend on the configured
`REPLICATION_TYPE`. So if we want replication triggers, we have to set
`REPLICATION_TYPE=1`,
`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.
@mwiencek mwiencek force-pushed the initdb-initial-sql branch from d7c4766 to 1092925 Compare May 27, 2024 19:32
@mwiencek
Copy link
Member Author

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

Mainly the commands I listed in the PR description:

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

and making sure the created databases have the appropriate customizations (e.g., mirrors should be without foreign keys/triggers, masters should have replication triggers).

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

Successfully merging this pull request may close these issues.

3 participants