Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit b60d47a

Browse files
author
David Robertson
authored
Updates to the schema dump script (#13770)
1 parent 540afb0 commit b60d47a

File tree

3 files changed

+21
-32
lines changed

3 files changed

+21
-32
lines changed

changelog.d/13770.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Update the script which makes full schema dumps.

scripts-dev/make_full_schema.sh

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
export PGHOST="localhost"
1010
POSTGRES_DB_NAME="synapse_full_schema.$$"
1111

12-
SQLITE_FULL_SCHEMA_OUTPUT_FILE="full.sql.sqlite"
13-
POSTGRES_FULL_SCHEMA_OUTPUT_FILE="full.sql.postgres"
12+
SQLITE_SCHEMA_FILE="schema.sql.sqlite"
13+
SQLITE_ROWS_FILE="rows.sql.sqlite"
14+
POSTGRES_SCHEMA_FILE="full.sql.postgres"
15+
POSTGRES_ROWS_FILE="rows.sql.postgres"
1416

1517
REQUIRED_DEPS=("matrix-synapse" "psycopg2")
1618

@@ -22,7 +24,7 @@ usage() {
2224
echo " Username to connect to local postgres instance. The password will be requested"
2325
echo " during script execution."
2426
echo "-c"
25-
echo " CI mode. Enables coverage tracking and prints every command that the script runs."
27+
echo " CI mode. Prints every command that the script runs."
2628
echo "-o <path>"
2729
echo " Directory to output full schema files to."
2830
echo "-h"
@@ -37,11 +39,6 @@ while getopts "p:co:h" opt; do
3739
c)
3840
# Print all commands that are being executed
3941
set -x
40-
41-
# Modify required dependencies for coverage
42-
REQUIRED_DEPS+=("coverage" "coverage-enable-subprocess")
43-
44-
COVERAGE=1
4542
;;
4643
o)
4744
command -v realpath > /dev/null || (echo "The -o flag requires the 'realpath' binary to be installed" && exit 1)
@@ -102,6 +99,7 @@ SQLITE_DB=$TMPDIR/homeserver.db
10299
POSTGRES_CONFIG=$TMPDIR/postgres.conf
103100

104101
# Ensure these files are delete on script exit
102+
# TODO: the trap should also drop the temp postgres DB
105103
trap 'rm -rf $TMPDIR' EXIT
106104

107105
cat > "$SQLITE_CONFIG" <<EOF
@@ -147,48 +145,34 @@ python -m synapse.app.homeserver --generate-keys -c "$SQLITE_CONFIG"
147145

148146
# Make sure the SQLite3 database is using the latest schema and has no pending background update.
149147
echo "Running db background jobs..."
150-
synapse/_scripts/update_synapse_database.py --database-config --run-background-updates "$SQLITE_CONFIG"
148+
synapse/_scripts/update_synapse_database.py --database-config "$SQLITE_CONFIG" --run-background-updates
151149

152150
# Create the PostgreSQL database.
153151
echo "Creating postgres database..."
154152
createdb --lc-collate=C --lc-ctype=C --template=template0 "$POSTGRES_DB_NAME"
155153

156-
echo "Copying data from SQLite3 to Postgres with synapse_port_db..."
157-
if [ -z "$COVERAGE" ]; then
158-
# No coverage needed
159-
synapse/_scripts/synapse_port_db.py --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
160-
else
161-
# Coverage desired
162-
coverage run synapse/_scripts/synapse_port_db.py --sqlite-database "$SQLITE_DB" --postgres-config "$POSTGRES_CONFIG"
163-
fi
154+
echo "Running db background jobs..."
155+
synapse/_scripts/update_synapse_database.py --database-config "$POSTGRES_CONFIG" --run-background-updates
156+
164157

165158
# Delete schema_version, applied_schema_deltas and applied_module_schemas tables
166159
# Also delete any shadow tables from fts4
167-
# This needs to be done after synapse_port_db is run
168160
echo "Dropping unwanted db tables..."
169161
SQL="
170162
DROP TABLE schema_version;
171163
DROP TABLE applied_schema_deltas;
172164
DROP TABLE applied_module_schemas;
173-
DROP TABLE event_search_content;
174-
DROP TABLE event_search_segments;
175-
DROP TABLE event_search_segdir;
176-
DROP TABLE event_search_docsize;
177-
DROP TABLE event_search_stat;
178-
DROP TABLE user_directory_search_content;
179-
DROP TABLE user_directory_search_segments;
180-
DROP TABLE user_directory_search_segdir;
181-
DROP TABLE user_directory_search_docsize;
182-
DROP TABLE user_directory_search_stat;
183165
"
184166
sqlite3 "$SQLITE_DB" <<< "$SQL"
185167
psql "$POSTGRES_DB_NAME" -w <<< "$SQL"
186168

187-
echo "Dumping SQLite3 schema to '$OUTPUT_DIR/$SQLITE_FULL_SCHEMA_OUTPUT_FILE'..."
188-
sqlite3 "$SQLITE_DB" ".dump" > "$OUTPUT_DIR/$SQLITE_FULL_SCHEMA_OUTPUT_FILE"
169+
echo "Dumping SQLite3 schema to '$OUTPUT_DIR/$SQLITE_SCHEMA_FILE' and '$OUTPUT_DIR/$SQLITE_ROWS_FILE'..."
170+
sqlite3 "$SQLITE_DB" ".schema --indent" > "$OUTPUT_DIR/$SQLITE_SCHEMA_FILE"
171+
sqlite3 "$SQLITE_DB" ".dump --data-only --nosys" > "$OUTPUT_DIR/$SQLITE_ROWS_FILE"
189172

190-
echo "Dumping Postgres schema to '$OUTPUT_DIR/$POSTGRES_FULL_SCHEMA_OUTPUT_FILE'..."
191-
pg_dump --format=plain --no-tablespaces --no-acl --no-owner $POSTGRES_DB_NAME | sed -e '/^--/d' -e 's/public\.//g' -e '/^SET /d' -e '/^SELECT /d' > "$OUTPUT_DIR/$POSTGRES_FULL_SCHEMA_OUTPUT_FILE"
173+
echo "Dumping Postgres schema to '$OUTPUT_DIR/$POSTGRES_SCHEMA_FILE' and '$OUTPUT_DIR/$POSTGRES_ROWS_FILE'..."
174+
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"
175+
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"
192176

193177
echo "Cleaning up temporary Postgres database..."
194178
dropdb $POSTGRES_DB_NAME

synapse/storage/schema/state/delta/30/state_stream.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
* (event, state) pair, we can use that stream_ordering to identify when
2727
* the new state was assigned for the event.
2828
*/
29+
30+
/* NB: This table belongs to the `main` logical database; it should not be present
31+
* in `state`.
32+
*/
2933
CREATE TABLE IF NOT EXISTS ex_outlier_stream(
3034
event_stream_ordering BIGINT PRIMARY KEY NOT NULL,
3135
event_id TEXT NOT NULL,

0 commit comments

Comments
 (0)