test_synapse_port_db.sh 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #!/usr/bin/env bash
  2. #
  3. # Test script for 'synapse_port_db'.
  4. # - configures synapse and a postgres server.
  5. # - runs the port script on a prepopulated test sqlite db. Checks that the
  6. # return code is zero.
  7. # - reruns the port script on the same sqlite db, targetting the same postgres db.
  8. # Checks that the return code is zero.
  9. # - runs the port script against a new sqlite db. Checks the return code is zero.
  10. #
  11. # Expects Synapse to have been already installed with `poetry install --extras postgres`.
  12. # Expects `poetry` to be available on the `PATH`.
  13. set -xe -o pipefail
  14. cd "$(dirname "$0")/../.."
  15. echo "--- Generate the signing key"
  16. poetry run synapse_homeserver --generate-keys -c .ci/sqlite-config.yaml
  17. echo "--- Prepare test database"
  18. # Make sure the SQLite3 database is using the latest schema and has no pending background updates.
  19. poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
  20. # Create the PostgreSQL database.
  21. psql -c "CREATE DATABASE synapse"
  22. echo "+++ Run synapse_port_db against test database"
  23. # TODO: this invocation of synapse_port_db (and others below) used to be prepended with `coverage run`,
  24. # but coverage seems unable to find the entrypoints installed by `pip install -e .`.
  25. poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
  26. # We should be able to run twice against the same database.
  27. echo "+++ Run synapse_port_db a second time"
  28. poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
  29. #####
  30. # Now do the same again, on an empty database.
  31. echo "--- Prepare empty SQLite database"
  32. # we do this by deleting the sqlite db, and then doing the same again.
  33. rm .ci/test_db.db
  34. poetry run update_synapse_database --database-config .ci/sqlite-config.yaml --run-background-updates
  35. # re-create the PostgreSQL database.
  36. psql \
  37. -c "DROP DATABASE synapse" \
  38. -c "CREATE DATABASE synapse"
  39. echo "+++ Run synapse_port_db against empty database"
  40. poetry run synapse_port_db --sqlite-database .ci/test_db.db --postgres-config .ci/postgres-config.yaml
  41. echo "--- Create a brand new postgres database from schema"
  42. cp .ci/postgres-config.yaml .ci/postgres-config-unported.yaml
  43. sed -i -e 's/database: synapse/database: synapse_unported/' .ci/postgres-config-unported.yaml
  44. psql -c "CREATE DATABASE synapse_unported"
  45. poetry run update_synapse_database --database-config .ci/postgres-config-unported.yaml --run-background-updates
  46. echo "+++ Comparing ported schema with unported schema"
  47. # Ignore the tables that portdb creates. (Should it tidy them up when the porting is completed?)
  48. psql synapse -c "DROP TABLE port_from_sqlite3;"
  49. pg_dump --format=plain --schema-only --no-tablespaces --no-acl --no-owner synapse_unported > unported.sql
  50. pg_dump --format=plain --schema-only --no-tablespaces --no-acl --no-owner synapse > ported.sql
  51. # By default, `diff` returns zero if there are no changes and nonzero otherwise
  52. diff -u unported.sql ported.sql | tee schema_diff