forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_indexer_schema.sh
executable file
·78 lines (66 loc) · 2.53 KB
/
generate_indexer_schema.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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"