-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(indexer-alt): framework from sui-indexer-alt (#20502)
## Description Pull out the parts of `sui-indexer-alt` that can be used to create any indexer, in `sui-indexer-alt-framework`. Also take this opportunity to clean up dependencies and lock down visibilities between projects. ## Test plan CI ## Stack - #20499 --- ## 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
Showing
68 changed files
with
860 additions
and
576 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
[package] | ||
name = "sui-indexer-alt-framework" | ||
version.workspace = true | ||
authors = ["Mysten Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
publish = false | ||
edition = "2021" | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
async-trait.workspace = true | ||
axum.workspace = true | ||
backoff.workspace = true | ||
bb8 = "0.8.5" | ||
chrono.workspace = true | ||
clap.workspace = true | ||
diesel = { workspace = true, features = ["chrono"] } | ||
diesel-async = { workspace = true, features = ["bb8", "postgres", "async-connection-wrapper"] } | ||
diesel_migrations.workspace = true | ||
futures.workspace = true | ||
prometheus.workspace = true | ||
reqwest.workspace = true | ||
serde.workspace = true | ||
thiserror.workspace = true | ||
tokio.workspace = true | ||
tokio-stream.workspace = true | ||
tokio-util.workspace = true | ||
tracing.workspace = true | ||
url.workspace = true | ||
|
||
sui-field-count.workspace = true | ||
sui-storage.workspace = true | ||
sui-types.workspace = true | ||
|
||
[dev-dependencies] | ||
rand.workspace = true | ||
telemetry-subscribers.workspace = true | ||
tempfile.workspace = true | ||
wiremock.workspace = true | ||
|
||
sui-pg-temp-db.workspace = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[print_schema] | ||
file = "src/schema.rs" | ||
patch_file = "schema.patch" | ||
|
||
[migrations_directory] | ||
dir = "migrations" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#!/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-alt-framework/migrations" | ||
|
||
# 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-alt-framework/schema.patch" \ | ||
> "$REPO/crates/sui-indexer-alt-framework/src/schema.rs" |
6 changes: 6 additions & 0 deletions
6
crates/sui-indexer-alt-framework/migrations/00000000000000_diesel_initial_setup/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass); | ||
DROP FUNCTION IF EXISTS diesel_set_updated_at(); |
36 changes: 36 additions & 0 deletions
36
crates/sui-indexer-alt-framework/migrations/00000000000000_diesel_initial_setup/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- This file was automatically created by Diesel to setup helper functions | ||
-- and other internal bookkeeping. This file is safe to edit, any future | ||
-- changes will be added to existing projects as new migrations. | ||
|
||
|
||
|
||
|
||
-- Sets up a trigger for the given table to automatically set a column called | ||
-- `updated_at` whenever the row is modified (unless `updated_at` was included | ||
-- in the modified columns) | ||
-- | ||
-- # Example | ||
-- | ||
-- ```sql | ||
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW()); | ||
-- | ||
-- SELECT diesel_manage_updated_at('users'); | ||
-- ``` | ||
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$ | ||
BEGIN | ||
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s | ||
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl); | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$ | ||
BEGIN | ||
IF ( | ||
NEW IS DISTINCT FROM OLD AND | ||
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at | ||
) THEN | ||
NEW.updated_at := current_timestamp; | ||
END IF; | ||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
diff --git a/crates/sui-indexer-alt-framework/src/schema.rs b/crates/sui-indexer-alt-framework/src/schema.rs | ||
--- a/crates/sui-indexer-alt-framework/src/schema.rs | ||
+++ b/crates/sui-indexer-alt-framework/src/schema.rs | ||
@@ -1 +1,3 @@ | ||
+// Copyright (c) Mysten Labs, Inc. | ||
+// SPDX-License-Identifier: Apache-2.0 | ||
// @generated automatically by Diesel CLI. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.