2021-03-25 19:53:54 +03:00
|
|
|
#!/usr/bin/env bash
|
2019-10-28 20:45:32 +03:00
|
|
|
#
|
2021-05-10 15:02:55 +03:00
|
|
|
# Test script for 'synapse_port_db'.
|
2022-04-08 14:00:05 +03:00
|
|
|
# - configures synapse and a postgres server.
|
2022-09-16 18:25:54 +03: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 14:00:05 +03:00
|
|
|
#
|
|
|
|
# Expects Synapse to have been already installed with `poetry install --extras postgres`.
|
|
|
|
# Expects `poetry` to be available on the `PATH`.
|
2019-10-28 20:45:32 +03:00
|
|
|
|
2022-09-16 18:25:54 +03:00
|
|
|
set -xe -o pipefail
|
2021-10-23 01:07:23 +03:00
|
|
|
cd "$(dirname "$0")/../.."
|
2019-10-28 20:45:32 +03:00
|
|
|
|
2019-10-29 18:39:44 +03:00
|
|
|
echo "--- Generate the signing key"
|
2022-04-08 14:00:05 +03:00
|
|
|
poetry run synapse_homeserver --generate-keys -c .ci/sqlite-config.yaml
|
2019-10-29 18:39:44 +03:00
|
|
|
|
2021-05-10 15:02:55 +03:00
|
|
|
echo "--- Prepare test database"
|
2022-09-16 18:25:54 +03:00
|
|
|
# Make sure the SQLite3 database is using the latest schema and has no pending background updates.
|
2022-04-08 14:00:05 +03:00
|
|
|
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
|
2019-10-28 20:45:32 +03:00
|
|
|
|
|
|
|
# Create the PostgreSQL database.
|
2022-09-16 18:25:54 +03:00
|
|
|
psql -c "CREATE DATABASE synapse"
|
2021-05-10 15:02:55 +03:00
|
|
|
|
|
|
|
echo "+++ Run synapse_port_db against test database"
|
2022-03-02 16:00:16 +03: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 14:00:05 +03:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2021-05-10 15:02:55 +03:00
|
|
|
|
2021-05-24 16:03:00 +03:00
|
|
|
# We should be able to run twice against the same database.
|
|
|
|
echo "+++ Run synapse_port_db a second time"
|
2022-04-08 14:00:05 +03:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2021-05-24 16:03:00 +03:00
|
|
|
|
2021-05-10 15:02:55 +03: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 22:19:56 +03:00
|
|
|
rm .ci/test_db.db
|
2021-05-10 15:02:55 +03:00
|
|
|
|
2022-04-08 14:00:05 +03:00
|
|
|
poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
|
2019-10-29 18:39:44 +03:00
|
|
|
|
2021-05-10 15:02:55 +03:00
|
|
|
# re-create the PostgreSQL database.
|
2022-09-16 18:25:54 +03:00
|
|
|
psql \
|
|
|
|
-c "DROP DATABASE synapse" \
|
|
|
|
-c "CREATE DATABASE synapse"
|
2019-10-28 20:45:32 +03:00
|
|
|
|
2021-05-10 15:02:55 +03:00
|
|
|
echo "+++ Run synapse_port_db against empty database"
|
2022-04-08 14:00:05 +03:00
|
|
|
poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
|
2022-09-16 18:25:54 +03: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
|