2021-03-25 10:53:54 -06:00
|
|
|
#!/usr/bin/env bash
|
2019-12-11 06:16:01 -07:00
|
|
|
#
|
|
|
|
# This script generates SQL files for creating a brand new Synapse DB with the latest
|
|
|
|
# schema, on both SQLite3 and Postgres.
|
|
|
|
#
|
|
|
|
# It does so by having Synapse generate an up-to-date SQLite DB, then running
|
|
|
|
# synapse_port_db to convert it to Postgres. It then dumps the contents of both.
|
|
|
|
|
2021-05-07 07:01:57 -06:00
|
|
|
export PGHOST="localhost"
|
2019-12-11 06:16:01 -07:00
|
|
|
POSTGRES_DB_NAME="synapse_full_schema.$$"
|
|
|
|
|
2022-09-13 03:53:11 -06:00
|
|
|
SQLITE_SCHEMA_FILE="schema.sql.sqlite"
|
|
|
|
SQLITE_ROWS_FILE="rows.sql.sqlite"
|
|
|
|
POSTGRES_SCHEMA_FILE="full.sql.postgres"
|
|
|
|
POSTGRES_ROWS_FILE="rows.sql.postgres"
|
2019-12-11 06:16:01 -07:00
|
|
|
|
|
|
|
REQUIRED_DEPS=("matrix-synapse" "psycopg2")
|
|
|
|
|
|
|
|
usage() {
|
|
|
|
echo
|
|
|
|
echo "Usage: $0 -p <postgres_username> -o <path> [-c] [-n] [-h]"
|
|
|
|
echo
|
|
|
|
echo "-p <postgres_username>"
|
|
|
|
echo " Username to connect to local postgres instance. The password will be requested"
|
|
|
|
echo " during script execution."
|
|
|
|
echo "-c"
|
2022-09-13 03:53:11 -06:00
|
|
|
echo " CI mode. Prints every command that the script runs."
|
2019-12-11 06:16:01 -07:00
|
|
|
echo "-o <path>"
|
|
|
|
echo " Directory to output full schema files to."
|
|
|
|
echo "-h"
|
|
|
|
echo " Display this help text."
|
|
|
|
}
|
|
|
|
|
|
|
|
while getopts "p:co:h" opt; do
|
|
|
|
case $opt in
|
|
|
|
p)
|
2021-05-07 07:01:57 -06:00
|
|
|
export PGUSER=$OPTARG
|
2019-12-11 06:16:01 -07:00
|
|
|
;;
|
|
|
|
c)
|
|
|
|
# Print all commands that are being executed
|
|
|
|
set -x
|
|
|
|
;;
|
|
|
|
o)
|
|
|
|
command -v realpath > /dev/null || (echo "The -o flag requires the 'realpath' binary to be installed" && exit 1)
|
|
|
|
OUTPUT_DIR="$(realpath "$OPTARG")"
|
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "ERROR: Invalid option: -$OPTARG" >&2
|
|
|
|
usage
|
|
|
|
exit
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
# Check that required dependencies are installed
|
|
|
|
unsatisfied_requirements=()
|
|
|
|
for dep in "${REQUIRED_DEPS[@]}"; do
|
|
|
|
pip show "$dep" --quiet || unsatisfied_requirements+=("$dep")
|
|
|
|
done
|
|
|
|
if [ ${#unsatisfied_requirements} -ne 0 ]; then
|
|
|
|
echo "Please install the following python packages: ${unsatisfied_requirements[*]}"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2021-05-07 07:01:57 -06:00
|
|
|
if [ -z "$PGUSER" ]; then
|
2019-12-11 06:16:01 -07:00
|
|
|
echo "No postgres username supplied"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z "$OUTPUT_DIR" ]; then
|
|
|
|
echo "No output directory supplied"
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create the output directory if it doesn't exist
|
|
|
|
mkdir -p "$OUTPUT_DIR"
|
|
|
|
|
2021-05-07 07:01:57 -06:00
|
|
|
read -rsp "Postgres password for '$PGUSER': " PGPASSWORD
|
2019-12-11 06:16:01 -07:00
|
|
|
echo ""
|
2021-05-07 07:01:57 -06:00
|
|
|
export PGPASSWORD
|
2019-12-11 06:16:01 -07:00
|
|
|
|
|
|
|
# Exit immediately if a command fails
|
|
|
|
set -e
|
|
|
|
|
|
|
|
# cd to root of the synapse directory
|
|
|
|
cd "$(dirname "$0")/.."
|
|
|
|
|
|
|
|
# Create temporary SQLite and Postgres homeserver db configs and key file
|
|
|
|
TMPDIR=$(mktemp -d)
|
|
|
|
KEY_FILE=$TMPDIR/test.signing.key # default Synapse signing key path
|
|
|
|
SQLITE_CONFIG=$TMPDIR/sqlite.conf
|
|
|
|
SQLITE_DB=$TMPDIR/homeserver.db
|
|
|
|
POSTGRES_CONFIG=$TMPDIR/postgres.conf
|
|
|
|
|
|
|
|
# Ensure these files are delete on script exit
|
2022-09-13 03:53:11 -06:00
|
|
|
# TODO: the trap should also drop the temp postgres DB
|
2019-12-11 06:16:01 -07:00
|
|
|
trap 'rm -rf $TMPDIR' EXIT
|
|
|
|
|
|
|
|
cat > "$SQLITE_CONFIG" <<EOF
|
|
|
|
server_name: "test"
|
|
|
|
|
|
|
|
signing_key_path: "$KEY_FILE"
|
|
|
|
macaroon_secret_key: "abcde"
|
|
|
|
|
|
|
|
report_stats: false
|
|
|
|
|
|
|
|
database:
|
|
|
|
name: "sqlite3"
|
|
|
|
args:
|
|
|
|
database: "$SQLITE_DB"
|
|
|
|
|
|
|
|
# Suppress the key server warning.
|
|
|
|
trusted_key_servers: []
|
|
|
|
EOF
|
|
|
|
|
|
|
|
cat > "$POSTGRES_CONFIG" <<EOF
|
|
|
|
server_name: "test"
|
|
|
|
|
|
|
|
signing_key_path: "$KEY_FILE"
|
|
|
|
macaroon_secret_key: "abcde"
|
|
|
|
|
|
|
|
report_stats: false
|
|
|
|
|
|
|
|
database:
|
|
|
|
name: "psycopg2"
|
|
|
|
args:
|
2021-05-07 07:01:57 -06:00
|
|
|
user: "$PGUSER"
|
|
|
|
host: "$PGHOST"
|
|
|
|
password: "$PGPASSWORD"
|
2019-12-11 06:16:01 -07:00
|
|
|
database: "$POSTGRES_DB_NAME"
|
|
|
|
|
|
|
|
# Suppress the key server warning.
|
|
|
|
trusted_key_servers: []
|
|
|
|
EOF
|
|
|
|
|
|
|
|
# Generate the server's signing key.
|
|
|
|
echo "Generating SQLite3 db schema..."
|
|
|
|
python -m synapse.app.homeserver --generate-keys -c "$SQLITE_CONFIG"
|
|
|
|
|
|
|
|
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
|
|
|
|
echo "Running db background jobs..."
|
2022-09-13 03:53:11 -06:00
|
|
|
synapse/_scripts/update_synapse_database.py --database-config "$SQLITE_CONFIG" --run-background-updates
|
2019-12-11 06:16:01 -07:00
|
|
|
|
|
|
|
# Create the PostgreSQL database.
|
|
|
|
echo "Creating postgres database..."
|
2021-05-07 07:01:57 -06:00
|
|
|
createdb --lc-collate=C --lc-ctype=C --template=template0 "$POSTGRES_DB_NAME"
|
2019-12-11 06:16:01 -07:00
|
|
|
|
2022-09-13 03:53:11 -06:00
|
|
|
echo "Running db background jobs..."
|
|
|
|
synapse/_scripts/update_synapse_database.py --database-config "$POSTGRES_CONFIG" --run-background-updates
|
|
|
|
|
2019-12-11 06:16:01 -07:00
|
|
|
|
|
|
|
# Delete schema_version, applied_schema_deltas and applied_module_schemas tables
|
2021-02-10 13:12:57 -07:00
|
|
|
# Also delete any shadow tables from fts4
|
2019-12-11 06:16:01 -07:00
|
|
|
echo "Dropping unwanted db tables..."
|
|
|
|
SQL="
|
|
|
|
DROP TABLE schema_version;
|
|
|
|
DROP TABLE applied_schema_deltas;
|
|
|
|
DROP TABLE applied_module_schemas;
|
|
|
|
"
|
|
|
|
sqlite3 "$SQLITE_DB" <<< "$SQL"
|
2021-05-07 07:01:57 -06:00
|
|
|
psql "$POSTGRES_DB_NAME" -w <<< "$SQL"
|
2019-12-11 06:16:01 -07:00
|
|
|
|
2022-09-13 03:53:11 -06:00
|
|
|
echo "Dumping SQLite3 schema to '$OUTPUT_DIR/$SQLITE_SCHEMA_FILE' and '$OUTPUT_DIR/$SQLITE_ROWS_FILE'..."
|
|
|
|
sqlite3 "$SQLITE_DB" ".schema --indent" > "$OUTPUT_DIR/$SQLITE_SCHEMA_FILE"
|
|
|
|
sqlite3 "$SQLITE_DB" ".dump --data-only --nosys" > "$OUTPUT_DIR/$SQLITE_ROWS_FILE"
|
2019-12-11 06:16:01 -07:00
|
|
|
|
2022-09-13 03:53:11 -06:00
|
|
|
echo "Dumping Postgres schema to '$OUTPUT_DIR/$POSTGRES_SCHEMA_FILE' and '$OUTPUT_DIR/$POSTGRES_ROWS_FILE'..."
|
|
|
|
pg_dump --format=plain --schema-only --no-tablespaces --no-acl --no-owner "$POSTGRES_DB_NAME" | sed -e '/^$/d' -e '/^--/d' -e 's/public\.//g' -e '/^SET /d' -e '/^SELECT /d' > "$OUTPUT_DIR/$POSTGRES_SCHEMA_FILE"
|
|
|
|
pg_dump --format=plain --data-only --inserts --no-tablespaces --no-acl --no-owner "$POSTGRES_DB_NAME" | sed -e '/^$/d' -e '/^--/d' -e 's/public\.//g' -e '/^SET /d' -e '/^SELECT /d' > "$OUTPUT_DIR/$POSTGRES_ROWS_FILE"
|
2019-12-11 06:16:01 -07:00
|
|
|
|
|
|
|
echo "Cleaning up temporary Postgres database..."
|
|
|
|
dropdb $POSTGRES_DB_NAME
|
|
|
|
|
|
|
|
echo "Done! Files dumped to: $OUTPUT_DIR"
|