Skip to content

Commit

Permalink
indexer: ensure schema always matches what diesel generated (#19337)
Browse files Browse the repository at this point in the history
## Description 

Create a script that regenerates the schema and run it to standardise
our schema. Also merge `schema/mod.rs` and `schema/pg.rs` into one
`schema.rs` -- otherwise we have an opportunity for things to go out of
sync because a module is present in the generated schema (`pg.rs`) but
not in the module entrypoint (`mod.rs`).

Run this script to refresh the schema (some inconsistencies already
crept in!) and set it up to run under CI.

## Test plan 

```
sui$ ./scripts/generate_indexer_schema.rs`
```

and CI

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
amnn committed Sep 12, 2024
1 parent 8ffc2a6 commit 02fbd47
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 107 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ jobs:
with:
ref: ${{ github.event.inputs.sui_repo_ref || github.ref }}
- uses: taiki-e/install-action@nextest
- name: Add postgres to PATH
run: echo "/usr/lib/postgresql/14/bin" >> $GITHUB_PATH
- name: Set Swap Space
uses: pierotofy/set-swap-space@master
with:
Expand All @@ -179,6 +181,14 @@ jobs:
- name: sui-execution
run: |
./scripts/execution_layer.py generate-lib
- name: Install diesel CLI, and cache the binary
uses: baptiste0928/cargo-install@1cd874a5478fdca35d868ccc74640c5aabbb8f1b # pin@v3.0.0
with:
crate: diesel_cli
locked: true
- name: Indexer schema
run: |
./scripts/generate_indexer_schema.sh
# Ensure there are no uncommitted changes in the repo after running tests
- run: scripts/changed-files.sh
shell: bash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ diesel::table! {
validator_signature -> Bytea,
end_of_epoch_data -> Nullable<Bytea>,
min_tx_sequence_number -> Nullable<Int8>,
max_tx_sequence_number -> Nullable<Int8>
max_tx_sequence_number -> Nullable<Int8>,
}
}

Expand Down Expand Up @@ -146,20 +146,6 @@ diesel::table! {
}
}

diesel::table! {
events_partition_0 (tx_sequence_number, event_sequence_number) {
tx_sequence_number -> Int8,
event_sequence_number -> Int8,
transaction_digest -> Bytea,
senders -> Array<Nullable<Bytea>>,
package -> Bytea,
module -> Text,
event_type -> Text,
timestamp_ms -> Int8,
bcs -> Bytea,
}
}

diesel::table! {
feature_flags (protocol_version, flag_name) {
protocol_version -> Int8,
Expand Down Expand Up @@ -221,29 +207,6 @@ diesel::table! {
}
}

diesel::table! {
objects_history_partition_0 (checkpoint_sequence_number, object_id, object_version) {
object_id -> Bytea,
object_version -> Int8,
object_status -> Int2,
object_digest -> Nullable<Bytea>,
checkpoint_sequence_number -> Int8,
owner_type -> Nullable<Int2>,
owner_id -> Nullable<Bytea>,
object_type -> Nullable<Text>,
object_type_package -> Nullable<Bytea>,
object_type_module -> Nullable<Text>,
object_type_name -> Nullable<Text>,
serialized_object -> Nullable<Bytea>,
coin_type -> Nullable<Text>,
coin_balance -> Nullable<Int8>,
df_kind -> Nullable<Int2>,
df_name -> Nullable<Bytea>,
df_object_type -> Nullable<Text>,
df_object_id -> Nullable<Bytea>,
}
}

diesel::table! {
objects_snapshot (object_id) {
object_id -> Bytea,
Expand Down Expand Up @@ -275,14 +238,6 @@ diesel::table! {
}
}

diesel::table! {
protocol_configs (protocol_version, config_name) {
protocol_version -> Int8,
config_name -> Text,
config_value -> Nullable<Text>,
}
}

diesel::table! {
packages (package_id, original_id, package_version) {
package_id -> Bytea,
Expand All @@ -294,31 +249,23 @@ diesel::table! {
}

diesel::table! {
pruner_cp_watermark (checkpoint_sequence_number) {
checkpoint_sequence_number -> Int8,
min_tx_sequence_number -> Int8,
max_tx_sequence_number -> Int8,
protocol_configs (protocol_version, config_name) {
protocol_version -> Int8,
config_name -> Text,
config_value -> Nullable<Text>,
}
}

diesel::table! {
transactions (tx_sequence_number) {
tx_sequence_number -> Int8,
transaction_digest -> Bytea,
raw_transaction -> Bytea,
raw_effects -> Bytea,
pruner_cp_watermark (checkpoint_sequence_number) {
checkpoint_sequence_number -> Int8,
timestamp_ms -> Int8,
object_changes -> Array<Nullable<Bytea>>,
balance_changes -> Array<Nullable<Bytea>>,
events -> Array<Nullable<Bytea>>,
transaction_kind -> Int2,
success_command_count -> Int2,
min_tx_sequence_number -> Int8,
max_tx_sequence_number -> Int8,
}
}

diesel::table! {
transactions_partition_0 (tx_sequence_number) {
transactions (tx_sequence_number) {
tx_sequence_number -> Int8,
transaction_digest -> Bytea,
raw_transaction -> Bytea,
Expand Down Expand Up @@ -418,18 +365,16 @@ diesel::allow_tables_to_appear_in_same_query!(
event_struct_name,
event_struct_package,
events,
events_partition_0,
feature_flags,
full_objects_history,
objects,
objects_history,
objects_history_partition_0,
objects_snapshot,
objects_version,
packages,
protocol_configs,
pruner_cp_watermark,
transactions,
transactions_partition_0,
tx_calls_fun,
tx_calls_mod,
tx_calls_pkg,
Expand Down
42 changes: 0 additions & 42 deletions crates/sui-indexer/src/schema/mod.rs

This file was deleted.

78 changes: 78 additions & 0 deletions scripts/generate_indexer_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0
#
# Update sui-indexer's generated src/schema.rs based on the schema after
# running all its migrations on a clean database. Expects the first argument to
# be a port to run the temporary database on (defaults to 5433).

set -x
set -e

if ! command -v git &> /dev/null; then
echo "Please install git: e.g. brew install git" >&2
exit 1
fi

for PG in psql initdb postgres pg_isready pg_ctl; do
if ! command -v $PG &> /dev/null; then
echo "Could not find $PG. Please install postgres: e.g. brew install postgresql@15" >&2
exit 1
fi
done

if ! command -v diesel &> /dev/null; then
echo "Please install diesel: e.g. cargo install diesel_cli --features postgres" >&2
exit 1
fi

REPO=$(git rev-parse --show-toplevel)

# Create a temporary directory to store the ephemeral DB.
TMP=$(mktemp -d)

# Set-up a trap to clean everything up on EXIT (stop DB, delete temp directory)
function cleanup {
pg_ctl stop -D "$TMP" -mfast
set +x
echo "Postgres STDOUT:"
cat "$TMP/db.stdout"
echo "Postgres STDERR:"
cat "$TMP/db.stderr"
set -x
rm -rf "$TMP"
}
trap cleanup EXIT

# Create a new database in the temporary directory
initdb -D "$TMP" --user postgres

# Run the DB in the background, on the port provided and capture its output
PORT=${1:-5433}
postgres -D "$TMP" -p "$PORT" -c unix_socket_directories= \
> "$TMP/db.stdout" \
2> "$TMP/db.stderr" &

# Wait for postgres to report as ready
RETRIES=0
while ! pg_isready -p "$PORT" --host "localhost" --username "postgres"; do
if [ $RETRIES -gt 5 ]; then
echo "Postgres failed to start" >&2
exit 1
fi
sleep 1
RETRIES=$((RETRIES + 1))
done

# Run all migrations on the new database
diesel migration run \
--database-url "postgres://postgres:postgrespw@localhost:$PORT" \
--migration-dir "$REPO/crates/sui-indexer/migrations/pg"

# Generate the schema.rs file, excluding partition tables and including the
# copyright notice.
diesel print-schema \
--database-url "postgres://postgres:postgrespw@localhost:$PORT" \
--patch-file "$REPO/crates/sui-indexer/src/schema.patch" \
--except-tables "^objects_version_|_partition_" \
> "$REPO/crates/sui-indexer/src/schema.rs"

0 comments on commit 02fbd47

Please sign in to comment.