2021-03-25 10:53:54 -06:00
|
|
|
#!/usr/bin/env bash
|
2019-10-28 11:45:32 -06:00
|
|
|
#
|
2021-05-10 06:02:55 -06:00
|
|
|
# Test script for 'synapse_port_db'.
|
2022-04-08 05:00:05 -06:00
|
|
|
# - configures synapse and a postgres server.
|
2022-09-16 09:25:54 -06:00
|
|
|
# - runs the port script on a prepopulated test sqlite db. Checks that the
|
|
|
|
# return code is zero.
|
|
|
|
# - reruns the port script on the same sqlite db, targetting the same postgres db.
|
|
|
|
# Checks that the return code is zero.
|
|
|
|
# - runs the port script against a new sqlite db. Checks the return code is zero.
|
2022-04-08 05:00:05 -06:00
|
|
|
#
|
|
|
|
# Expects Synapse to have been already installed with `poetry install --extras postgres`.
|
|
|
|
# Expects `poetry` to be available on the `PATH`.
|
2019-10-28 11:45:32 -06:00
|
|
|
|
2022-09-16 09:25:54 -06:00
|
|
|
set -xe -o pipefail
|
2021-10-22 16:07:23 -06:00
|
|
|
cd "$(dirname "$0")/../.."
|
2019-10-28 11:45:32 -06:00
|
|
|
|
2019-10-29 09:39:44 -06:00
|
|
|
echo "--- Generate the signing key"
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run synapse_homeserver --generate-keys -c .ci/sqlite-config.yaml
|
2019-10-29 09:39:44 -06:00
|
|
|
|
2021-05-10 06:02:55 -06:00
|
|
|
echo "--- Prepare test database"
|
2022-09-16 09:25:54 -06:00
|
|
|
# Make sure the SQLite3 database is using the latest schema and has no pending background updates.
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
|
2019-10-28 11:45:32 -06:00
|
|
|
|
|
|
|
# Create the PostgreSQL database.
|
2022-09-16 09:25:54 -06:00
|
|
|
psql -c "CREATE DATABASE synapse"
|
2021-05-10 06:02:55 -06:00
|
|
|
|
|
|
|
echo "+++ Run synapse_port_db against test database"
|
2022-03-02 06:00:16 -07:00
|
|
|
# TODO: this invocation of synapse_port_db (and others below) used to be prepended with `coverage run`,
|
|
|
|
# but coverage seems unable to find the entrypoints installed by `pip install -e .`.
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2021-05-10 06:02:55 -06:00
|
|
|
|
2021-05-24 07:03:00 -06:00
|
|
|
# We should be able to run twice against the same database.
|
|
|
|
echo "+++ Run synapse_port_db a second time"
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2021-05-24 07:03:00 -06:00
|
|
|
|
2021-05-10 06:02:55 -06:00
|
|
|
#####
|
|
|
|
|
|
|
|
# Now do the same again, on an empty database.
|
|
|
|
|
|
|
|
echo "--- Prepare empty SQLite database"
|
|
|
|
|
|
|
|
# we do this by deleting the sqlite db, and then doing the same again.
|
2021-08-11 13:19:56 -06:00
|
|
|
rm .ci/test_db.db
|
2021-05-10 06:02:55 -06:00
|
|
|
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
|
2019-10-29 09:39:44 -06:00
|
|
|
|
2021-05-10 06:02:55 -06:00
|
|
|
# re-create the PostgreSQL database.
|
2022-09-16 09:25:54 -06:00
|
|
|
psql \
|
|
|
|
-c "DROP DATABASE synapse" \
|
|
|
|
-c "CREATE DATABASE synapse"
|
2019-10-28 11:45:32 -06:00
|
|
|
|
2021-05-10 06:02:55 -06:00
|
|
|
echo "+++ Run synapse_port_db against empty database"
|
2022-04-08 05:00:05 -06:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2022-09-16 09:25:54 -06:00
|
|
|
|
|
|
|
echo "--- Create a brand new postgres database from schema"
|
|
|
|
cp .ci/postgres-config.yaml .ci/postgres-config-unported.yaml
|
|
|
|
sed -i -e 's/database: synapse/database: synapse_unported/' .ci/postgres-config-unported.yaml
|
|
|
|
psql -c "CREATE DATABASE synapse_unported"
|
|
|
|
poetry run update_synapse_database --database-config .ci/postgres-config-unported.yaml --run-background-updates
|
|
|
|
|
|
|
|
echo "+++ Comparing ported schema with unported schema"
|
|
|
|
# Ignore the tables that portdb creates. (Should it tidy them up when the porting is completed?)
|
|
|
|
psql synapse -c "DROP TABLE port_from_sqlite3;"
|
|
|
|
pg_dump --format=plain --schema-only --no-tablespaces --no-acl --no-owner synapse_unported > unported.sql
|
|
|
|
pg_dump --format=plain --schema-only --no-tablespaces --no-acl --no-owner synapse > ported.sql
|
|
|
|
# By default, `diff` returns zero if there are no changes and nonzero otherwise
|
|
|
|
diff -u unported.sql ported.sql | tee schema_diff
|