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

Add dev script to generate full SQL schema files #6394

Merged
merged 13 commits into from
Dec 11, 2019

Conversation

anoadragon453
Copy link
Member

@anoadragon453 anoadragon453 commented Nov 20, 2019

Fixes #6476

So we ran into a bug that was a result of INSERT statements getting lost from converting SQL delta files to full schema. This was due to that process just copying over the schema of fully updated SQLite and Postgres tables, instead of the actual data.

Additionally, there wasn't any automated way (or much documentation) on how to generate full schemas, and I think if we had a canonical, automated way we might do it more often.

The script does this by generating a sqlite database with all background updates applied to it, then making use of the now-working synapse_port_db script to convert it to a postgres database. These two databases are then dumped to full.{sqlite,postgres}.sql files.

Also converted the full schema README from .txt to .md.

@anoadragon453 anoadragon453 requested a review from a team November 20, 2019 18:23
@anoadragon453 anoadragon453 self-assigned this Nov 20, 2019
@anoadragon453 anoadragon453 force-pushed the anoa/update_full_schema_instructions branch from f019aff to f6eff4b Compare November 20, 2019 18:27
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks generally good, though it's bash so I have Opinions.

scripts/synapse_port_db Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/generate_full_schema.sh Outdated Show resolved Hide resolved
rm "$KEY_FILE"
dropdb $POSTGRES_DB_NAME

# Remove last pesky instance of this table from the output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's going on here, ooi?

anoadragon453 and others added 4 commits December 2, 2019 17:38
richvdh
richvdh previously requested changes Dec 4, 2019
scripts-dev/make_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/make_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/make_full_schema.sh Outdated Show resolved Hide resolved
cat > "$SQLITE_CONFIG" <<EOF
server_name: "test"

signing_key_path: "$KEY_FILE"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what happens if you don't set this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks for test.signing.key in the current directory, not the temp dir.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be a bug. It's due to config_dir_path being "" here:

def read_config(self, config, config_dir_path, **kwargs):

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that doesn't sound right. Ok, let's not get distracted by going on a bughunt now.

scripts-dev/make_full_schema.sh Outdated Show resolved Hide resolved
scripts-dev/make_full_schema.sh Outdated Show resolved Hide resolved
Copy link
Member

@richvdh richvdh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@anoadragon453 anoadragon453 merged commit 6676ee9 into develop Dec 11, 2019
@anoadragon453 anoadragon453 deleted the anoa/update_full_schema_instructions branch December 11, 2019 13:16
babolivier pushed a commit that referenced this pull request Sep 1, 2021
* commit '6676ee9c4':
  Add dev script to generate full SQL schema files (#6394)
sqlite3 "$SQLITE_DB" ".dump" > "$OUTPUT_DIR/$SQLITE_FULL_SCHEMA_OUTPUT_FILE"

echo "Dumping Postgres schema to '$OUTPUT_DIR/$POSTGRES_FULL_SCHEMA_OUTPUT_FILE'..."
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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing ^SELECT lines has the unfortunate side effect of removing

SELECT pg_catalog.setval('account_data_sequence', 1, true);
SELECT pg_catalog.setval('application_services_txn_id_seq', 1, false);
SELECT pg_catalog.setval('cache_invalidation_stream_seq', 1, true);
SELECT pg_catalog.setval('device_inbox_sequence', 1, true);
SELECT pg_catalog.setval('event_auth_chain_id', 1, false);
SELECT pg_catalog.setval('events_backfill_stream_seq', 1, true);
SELECT pg_catalog.setval('events_stream_seq', 1, true);
SELECT pg_catalog.setval('instance_map_instance_id_seq', 1, false);
SELECT pg_catalog.setval('presence_stream_sequence', 1, true);
SELECT pg_catalog.setval('receipts_sequence', 1, true);
SELECT pg_catalog.setval('user_id_seq', 1, false);

Note that passing true to setval marks the set value as having been used, so that nextval will be larger. https://www.postgresql.org/docs/current/functions-sequence.html

This probably explains #13873 (comment).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants